3

I want to display a dialog box on a button click. Here is my code, but it is not working.

AlertDialog.Builder builder = new AlertDialog.Builder(
                        getApplicationContext());
                builder.setCancelable(true);
                builder.setTitle("Title");
                builder.setInverseBackgroundForced(true);
                builder.setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                dialog.dismiss();
                            }
                        });
                builder.setNegativeButton("No",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                dialog.dismiss();
                            }
                        });
                AlertDialog alert = builder.create();
                alert.show();
Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
Krishna
  • 1,454
  • 5
  • 16
  • 31
  • 12
    What is the problem/error-message? "Not working" is not a problem-description ;) – dst May 16 '11 at 07:45
  • 4
    Where this code is placed? In onCreate()? Then it is wrong. Dialog showing should be called when activity has been shown, so put this code in some button onClickListener. – Vladimir Ivanov May 16 '11 at 07:55
  • if you are using alert dialog then it is not need to dismiss for alert dialog. when you will click on button of alert dialog it will be automatically dismiss.. – Niranj Patel May 16 '11 at 07:59
  • Problem solved. I placed the code in onCreate() method. I corrected it and placed it in a button onClick() method, its working perfectly. Thanks Mr. Vladimir Ivanov. – Krishna May 16 '11 at 11:29
  • I can use this very code to ask my query..what does this positiveButton, negative and neutral button? – Ayush Goyal Jun 09 '12 at 10:57
  • Check this link is usefull to you http://stackoverflow.com/questions/3965122/android-how-to-align-message-in-alertdialog/22558128#22558128 – SAndroidD Mar 21 '14 at 12:09

4 Answers4

7

try this,

instead of passing getApplicationContext() pass this;

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

try this:

AlertDialog.Builder builder = new AlertDialog.Builder(YourActivityName.this);
Faruk Toptas
  • 1,257
  • 14
  • 21
0

This is a very old post however I have made a class that I think it can help to keep the code clean

https://github.com/houmanka/DialogMaker

You dont need to worry about the OnClickListener all have been taking care of. Just override the methods and you are done. Also I put the support for Standard and Neutral too.

Cheers

Hope it helps someone. H.

Mr H
  • 5,254
  • 3
  • 38
  • 43
-1

try this

AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
t0mm13b
  • 34,087
  • 8
  • 78
  • 110
avulosunda
  • 137
  • 2
  • 14