Ameya Joshi
Ameya Joshi

Reputation: 31

not showing alertdialog box

    DialogInterface.OnClickListener clickListener= new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which)
                {
                    case BUTTON_POSITIVE :
                        udb.signout();
                        break;

                    case BUTTON_NEGATIVE:
                        finish();
                        break;
                }
            }
        };

        AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
        builder.setTitle("Notification");
        builder.setMessage("You are already logged in.\nDo you want to signout and login with different account?");
        builder.setPositiveButton("Yes",clickListener);
        builder.setNegativeButton("No",clickListener);
        builder.show();

this my code for showing pop up dialog box.. but I'm getting problem on "builder.show()" line. And I can't understand what i did wrong. Please. I'll appreciate any help

Upvotes: 1

Views: 3019

Answers (4)

Iamat8
Iamat8

Reputation: 3906

You have to create dialog of your AlertDialog.Builder and then show it...remove builder.show();

AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
builder.setTitle("Notification");
builder.setMessage("You are already logged in.\nDo you want to signout and login with different account?");
builder.setPositiveButton("Yes",clickListener);
builder.setNegativeButton("No",clickListener);   


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

Upvotes: 1

bashizip
bashizip

Reputation: 581

First call builder.create() . You cannot display the builder itself.

Upvotes: 0

solosodium
solosodium

Reputation: 733

First of all, as I tested personally on my device,

builder.show();

should has the same effect as

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

no matter android.support.v7.app.AlertDialog or android.app.AlertDialog is used.

In my case, I found what is causing the problem is the way AlertDialog.Builder is initialized.

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

From Dialog documentation, you need to pass in an Activity to this constructor, which the followings will work:

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

or,

AlertDialog.Builder builder = new AlertDialog.Builder(<YourActivity>.this);

Upvotes: 3

Rutvik
Rutvik

Reputation: 137

You need to create AlertDialog object using the AlertDialog.Builder object and show dialog. For e.g.,

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

/*All your dialog code*/

//Create AlertDialog object
AlertDialog alertDialog = builder.create ();
//show the AlertDialog using show() method
alertDialog.show();

Upvotes: 0

Related Questions