How to disable / enable dialog negative positive buttons?

Solution 1:

Edit for complete solution...

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(android.R.drawable.ic_dialog_info);
builder.setTitle("Alert dialog title");
builder.setMessage("This is the example code snippet to disable button if edittext attached to dialog is empty.");
builder.setPositiveButton("PositiveButton",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
                // DO TASK
            }
        });
builder.setNegativeButton("NegativeButton",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
                // DO TASK
            }
        });

// Set `EditText` to `dialog`. You can add `EditText` from `xml` too.
final EditText input = new EditText(MainActivity.this);

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.MATCH_PARENT
);
input.setLayoutParams(lp);


builder.setView(input);

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

// Initially disable the button
((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);

// OR you can use here setOnShowListener to disable button at first time.

// Now set the textchange listener for edittext
input.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
    }

    @Override
    public void afterTextChanged(Editable s) {

        // Check if edittext is empty
        if (TextUtils.isEmpty(s)) {
            // Disable ok button
            ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);

        } else {
            // Something into edit text. Enable the button.
            ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
        }

    }
});

Below are edited history, which can be refer as some more details

Here is a sample code, try this

AlertDialog.Builder builder = new AlertDialog.Builder(AddSchedule.this);
builder.setIcon(android.R.drawable.ic_dialog_info);
builder.setTitle("Alert dialog title");
builder.setMessage("Dialog message");
builder.setPositiveButton("Button1", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface arg0, int arg1) {
        //DO TASK
    }
});
builder.setNegativeButton("Button2", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface arg0, int arg1) {
        //DO TASK
    }
});

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

// After calling show method, you need to check your condition and enable/disable the dialog buttons 
if (your_condition_true) {
    // BUTTON1 is the positive button
    dialog.getButton(AlertDialog.BUTTON1).setEnabled(false);
}

For negative button

dialog.getButton(AlertDialog.BUTTON2).setEnabled(false); //BUTTON2 is negative button

For buttons id : Reference alert_dialog.xml

Edited :

And the setOnShowListener since level 8 API (FroYo), does the same,

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setPositiveButton(android.R.string.ok, null);

AlertDialog dialog = builder.create();
dialog.setOnShowListener(new OnShowListener() {

    @Override
    public void onShow(DialogInterface dialog) {
        if (condition) {
            ((AlertDialog)dialog).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
        }
    }
});

dialog.show();

Edited

new AlertDialog.Builder(this)
    .setMessage("This may take a while")
    .setPositiveButton("OK", new android.content.DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            ((AlertDialog)dialog).getButton(which).setVisibility(View.INVISIBLE);
            // the rest of your stuff
        }

    }).show();

Solution 2:

None of these answers really solve the problem.

I accomplish this using a custom layout with an EditText in it and a TextWatcher on that view.

final LinearLayout layout = (LinearLayout) inflator.inflate(R.layout.text_dialog, null);
final EditText text = (EditText) layout.findViewById(R.id.text_edit);
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(layout);
// Now add the buttons...
builder.setPositiveButton(R.string.ok, new AlertDialog.OnClickListener() {
    // Left out for brevity...
}
builder.setNegativeButton(R.string.cancel, new AlertDialog.OnClickListener() {
    // Left out for brevity...
}

// Create the dialog
final AlertDialog d = builder.create();

// Now add a TextWatcher that will handle enable/disable of save button
text.addTextChangedListener(new TextWatcher() {
    private void handleText() {
        // Grab the button
        final Button okButton = d.getButton(AlertDialog.BUTTON_POSITIVE);
        if(text.getText().length() == 0) {
            okButton.setEnabled(false);
        } else {
            okButton.setEnabled(true);
        }
    }
    @Override
    public void afterTextChanged(Editable arg0) {
        handleText();
    }
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        // Nothing to do
    }
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
       // Nothing to do
    }
});

// show the dialog
d.show();
// and disable the button to start with
d.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);