Skip to content

CustomViewDialog

Eltos edited this page Oct 29, 2021 · 13 revisions

Dialogs with custom views

extends SimpleDialog

API reference

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

Custom implementation

public class MyCustomDialog extends CustomViewDialog<MyCustomDialog> {

    public static final String TAG = "MyCustomDialog.";
    
    public static MyCustomDialog build(){
        return new MyCustomDialog()
                    .pos(R.string.customDefaultText); // can be useful to provide presets here
    }
    
    // ...
}

It is strongly recommended to create a TAG field which will be used as a default tag for receiving results. Also if you want to use the builder, make sure to overwrite the static .build() function.

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(...) or getArgs().get(...) later on:

    protected static final String
            CHECKBOX_LABEL = TAG + "check_label";

    public MyCustomDialog label(String checkBoxLabel){
        return setArg(CHECKBOX_LABEL, checkBoxLabel);
    }
    
    // ...

Inflate view

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

    public static final String
            CHECKED = TAG + "CHECKED";

    private CheckBox mCheckBox;
    
    @Override
    public View onCreateContentView(Bundle savedInstanceState){

        // inflate and set your custom view here
        View view = inflate(R.layout.my_custom_view_layout);
        
        // setup the layout
        mCheckBox = (CheckBox) view.findViewById(R.id.checkBox);
        mCheckBox.setText(getArgString(CHECKBOX_LABEL));
        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;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBoolean(CHECKED, mCheckBox.isChecked());
    }

Providing results

Overwrite the onResult method to return a Bundle that will be merged into the results extras passed to OnDialogResultListener.onResult.

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

Modifying buttons behaviour

The following methods can be used to modify button behaviour:

For further customizations overwrite the onDialogShown method. This can be used to e.g. set the initial state of the buttons, 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 here
                    getDialog().dismiss();
                    callResultListener(DialogInterface.BUTTON_NEUTRAL, null);
                }
            });
        }
    }

Examples

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