Skip to content

CustomViewDialog

Philipp Niedermayer edited this page Sep 24, 2021 · 13 revisions

Dialogs with custom views

extends SimpleDialog, Full API reference

Extend this class to create a dialog with a custom view.

General

It is strongly recommended to create a TAG field which will be used as a default tag for receiving results:

public static final String TAG = "YourTag";

If you want to use the builder, make sure to overwrite the .build() function (it might come handy for setting some presets):

public static MyDialog build() {
    return new MyDialog()
            .title(R.string.myDialogTitle)
            .pos(R.string.myPositiveButtonText)
            .neut(R.string.myNeutralButtonText);
}

Custom attributes

To make sure custom attributes are persistent across rotation changes, use the setArg(...) methods provided to set the value, and retrieve the values via one of getArgString(...), getArgs().get(...) later on (see below).

protected static final String CHECKBOX_LABEL = "simpleCheckDialog.check_label";

public MyDialog label(String checkBoxLabel){
    return setArg(CHECKBOX_LABEL, checkBoxLabel);
}

Inflate view

Inflate, setup and return your custom view in onCreateContentView.
For inflating, use one of the provided inflate(...) functions to make sure custom theme attributes are applied correctly.

@Override
public View onCreateContentView(Bundle savedInstanceState){

    // inflate and set your custom view here
    View view = inflate(R.layout.my_custom_view_layout);
    
    // Some example code
    mCheckBox = (CheckBox) view.findViewById(R.id.checkBox);
    if (savedInstanceState != null){
        mCheckBox.setChecked(savedInstanceState.getBoolean(CHECKED, false));
    } else {
        mCheckBox.setChecked(getArgs().getBoolean(CHECKED, false));
    }
    mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            //...
        }
    });

    return view;
}

Providing results

Overwrite this method to return a Bundle that will be merged into the results returned by OnDialogResultListener.onResult.

@Override
public Bundle onResult(int which) {
    Bundle result = new Bundle();
    result.putBoolean(CHECKED, mCheckBox.isChecked());
    return result;
})

Modifying buttons behaviour

These methods can be used to modify button behaviour:

/**
 * Overwrite this method to catch positive button presses,
 * e.g. if you need to verify input by the user
 */
protected boolean acceptsPositiveButtonPress()

/**
 * Call these methods to enable or disable the respective buttons
 */     
protected void setPositiveButtonEnabled(boolean enabled)
protected void setNegativeButtonEnabled(boolean enabled)
protected void setNeutralButtonEnabled(boolean enabled)

/*
 * Simulates a positive button press e.g. due to an IME action.
 */
protected void pressPositiveButton()

For full control, you can use the onDialogShown() method. Overwrite this method to take action once the dialog is shown such as settings the initial state of the positive button (see below), an input focus, or customizing the click listeners on the buttons. You can retrieve the buttons from here using getPositiveButton(), getNegativeButton() and getNeutralButton(). Please note that these methods will return null until the dialog was shown.

@Override
protected void onDialogShown() {

    setPositiveButtonEnabled(false);

    Button button = getNeutralButton();

    if (button != null) {
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // neutral button pressed, do something custom or just dismiss the dialog and call the result listener as default
                getDialog().dismiss();
                callResultListener(DialogInterface.BUTTON_NEUTRAL, null);
            }
        });
    }
}

Examples

For exemplary use have a look at the following dialogs (which extend CustomViewDialog):

Clone this wiki locally