user1879118
user1879118

Reputation: 71

Android AlertDialog how to disable buttons before showing?

How to disable buttons before showing alert dialog, like it is done on Fatal dialog "Android error: The application has stopped unexpectedly please try again".

I use such example:

  @Override
  protected Dialog onCreateDialog(int id) {
    if (id == DIALOG) {
      Log.d(LOG_TAG, "Create");
      AlertDialog.Builder adb = new AlertDialog.Builder(this);
      adb.setTitle("Title");
      adb.setMessage("Message");
      adb.setPositiveButton("OK", null);
      dialog = adb.create();

      dialog.setOnShowListener(new OnShowListener() {
        public void onShow(DialogInterface dialog) {
          Log.d(LOG_TAG, "Show");
        }
      });

      dialog.setOnCancelListener(new OnCancelListener() {
        public void onCancel(DialogInterface dialog) {
          Log.d(LOG_TAG, "Cancel");
        }
      });

      dialog.setOnDismissListener(new OnDismissListener() {
        public void onDismiss(DialogInterface dialog) {
          Log.d(LOG_TAG, "Dismiss");
        }
      });
      return dialog;
    }
    return super.onCreateDialog(id);
  }

  public void onclick(View v) {
    showDialog(DIALOG);
  }

If I enable button on dialog.setOnShowListener then users get possibility to click twice on OK button.

Upvotes: 0

Views: 1219

Answers (2)

QuokMoon
QuokMoon

Reputation: 4425

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);
}

use getButton to enable and disable

Upvotes: 2

MysticMagicϡ
MysticMagicϡ

Reputation: 28823

I think you should disable it by default. and use onShowListener() as below:

dlg.setOnShowListener(new OnShowListener() {

            @Override
            public void onShow(DialogInterface dialog) {
                // TODO Auto-generated method stub
                //Enable buttons..
            }
        });

Upvotes: 0

Related Questions