Android Alert Dialog with One, Two, and Three Buttons

Android Alert Dialog with one, two, and three buttons

One button

Sample Image

import android.support.v7.app.AlertDialog;

public class MainActivity extends AppCompatActivity {

public void showAlertDialogButtonClicked(View view) {

// setup the alert builder
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("My title");
builder.setMessage("This is my message.");

// add a button
builder.setPositiveButton("OK", null);

// create and show the alert dialog
AlertDialog dialog = builder.create();
dialog.show();
}
}

Two buttons

Sample Image

public class MainActivity extends AppCompatActivity {

public void showAlertDialogButtonClicked(View view) {

// setup the alert builder
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("AlertDialog");
builder.setMessage("Would you like to continue learning how to use Android alerts?");

// add the buttons
builder.setPositiveButton("Continue", null);
builder.setNegativeButton("Cancel", null);

// create and show the alert dialog
AlertDialog dialog = builder.create();
dialog.show();
}
}

Three buttons

Sample Image

public class MainActivity extends AppCompatActivity {

public void showAlertDialogButtonClicked(View view) {

// setup the alert builder
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Notice");
builder.setMessage("Launching this missile will destroy the entire universe. Is this what you intended to do?");

// add the buttons
builder.setPositiveButton("Launch missile", null);
builder.setNeutralButton("Remind me later", null);
builder.setNegativeButton("Cancel", null);

// create and show the alert dialog
AlertDialog dialog = builder.create();
dialog.show();
}
}

If the button text it too long to all fit horizontally, then it will automatically get laid out in a vertical column of three buttons.

Sample Image

Handling Button Clicks

The OnClickListener was null in the above examples. You can replace null with a listener to do something when the user taps a button. For example:

builder.setPositiveButton("Launch missile", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

// do something like...
launchMissile();
}
});

Going On

There are many more varieties of dialogs that you can make. See the documentation for help with this.

Since only three buttons are supported in an AlertDialog, here is an example of a dialog with a list.

Sample Image

public class MainActivity extends AppCompatActivity {

public void showAlertDialogButtonClicked(View view) {

// setup the alert builder
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Choose an animal");

// add a list
String[] animals = {"horse", "cow", "camel", "sheep", "goat"};
builder.setItems(animals, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0: // horse
case 1: // cow
case 2: // camel
case 3: // sheep
case 4: // goat
}
}
});

// create and show the alert dialog
AlertDialog dialog = builder.create();
dialog.show();
}
}

See this answer for similar examples of a radio button list and a checkbox list.

Notes

  • Use string resources rather than hard coded strings.
  • You can wrap everything in a class that extends DialogFragment for easy reuse of a dialog. (See this for help.)
  • These examples used the support library to support versions prior to API 11. So the import should be

    import android.support.v7.app.AlertDialog;
  • I omitted the onCreate method in the examples above for brevity. There was nothing special there.

See also

  • How to disable the positive button
  • Use a Toast rather than an Alert for short messages
  • Single-choice list, radio button list, and checkbox list
  • How to implement a custom AlertDialog View

AlertDialog with EditText and Three buttons

There is no need to call boite.show() several times, just call it once like below :

   b5.setOnClickListener(new View.OnClickListener() {
@SuppressLint("UseCompatLoadingForDrawables")
@Override
public void onClick(View view) {
AlertDialog.Builder boite;
boite = new AlertDialog.Builder(MainActivity.this);
boite.setTitle("boite de dialogue");
boite.setIcon(getDrawable(R.drawable.warning_shield_96px));

final EditText input = new EditText(MainActivity.this);
input.setInputType(InputType.TYPE_CLASS_TEXT);
boite.setView(input);

boite.setPositiveButton("OUI", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//whatever action
}
});
boite.setNegativeButton("NON", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//whatever action
}
});
boite.setNeutralButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//whatever action
}
});
boite.show();
}
});

AlertDialog uses Builder Pattern to initialize, so you can set different methods and buttons and anything you like, then when you call alertDialog.show() it builds the object with any configs you set before that call.

How can I add a third button to an Android Alert Dialog?

This code snippet should help explain the three different buttons you can use:

    alertDialog = new AlertDialog.Builder(this).create();

alertDialog.setTitle("Dialog Button");

alertDialog.setMessage("This is a three-button dialog!");

alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Button 1 Text", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int id) {

//...

} });

alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Button 2 Text", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int id) {

//...

}});

alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "Button 3 Text", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int id) {

//...

}});

How to add multiple buttons on a single AlertDialog

I would inflate the AlertDialog with my own custom view (my_alert_dialog.xml).

AlertDialog.Builder alert = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
//inflate view for alertdialog since we are using multiple views inside a viewgroup (root = Layout top-level) (linear, relative, framelayout etc..)
View view = inflater.inflate(R.layout.my_alert_dialog, (ViewGroup) findViewById(R.id.root));

Button button1 = (Button) view.findViewById(R.id.button1); // etc.. for button2,3,4.
alert.setView(view);
alert.show();

Dialog with more than 2 buttons

You can add a third button using setPositiveButton but aside from that, you'll have to set a custom view. To set a custom view you just call setView(yourView) with the AlertDialog.Builder.

Set 3 buttons on alert dialog, android?

Using a custom dialog is more user friendly and more easy in my opinion.

Just have a play button in the dialog. When pressed change the text to "Stop".

Here is a simple tutorial on custom dialog to get you going.

Hope this helps.

Alert Dialog Two Buttons

try this

public void showDialog(Activity activity, String title, CharSequence message) {
AlertDialog.Builder builder = new AlertDialog.Builder(activity);

if (title != null) builder.setTitle(title);

builder.setMessage(message);
builder.setPositiveButton("OK", null);
builder.setNegativeButton("Cancel", null);
builder.show();
}


Related Topics



Leave a reply



Submit