Android Interview Questions - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Android Toast

Android Toast

At times, users need to take a decision (Yes/No) in response to the action they have performed. In these cases, you can use Android Alert Dialog to help user take decisions by remaining in the same activity without even changing the screen. On the other hand, Android Toast can be used to display the information to the user for a short interval. A toast contains message to be displayed quickly and disappears after sometime. Also, you can create custom toast as well. The android.widget.Toast class is the subclass of java.lang.Object class. Now, let us give you an overview of how to use Android Toast and Android AlertDialog in applications. AlertDialog: Many times in our applications, we want to alert the user to take a decision.  Here, AlertDialog class can be used, where a message and some buttons(one or two or three) are displayed in a popup window. In order to make use of alert dialog, you should create an object of AlertDialogBuilder, which is an inner class of AlertDialog. Also, you have to set positive and negative buttons. Ex:
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
alertDialog.setMessage("Are you sure,You wanted to exit this App");
alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {

}
});

alertDialog.setNegativeButton("No",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

}
});

AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
});
Apart from the above functions, AlertDialog.Builder provides variuos other functions to customize the application. You can also create and customize dialog using Dialogfragment, single choice list dialog, list dialog, and the list goes on. Toast: Android toast provides user a feedback about an operation in a small popup. It only takes the amount of space required for the message and the current activity remains visible and interactive. You can set gravity for Toast and customize the application. Ex:
Toast.makeText(MainActivity.this,"You clicked on No Button",Toast.LENGTH_LONG).show();
The following example illustrates how to use AlertDialog and Toast in your respective projects: Create MainActivity.java under src/<your packagename>. MainActivity.java: [java] public class MainActivity extends Activity { Context context; @Override protected void onCreate(Bundle arg0) { super.onCreate(arg0); //hide action bar requestWindowFeature(Window.FEATURE_NO_TITLE); //set activity to external view setContentView(R.layout.activity_main); Button exit=(Button)findViewById(R.id.btnExit); //on click exit call listener exit.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this); alertDialog.setMessage("Are you sure,You wanted to exit this App"); alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { } }); alertDialog.setNegativeButton("No",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"You clicked on No Button",Toast.LENGTH_LONG).show(); } }); AlertDialog alert = alertDialogBuilder.create(); alert.show(); } }); } } [/java] Create activity_main.xml under res/layout folder. activity_main.xml: [xml] <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:gravity="center" android:orientation="vertical" > <Button android:id="@+id/btnExit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Exit" android:background="#009ACD"/> </LinearLayout> [/xml] AndroidManifest.xml: [xml] <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.spl.myproject" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.spl.myproject.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> [/xml] Output: