Dialogs
Dialog is small window that show the message user related information or it takes some additional information from user.
The Dialog class is the base class for dialogs, but you should avoid instantiating Dialog directly instead, use one of the following subclasses.
AlertDialog
Alert Dialog is generally use for showing alert message to user. We can set different buttons for get the confirmation of user.
Alert dialog has three parts :
1. Title : It is optional part. Title is the upper part of the Dialog.
2. Content :This Displays the message to user. It can be string or Custom Layout.
3. Action Buttons : Action Buttons are three Types. They are Positive, Negative and Neutral action buttons. If user accept the action, Use positive Button. It is normally displayed as OK/YES. If the user Reject the action, It is normally displayed as NO. If the user postpone the action, It is normally displayed as LATER/CANCEL.
How to show Alert dialog in Android
AlertDialog with one Button in Android
AlertDialog with two Button
AlertDialog with Three Buttons
Dialog is small window that show the message user related information or it takes some additional information from user.
The Dialog class is the base class for dialogs, but you should avoid instantiating Dialog directly instead, use one of the following subclasses.
AlertDialog
Alert Dialog is generally use for showing alert message to user. We can set different buttons for get the confirmation of user.
Alert dialog has three parts :
1. Title : It is optional part. Title is the upper part of the Dialog.
2. Content :This Displays the message to user. It can be string or Custom Layout.
3. Action Buttons : Action Buttons are three Types. They are Positive, Negative and Neutral action buttons. If user accept the action, Use positive Button. It is normally displayed as OK/YES. If the user Reject the action, It is normally displayed as NO. If the user postpone the action, It is normally displayed as LATER/CANCEL.
How to show Alert dialog in Android
// Initilize Alert Dialog AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this); //set title alertDialogBuilder.setTitle("Your Title"); | |
// 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(); |
AlertDialog with one Button in Android
| ||
// Setting Dialog Title | ||
alertDialogBuilder.setTitle("Alert Dialog"); | ||
// Setting Dialog Message | ||
alertDialogBuilder.setMessage("Alert Dialog with One Button!!"); | ||
// Setting OK Button | ||
alertDialogBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { | ||
public void onClick(DialogInterface dialog, int id) { | ||
Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show(); | ||
} | ||
}); | ||
// Showing Alert Message | ||
alertDialogBuilder.show(); |
AlertDialog with two Button
| ||
// Setting Dialog Title | ||
alertDialogBuilder.setTitle("Alert Dialog"); | ||
// Setting Dialog Message | ||
alertDialogBuilder.setMessage("Alert Dialog with Two Button!!"); | ||
// Setting positive OK Button | ||
alertDialogBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { | ||
public void onClick(DialogInterface dialog, int id) { | ||
Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show(); | ||
} | ||
}); | ||
// Setting Negative "NO" Button | ||
alertDialogBuilder.setNegativeButton("NO", new DialogInterface.OnClickListener() { | ||
public void onClick(DialogInterface dialog, int which) { | ||
// Write your code here to invoke NO event | ||
Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show(); | ||
dialog.cancel(); | ||
} | ||
}); | ||
// Showing Alert Message | ||
alertDialogBuilder.show(); |
AlertDialog with Three Buttons
| ||
// Setting Dialog Title | ||
alertDialogBuilder.setTitle("Alert Dialog"); | ||
// Setting Dialog Message | ||
alertDialogBuilder.setMessage("Alert Dialog with Two Button!!"); | ||
// Setting positive OK Button | ||
alertDialogBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { | ||
public void onClick(DialogInterface dialog, int id) { | ||
Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show(); | ||
} | ||
}); | ||
// Setting Negative "NO" Button | ||
alertDialogBuilder.setNegativeButton("NO", new DialogInterface.OnClickListener() { | ||
public void onClick(DialogInterface dialog, int which) { | ||
// Write your code here to invoke NO event | ||
Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show(); | ||
dialog.cancel(); | ||
} | ||
}); | ||
// Setting Netural "Cancel" Button | ||
alertDialogBuilder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() { | ||
public void onClick(DialogInterface dialog, int which) { | ||
// User pressed Cancel button. Write Logic Here | ||
Toast.makeText(getApplicationContext(), "You clicked on Cancel", | ||
Toast.LENGTH_SHORT).show(); | ||
} | ||
}); | ||
// Showing Alert Message | ||
alertDialogBuilder.show();
Download the project from Github |
0 comments:
Post a Comment