How do you disable a button inside of an AlertDialog?
Asked Answered
M

3

14

I am trying to write an AlertDialog with 3 buttons. I want the middle, Neutral Button to be disabled if a certain condition is not met.

Code

int playerint = settings.getPlayerInt();
int monsterint = settings.getMonsterInt();



        AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
        alertbox.setMessage("You have Encountered a Monster");

        alertbox.setPositiveButton("Fight!",
                new DialogInterface.OnClickListener() {

                    // do something when the button is clicked
                    public void onClick(DialogInterface arg0, int arg1) {
                        createMonster();
                        fight();

                    }
                });

        alertbox.setNeutralButton("Try to Outwit",
                new DialogInterface.OnClickListener() {

                    // do something when the button is clicked
                    public void onClick(DialogInterface arg0, int arg1) {
                        // This should not be static
//                      createTrivia();
                        trivia();

                    }
                });

        // Return to Last Saved CheckPoint
        alertbox.setNegativeButton("Run Away!",
                new DialogInterface.OnClickListener() {

                    // do something when the button is clicked
                    public void onClick(DialogInterface arg0, int arg1) {
                        runAway();
                    }
                });

        // show the alert box
        alertbox.show();

// Intellect Check

Button button = ((AlertDialog) alertbox).getButton(AlertDialog.BUTTON_NEUTRAL);

        if(monsterint > playerint) {


            button.setEnabled(false);

        }
    }

The line:

Button button = ((AlertDialog) alertbox).getButton(AlertDialog.BUTTON_NEUTRAL);

Gives error:

Cannot cast from AlertDialog.Builder to AlertDialog

How do I fix this?

Mobster answered 12/9, 2011 at 18:37 Comment(1)
This isn't going to work out well for v2.3.3. In this version, getButton always returns null.Teen
D
24

You can't call getButton() on the AlertDialog.Builder. It has to be called on the resulting AlertDialog after creation. In other words

AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
//...All your code to set up the buttons initially

AlertDialog dialog = alertbox.create();
Button button = dialog.getButton(AlertDialog.BUTTON_NEUTRAL);
if(monsterint > playerint) {
    button.setEnabled(false);
}

The builder is just a class to make constructing the dialog easier...it isn't the actual dialog itself.

HTH

Durrell answered 12/9, 2011 at 18:58 Comment(3)
#7403540Mobster
I tried this approach first on my own, but the return from dialog.getButton() was null, so I got a null pointer exception. I'm not sure this works any more.Homotaxis
You can't call setEnabled unless the dialog is shown.Dissidence
C
12

Better solution in my opinion:

AlertDialog.Builder builder = new AlertDialog.Builder(context); 
builder.setPositiveButton("Positive", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        // some code
    }
});
AlertDialog alertDialog = builder.create();
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                if(**some condition**)
                {
                    Button button = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
                    if (button != null) {
                        button.setEnabled(false);
                    }
                }
            }
        });
Continuity answered 4/11, 2013 at 10:54 Comment(4)
This answer must be outdated. I don't see setOnShowListener in the document for AlertDialog.Builder. (developer.android.com/reference/android/app/…)Engvall
I think I made a mistake setOnShowListener() is available in AlerDialog class itself. I will edit my answerContinuity
alertDialog needs to be final here, so that it will be valid when used in onShow().Homotaxis
This still works with Java 11/API30=Android 11/Android Studio 2022 and is a great solution if the code that creates the dialog at runtime (no .xml file!) + handles the clicks (and in my case EditTexts input) and the code that actually shows the dialog are in different classes!Argenteuil
A
7

The trick is that you need to use the AlertDialog object retuned by AlertDialog.Builder.show() method. No need to call AlertDialog.Builder.create().

Example:

        AlertDialog dialog = alertbox.show();
        if(monsterint > playerint) {
            Button button = dialog.getButton(AlertDialog.BUTTON_NEUTRAL);
            button.setEnabled(false);
        }
Artina answered 17/10, 2014 at 13:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.