43

Hello all i have a simple problem i have a alertDialog and i want it to show two buttons i have searched here but it seems the options before don't work anymore and are deprecated.

Anyone know the new way of doing this you can see my code below that doesn't work.

  Button share = (Button) findViewById(R.id.btn_share);
    share.setOnClickListener(new OnClickListener() {   
        public void onClick(View v) {
           // call some other methods before that I guess...
             AlertDialog alertDialog = new AlertDialog.Builder(PasswActivity.this).create(); //Read Update
             alertDialog.setTitle("Uprgade");
             alertDialog.setMessage("Upgrade Text Here");

             alertDialog.setButton("Upgrade", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

             });
                 alertDialog.setButton("Cancel", new DialogInterface.OnClickListener()    {
                public void onClick(DialogInterface dialog, int which) {

             });



             alertDialog.show();  //<-- See This!


    }
    });
Adan
  • 431
  • 1
  • 6
  • 9
Matt
  • 1,747
  • 8
  • 33
  • 58

7 Answers7

91

Adding Buttons

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
   .setCancelable(false)
   .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
            MyActivity.this.finish();
       }
   })
   .setNegativeButton("No", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
       }
   });
AlertDialog alert = builder.create();
alert.show();
OWADVL
  • 10,704
  • 7
  • 55
  • 67
Joe
  • 2,649
  • 21
  • 33
64

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();
}
Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245
rfsk2010
  • 8,571
  • 4
  • 32
  • 46
  • 8
    do we need to declare the buttons as positive and negative? what if they're enither? just 2 different options? or maybe 3? – stanley santoso Aug 30 '15 at 16:07
  • You can add NeutralButton - see https://developer.android.com/guide/topics/ui/dialogs.html#AddingButtons . – Xdg Jan 03 '17 at 07:58
  • 1
    Better use `android.R.string.ok` and `android.R.string.cancel` instead of hard-coded strings "OK" and "Cancel" – Vivek Apr 06 '19 at 08:29
13

This should do the trick for you:

Button share = (Button) findViewById(R.id.btn_share);
share.setOnClickListener(new OnClickListener() {   
  public void onClick(View v) {
    // call some other methods before that I guess...
    AlertDialog alertDialog = new AlertDialog.Builder(PasswActivity.this).create(); //Read Update
    alertDialog.setTitle("Uprgade");
    alertDialog.setMessage("Upgrade Text Here");
    alertDialog.setButton( Dialog.BUTTON_POSITIVE, "Upgrade", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int which) {

       });

    alertDialog.setButton( Dialog.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener()    {
      public void onClick(DialogInterface dialog, int which) {

      });

    alertDialog.show();  //<-- See This!
  }
});
kaspermoerch
  • 16,127
  • 4
  • 44
  • 67
  • Don't you need to call `create()` **after** using the `AlertDialog.builder`'s setter methods to set up the `AlertDialog`? If you don't, I don't think the options set will affect the dialog that's shown. – hotshot309 Dec 30 '11 at 18:28
3
btn_cancle.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        AlertDialog.Builder alert = new AlertDialog.Builder(inflater.getContext());
        alert.setTitle("Do you want to Reject request");
        alert.setIcon(android.R.drawable.ic_dialog_alert);
        alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

                Toast.makeText(inflater.getContext(), "Rejected", Toast.LENGTH_SHORT).show();
            } });


        adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
               // finish();
            } });
        adb.show();
       // Toast.makeText(inflater.getContext(), "Hello", Toast.LENGTH_SHORT).show();
    }
Pang
  • 9,564
  • 146
  • 81
  • 122
Deep Adhia
  • 382
  • 4
  • 11
1
AlertDialog.Builder adb = new AlertDialog.Builder(this);


adb.setView(alertDialogView);


adb.setTitle("Title of alert dialog");


adb.setIcon(android.R.drawable.ic_dialog_alert);


adb.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {


        EditText et = (EditText)alertDialogView.findViewById(R.id.EditText1);


        Toast.makeText(Tutoriel18_Android.this, et.getText(), Toast.LENGTH_SHORT).show();
  } });


adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {

        finish();
  } });
adb.show();
Nicolas
  • 5,249
  • 3
  • 19
  • 20
0

Alert dialog builder have an additional method called setButton2 and setButton3 which can also be used !

rohitsakala
  • 379
  • 3
  • 15
0

Kotlin Solution:

val alertDialog: AlertDialog = AlertDialog.Builder(this@ImageViewerActivity)
                                .create()

                        alertDialog.setTitle("Uprgade");
                        alertDialog.setMessage("Upgrade Text Here");

                        alertDialog.setButton(Dialog.BUTTON_POSITIVE, "Upgrade", DialogInterface.OnClickListener { dialog, which ->


                        });
                        alertDialog.setButton(Dialog.BUTTON_NEGATIVE, "Cancel", DialogInterface.OnClickListener { dialog, which ->


                        });
                        alertDialog.show();
Lins Louis
  • 2,393
  • 2
  • 25
  • 30