3
When the user clicks on an option he can receive an Alert saying, you selected certain command, certain option, how to make an alert of this type in an Android application?
3
When the user clicks on an option he can receive an Alert saying, you selected certain command, certain option, how to make an alert of this type in an Android application?
5
For short and fast alerts, we can make use of Toasts
:
A toast
allows to display a short and quick message to the user (used only with text).
See an example of an application:
In the package of my application br.com.toasts
, the file MainActivity.java
:
package br.com.toasts;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void notify1(View v) {
Toast t = Toast.makeText(this, "Aviso!", Toast.LENGTH_SHORT);
t.show();
}
public void notify2(View v) {
Toast t = Toast.makeText(this, "Aviso!", Toast.LENGTH_SHORT);
t.setGravity(Gravity.CENTER, 0, 0);
t.show();
}
public void notify3(View v) {
Toast t = new Toast(this);
t.setGravity(Gravity.CENTER,0, 0);
View view = getLayoutInflater().inflate(R.layout.toast, null);
TextView textView = (TextView) view.findViewById(R.id.txt_toast);
textView.setText("Aviso!");
t.setView(view);
t.show();
}
}
In the folder layout
> filing cabinet activity_main.xml
:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="horizontal"
style="?android:attr/buttonBarStyle"
tools:context=".MainActivity">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="notify1"
android:text="@string/btn_toast1"
style="?android:attr/buttonBarButtonStyle"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="notify2"
android:text="@string/btn_toast2"
style="?android:attr/buttonBarButtonStyle"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="notify3"
android:text="@string/btn_toast3"
style="?android:attr/buttonBarButtonStyle"/>
</LinearLayout>
In the folder layout
> filing cabinet toast.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#FFFF00"
android:padding="10dp" >
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:src="@android:drawable/ic_dialog_alert"
tools:ignore="ContentDescription" />
<TextView
android:id="@+id/txt_toast"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:gravity="center_vertical"
android:layout_marginStart="10dp"
android:textStyle="bold"
style="?android:attr/textAppearanceMedium" />
</LinearLayout>
In the folder values
> filing cabinet strings.xml
:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Toast</string>
<string name="btn_toast1">Toast 1</string>
<string name="btn_toast2">Toast 2</string>
<string name="btn_toast3">Toast 3</string>
</resources>
In this example application, there are three types of warning, as in the image below:
Browser other questions tagged android alert
You are not signed in. Login or sign up in order to post.
It is always useful for the author of the answer to know what motivates a negative vote. If those who voted (or others) see wrong things in the reply it is very good to comment and warn the author.
– Sergio