6 6 Votes

Android Programming: YES/NO Dialog Box

Tip by Progger99 | 11/05/2013 at 00:18

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

Avatar AuthorThe author has not added a short description to his profile yet.
Show Profile | Message

 

Related Topics

Android Getting Sound Levels
Open Question | 1 Answer
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 articles on askingbox.com. How to do.