How to Disable/Enable Dialog Negative Positive Buttons

How to disable / enable dialog negative positive buttons?

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

Disable (positive) button of AlertDialog by Default

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

Disable positive/negative button in DialogFragment

You can enable or disable the Button after the view of the FragmentDialog has been created. So you have to call it in the onStart() method of your Dialog.

See my code:

public class DChooseSeparator extends DialogFragment
{
// MEMBER
private AlertDialog dialog;
private static boolean mEnableButton;

// You need an empty constructor: "All subclasses of Fragment must include a public empty constructor. "
// like it's described in the Fragment API -> so create a new Insatnce with this static methjod
public static DChooseSeparator newInstance(boolean enableButton){
mEnableButton = enableButton;
return new DChooseSeparator();
}
// ...
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();

builder
.setTitle("My Title")
.setView(myDialogLayout)
.setPositiveButton(getString(R.string.sOKButton), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if(myEditText.getText().toString().equals("")) // disable positive button if this is empty
{
Toast.makeText(getActivity(), "enter something!", Toast.LENGTH_SHORT).show();
}
else { myListener.onSet(myEditText.getText().toString()); }
}
})
.setNegativeButton(getString(R.string.sCancelButton), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// do nothing
}
});

dialog = builder.create()

return dialog;
}

@Override
public void onStart(){
super.onStart();
dialog.getButton(Dialog.BUTTON_POSITIVE).setEnabled(mEnableButton);
}
}

Now you can call your Dialog like this:

sepButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
MyDialog myDialog = new MyDialog(false);
myDialog.show(getFragmentManager(), "tMyDialogTag");
}
}

Android How to disable a positive button after click

(Dialog.class.cast(arg0)).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);

How to disable positive and negative button if the checkbox is unchecked in java?

This is answered already here--How to disable / enable dialog negative positive buttons?

after dialog.show use below code

if(your_condition_true)
dialog.getButton(AlertDialog.BUTTON1).setEnabled(false); //BUTTON1 is positive button

How to enable/disable dialog buttons (actions)

You can pass null on onPressed to disable the button state. While it is not clear from where you like to controll the state, you can use ValueNotifier, and it work for all widget

  final ValueNotifier<bool> enableButton = ValueNotifier(false);
Future<void> showContentDialog(
BuildContext context,
) async {
await showDialog(
context: context,
barrierDismissible: true,
builder: (context) => AlertDialog(
content: SizedBox(
width: 222,
child: Column(
children: [
Text("A"),
ElevatedButton(
onPressed: () {
enableButton.value = !enableButton.value;
},
child: Text("toggleButtonState"),
)
],
),
),
actions: [
ValueListenableBuilder<bool>(
valueListenable: enableButton,
builder: (context, value, child) => ElevatedButton(
onPressed: value ? () {} : null,
child: Text("BTN"),
),
),
],
),
);
}

Android disable positive button on some condion in addTextChangedListener (editext)

Try something like this:

@Override
public void afterTextChanged(Editable s) {
if (s.length() > 5) {
dialog.getButton(Dialog.BUTTON_POSITIVE).setEnabled(false);
} else {
dialog.getButton(Dialog.BUTTON_POSITIVE).setEnabled(true);
}
}

where dialog is:

final AlertDialog dialog = builder.create();


Related Topics



Leave a reply



Submit