How to show Alert Dialog / popup in Android

AlertDialog Example

In Android alert dialog is like a popup message to confirm some thing from end user (user of your application /  game).

In Android it is very easy to create / show alert dialog. Before deletion of any item it is necessary to confirm form user by AlertDialog. Code in following function is describing how to show a simple alert dialog in android application.

Alert Dialog with three buttons

It is like confirm dialog in html

alertdialog-android

This dialog contains two buttons “Yes” and “No” with message “Are you sure you want to delete?”. You can change button names and popup title and message.

Below is dialog code.

[sociallocker]

I am using alert builder.

private void showAlertDialog()
	{		
		AlertDialog.Builder builder = new AlertDialog.Builder(this); 
		builder.setMessage("Are you sure you want to delete?")
		.setTitle("Are you sure?") 
		.setCancelable(false)
		.setPositiveButton("Yes", 
				new DialogInterface.OnClickListener() { 
			public void onClick(DialogInterface dialog, int id) {
				// Perform some action;
			}
		})
		.setNegativeButton("No", new DialogInterface.OnClickListener() { 
			public void onClick(DialogInterface dialog, int id) {
				dialog.cancel(); 
				// Cancel your action 
			}
		});
		builder.create().show();
	}

[/sociallocker]

Leave a comment

Your email address will not be published. Required fields are marked *