-
-
Notifications
You must be signed in to change notification settings - Fork 18
CustomViewDialog
extends SimpleDialog, Full API reference
Extend this class to create a dialog with a custom view.
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);
}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, 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;
}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;
})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);
}
});
}
}For exemplary use have a look at the following dialogs (which extend CustomViewDialog):
Javadoc API
Screenshot gallery
Styling dialogs with themes
Fullscreen dialogs
SimpleDialog
CustomViewDialog
CustomListDialog
SimpleCheckDialog
SimpleColorDialog
SimpleColorWheelDialog
SimpleDateDialog
SimpleEMailDialog
SimpleFormDialog
SimpleImageDialog
SimpleInputDialog
SimpleListDialog
SimplePinDialog
SimpleProgressDialog
SimpleTimeDialog