Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dialog Box example in android

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();
like image 418
Krishna Avatar asked Dec 17 '25 16:12

Krishna


2 Answers

try this,

instead of passing getApplicationContext() pass this;

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

try this:

AlertDialog.Builder builder = new AlertDialog.Builder(YourActivityName.this);
like image 27
Faruk Toptas Avatar answered Dec 20 '25 06:12

Faruk Toptas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!