1.

I have to build one Alert Dialog box for my app. Write the step to show Alert Dialogue box and why we are using it.

Answer»

In Android application when we need to display a message to the user with to warn him with a button like “Ok” and “Cancel” we are using Alert Dialog Box.

Android Alert Dialog is built with the use of THREE FIELDS: Title, Message AREA, Action Button.  The code for creating an Alert Dialogue box is mention below:

AlertDialog.Builder alertDialogBuilder = NEW AlertDialog.Builder(context); // set title alertDialogBuilder.setTitle(" Are you Sure"); // set dialog message alertDialogBuilder .setMessage("Click yes to exit!") .setCancelable(false) .setPositiveButton("Yes",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int ID) { // if this button is clicked, close // current activity MainActivity.this.finish(); }   }) .setNegativeButton("No",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int id) { // if this button is clicked, just close // the dialog box and do nothing dialog.cancel(); } });// create alert dialog AlertDialog alertDialog = alertDialogBuilder.create(); // show it alertDialog.show();


Discussion

No Comment Found

Related InterviewSolutions