When the user clicks on an option, he can receive an Alert saying, did you select a certain command, a certain option, how to make an alert of this type in an Android application?
When the user clicks on an option, he can receive an Alert saying, did you select a certain command, a certain option, how to make an alert of this type in an Android application?
For short and quick alerts, we can make use of Toasts
:
A toast
allows you to display a short, quick message to the user (used only with text).
See the example of an application:
In my application package 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 layout
> file 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 layout
> file 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 values
> file 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 alert, as in the image below: