android alertdialog
ReportQuestion
2 years ago 746 views
Android AlertDialog can be used to display the dialog message with OK and Cancel buttons. It can be used to interrupt and ask the user about his/her choice to continue or discontinue.
Android AlertDialog is composed of three regions: title, content area and action buttons.
Android AlertDialog is the subclass of Dialog class.
These methods are used to place the content in all regions.
1. public AlertDialog.Builder setTitle(CharSequence)
2. public AlertDialog.Builder setMessage(CharSequence)
3. public AlertDialog.Builder setIcon(int)
AlertDialog.Builder builder;
builder = new AlertDialog.Builder(this);
- builder.setMessage(R.string.dialog_message) .setTitle(R.string.dialog_title);
- //Setting message manually and performing action on button click
- builder.setMessage("Do you want to close this application ?")
- .setCancelable(false)
- .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- finish();
- Toast.makeText(getApplicationContext(),"you choose yes action for alertbox",
- Toast.LENGTH_SHORT).show();
- }
- })
- .setNegativeButton("No", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- // Action for 'NO' Button
- dialog.cancel();
- Toast.makeText(getApplicationContext(),"you choose no action for alertbox",
- Toast.LENGTH_SHORT).show();
- }
- });
- //Creating dialog box
- AlertDialog alert = builder.create();
- //Setting the title manually
- alert.setTitle("AlertDialogExample");
- alert.show();
Thread Reply