Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alert Dialog Two Buttons

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!       }     }); 
like image 627
Matt Avatar asked Nov 22 '11 13:11

Matt


People also ask

How many buttons can an alert dialog display?

AlertDialog. A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout.

What are the three regions of an alert dialog?

AlertDialog in Android application comprises three regions: Title, Content area (Message), Action Button.

What is the difference between an alert and an alert dialog?

AlertDialog is a lightweight version of a Dialog. This is supposed to deal with INFORMATIVE matters only, That's the reason why complex interactions with the user are limited. Dialog on the other hand is able to do even more complex things .

What is alert dialog box and example?

Android AlertDialog can be used to display the dialog message with OK and Cancel buttons. It can be used to interrupt and ask the user about his/her choice to continue or discontinue.


1 Answers

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(); 
like image 113
Joe Avatar answered Oct 07 '22 12:10

Joe