Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable positive/negative button in DialogFragment

I mimicked what I thought was fairly standard Dialog code:

public class DChooseSeparator extends DialogFragment
{
    // ...
    @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
                }
            });

        return builder.create();
    }
}

And in onStart of the Fragment that shows it:

sepButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        MyDialog myDialog = new MyDialog();
        myDialog.show(getFragmentManager(), "tMyDialogTag");
        myDialog.getButton(Dialog.BUTTON_POSITIVE).setEnabled(false);   // DOES NOT WORK
    }
}

However, this does not work, as the getButton function is not available for my DialogFragment. I can also not do this in the DialogFragment class, as I need to show() it first.

So...where exactly can/should I disable the Button? Do I really have to move the whole creation of the Dialog to the onClick method?

Thank you in advance for your help.

like image 936
Markstar Avatar asked Jan 30 '14 22:01

Markstar


1 Answers

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");
    }
}
like image 148
owe Avatar answered Sep 25 '22 17:09

owe