mytechead

Android & Wordpress Help center

How to create alert dialog box in android??

This post explains, how to create alert dialog box in android:

You can use the following code to create an alert dialog box:

String positiveButtonText = "OK";
String negativeButtonText = "Cancel";

AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("This is my tittle");
alert.setMessage("This is body of my alert box");

//To add buttons
alert.setPositiveButton(positiveButtonText, new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
//Do some onClick handling here
}
});

alert.setNegativeButton(negativeButtonText, new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
//Do some onClick handling here
}
});

alert.show();

Note: .show() method displays the alert dialog box, if you forget to add it, your alert box will not be visible ever!!

Advertisements

Single Post Navigation

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: