66 Votes

Android Programming: YES/NO Dialog Box

Tip by Progger99 | Last update on 2021-04-02 | Created on 2013-05-11

In this tip, I would like to show you how to display a dialog in Android which contains an arbitrary question as well as a YES and a NO button and how to assign actions to these buttons.

AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setTitle("Title");
builder.setMessage("Is this a question?");


builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {

   @Override
   public void onClick(DialogInterface dialog, int which) {
        
        // Code that is executed when clicking YES

        dialog.dismiss();
   }

});


builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {

   @Override
   public void onClick(DialogInterface dialog, int which) {

        // Code that is executed when clicking NO

        dialog.dismiss();
   }

});


AlertDialog alert = builder.create();
alert.show();

We are using AlterDialog.Builder for this. First of all, we are setting the title and the text (question) that should be shown in the dialog. After that, we are defining the actions for the YES and the NO button.

Of course, at this point you can write any code you want, for example you can call a function.

ReplyPositiveNegative

About the Author

AvatarThe author has not added a profile short description yet.
Show Profile

 

Related Topics

Android Splash Screen Tutorial

Tutorial | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.