Android: disable DialogFragment OK/Cancel buttons
Asked Answered
S

2

15

How can I disable OK/Cancel button of a DialogFragment when it is created using an AlertDialog ? I tried calling myAlertDialogFragment.getDialog() but it's always returning null even once the fragment is displayed

public static class MyAlertDialogFragment extends DialogFragment {

    public static MyAlertDialogFragment newInstance(int title) {
        MyAlertDialogFragment frag = new MyAlertDialogFragment();
        Bundle args = new Bundle();
        args.putInt("title", title);
        frag.setArguments(args);
        return frag;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        int title = getArguments().getInt("title");

        return new AlertDialog.Builder(getActivity())
                .setIcon(R.drawable.alert_dialog_icon)
                .setTitle(title)
                .setPositiveButton(R.string.alert_dialog_ok,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            ((FragmentAlertDialog)getActivity()).doPositiveClick();
                        }
                    }
                )
                .setNegativeButton(R.string.alert_dialog_cancel,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            ((FragmentAlertDialog)getActivity()).doNegativeClick();
                        }
                    }
                )
                .create();
    }
}

I know I can to it by inflating a layout that contains both a cancel and an ok button, but I rather use the AlertDialog solution if possible

Semaphore answered 9/4, 2013 at 20:43 Comment(0)
A
29

Attach your AlertDialog to variable:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
(initialization of your dialog)
AlertDialog alert = builder.create();
alert.show();

And then get button from your AlertDialogand set it disable/enable:

Button buttonNo = alert.getButton(AlertDialog.BUTTON_NEGATIVE);
buttonNo.setEnabled(false);

It give you opportunity to change button properties on runtime.

Then return your alert variable.

AlertDialog must be showed before acquiring its views.

Adela answered 9/4, 2013 at 20:50 Comment(2)
I tried but it's not working because alert.getButton(AlertDialog.BUTTON_NEGATIVE); will return null when called before alert.show() Therefore I don't know where to call it...Semaphore
It does that, yes (and I personally find it really annoying). What you want to do is do your setEnabled() call somewhere later in the lifecycle, possibly after onResume().Wallraff
P
31

You need to override onStart() in DialogFragment and keep a reference to the button. You can then use the reference to re-enable the button later:

Button positiveButton;

@Override
public void onStart() {
    super.onStart();
    AlertDialog d = (AlertDialog) getDialog();
    if (d != null) {
        positiveButton = d.getButton(Dialog.BUTTON_POSITIVE);
        positiveButton.setEnabled(false);
    }

}
Paulenepauletta answered 5/5, 2014 at 20:43 Comment(2)
Great answer! Anyway, you don't have to cast the return of d.getButton to a Button object.Refrigerate
It works. No casting needed: positiveButton = d.getButton(Dialog.BUTTON_POSITIVE); is enough.Calica
A
29

Attach your AlertDialog to variable:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
(initialization of your dialog)
AlertDialog alert = builder.create();
alert.show();

And then get button from your AlertDialogand set it disable/enable:

Button buttonNo = alert.getButton(AlertDialog.BUTTON_NEGATIVE);
buttonNo.setEnabled(false);

It give you opportunity to change button properties on runtime.

Then return your alert variable.

AlertDialog must be showed before acquiring its views.

Adela answered 9/4, 2013 at 20:50 Comment(2)
I tried but it's not working because alert.getButton(AlertDialog.BUTTON_NEGATIVE); will return null when called before alert.show() Therefore I don't know where to call it...Semaphore
It does that, yes (and I personally find it really annoying). What you want to do is do your setEnabled() call somewhere later in the lifecycle, possibly after onResume().Wallraff

© 2022 - 2024 — McMap. All rights reserved.