Android AlertDialog Single Button

157,382

Solution 1

Couldn't that just be done by only using a positive button?

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Look at this dialog!")
       .setCancelable(false)
       .setPositiveButton("OK", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                //do things
           }
       });
AlertDialog alert = builder.create();
alert.show();

Solution 2

You could use this:

AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
builder1.setTitle("Title");
builder1.setMessage("my message");
builder1.setCancelable(true);
builder1.setNeutralButton(android.R.string.ok,
        new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        dialog.cancel();
    }
});

AlertDialog alert11 = builder1.create();
alert11.show();

Solution 3

Another approach

Builder alert = new AlertDialog.Builder(ActivityName.this);
alert.setTitle("Doctor");
alert.setMessage("message");
alert.setPositiveButton("OK",null);
alert.show(); 

Bonus

AlertDialog.Builder builder = new AlertDialog.Builder(YourActivityName.this);
builder.setMessage("Message dialog with three buttons");

       builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                //do things
           }
       });

      builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                //do things
           }
       });

       builder.setNeutralButton("CANCEL", new DialogInterface.OnClickListener()     {
           public void onClick(DialogInterface dialog, int id) {
                //do things
           }
       });
AlertDialog alert = builder.create();
alert.show();

Now it is up to you to use one,two or three buttons..

Solution 4

This is the closer I could get to the one liner this should be if the Android API was any smart:

new AlertDialog.Builder(this)
    .setMessage(msg)
    .setPositiveButton("OK", null)
    .show();

Solution 5

For code reuse, You can make it in a method like this

public static Dialog getDialog(Context context,String title, String message, DialogType typeButtons ) {

        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(title)
        .setMessage(message)
               .setCancelable(false);

        if (typeButtons == DialogType.SINGLE_BUTTON) {
            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                        //do things
                   }
               });
        }

        AlertDialog alert = builder.create();

        return alert;
    }

    public enum DialogType {
        SINGLE_BUTTON

    }

//Other code reuse issues like using interfaces for providing feedback will also be excellent.

Share:
157,382

Related videos on Youtube

Elec0
Author by

Elec0

Updated on April 27, 2020

Comments

  • Elec0
    Elec0 about 4 years

    I'd like to have an AlertDialog builder that only has one button that says OK or Done or something, instead of the default yes and no. Can that be done with the standard AlertDialog, or would I have to use something else?

  • MSaudi
    MSaudi about 9 years
    @MarianPaździoch why you think that ?
  • SMBiggs
    SMBiggs over 8 years
    Works great, as long as you don't mind that single button right justified. :(
  • aberaud
    aberaud almost 8 years
    @ScottBiggs Button alignment in dialogs are decided by the system and could change between devices, versions, and languages (RTL etc.). The App shouldn't attempt to set the alignment but the intent (positive, negative etc.)
  • Jaimin Modi
    Jaimin Modi about 7 years
    what if i want to add four buttons ?
  • IgorGanapolsky
    IgorGanapolsky almost 7 years
    Wouldn't that cause the one button to be right-justified?
  • JackLametta
    JackLametta almost 7 years
    Awesome! Thanks!
  • Utkarsh Vishnoi
    Utkarsh Vishnoi over 6 years
    I don't want the button to perform any action, it should simply just dismiss the alertbox, Is it a good practice to make an empty onClick listener or should i use a different approach.
  • Joshua Pinter
    Joshua Pinter almost 6 years
    This is okay if it's just for a "confirmation dialog" with a single button with no listener. Anything more than that and it's not worth wrapping in a helper like this because you'll need to handle each button press.
  • Muhammad Younus
    Muhammad Younus about 5 years
    Simple and easy.Thanks