Android AlertDialog

AlertDialog is a window that provides feedback to the user. Unlike a Toast that fades by itself, however, an AlertDialog shows indefinitely until it loses focus. In addition, an AlertDialog can contain up to three buttons and a list of selectable items. A button added to an AlertDialog can be connected to a listener that gets triggered when the button is clicked.

Example of a AlertDialog.

The android.app.AlertDialog class is the template for creating an AlertDialog. All constructors in this class are protected, so you cannot use them unless you are subclassing the class. Instead, you should use the AlertDialog.Builder class to create an AlertDialog.

You can use one of the two constructors of AlertDialog.Builder.

public AlertDialog.Builder(android.content.Context context)
public AlertDialog.Builder(android.content.Context context, int theme)

Once you have an instance of AlertDialog.Builder, you can call its create method to return an AlertDialog. However, before calling create you can call various methods of AlertDialog.Builder to decorate the resulting AlertDialog. Interestingly, the methods in AlertDialog.Builder return the same instance of AlertDialog.Builder, so you can cascade them. Here are some of the methods in AlertDialog.Builder.

public AlertDialog.Builder setIcon(int resourceId)

Sets the icon of the resulting AlertDialog with the Drawable pointed by resourceId.
public AlertDialog.Builder setMessage(java.lang.CharSequence message)

Sets the message of the resulting AlertDialog .
public AlertDialog.Builder setTitle(java.lang.CharSequence title)

Sets the title of the resulting AlertDialog.
public AlertDialog.Builder setNegativeButton(
java.lang.CharSequence text,
android.content.DialogInterface.OnClickListener listener)

 

Assigns a button that the user should click to provide a negative response.
public AlertDialog.Builder setPositiveButton(java.lang.CharSequence text,android.content.DialogInterface.OnClickListener listener)

 

Assigns a button that the user should click to provide a positive response.
public AlertDialog.Builder setNeutralButton(java.lang.CharSequence text,android.content.DialogInterface.OnClickListener listener)

 

Assigns a button that the user should click to provide a neutral response.

For instance, the following code produces an AlertDialog –

new AlertDialog.Builder(this)

.setTitle(“Please confirm”)

.setMessage(

“Are you sure you want to delete ” +

“this contact?”)

.setPositiveButton(“Yes”,

new DialogInterface.OnClickListener() {

public void onClick(

DialogInterface dialog,

int whichButton) {

// delete picture here

dialog.dismiss();

}

})

.setNegativeButton(“No”,

new DialogInterface.OnClickListener() {

public void onClick(

DialogInterface dialog,

int which) {

dialog.dismiss();

}

})

.create()

.show();

Pressing the Yes button will execute the listener passed to the setPositiveButton method and pressing the No button will run the listener passed to the setNegativeButton method.

 

Leave a Reply

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