Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: disable DialogFragment OK/Cancel buttons

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

like image 657
user1026605 Avatar asked Apr 09 '13 20:04

user1026605


People also ask

How do I cancel DialogFragment?

tl;dr: The correct way to close a DialogFragment is to use dismiss() directly on the DialogFragment.

Can you have an alert dialog without any buttons if not why?

You can do this very easily. AlertDialog. Builder alertDialogBuilder = new AlertDialog. Builder(context); // set title alertDialogBuilder.

What is DialogFragment in Android?

DialogFragment is a utility class which extends the Fragment class. It is a part of the v4 support library and is used to display an overlay modal window within an activity that floats on top of the rest of the content. Essentially a DialogFragment displays a Dialog but inside a Fragment.

How do you dismiss dialog with click on outside of dialog?

You can use dialog. setCanceledOnTouchOutside(true); which will close the dialog if you touch outside of the dialog. Window window = this.


2 Answers

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.

like image 122
ArturSkowronski Avatar answered Sep 19 '22 16:09

ArturSkowronski


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

}
like image 30
agirardello Avatar answered Sep 22 '22 16:09

agirardello