13 Validating preferences - michael-rapp/AndroidMaterialPreferences GitHub Wiki

Some preferences, such as the EditTextPreference, can be validated and therefore are extended from the class AbstractValidateableDialogPreference. The validation is performed according to so called validators, which can be added to the preference by using the following methods. If the validation fails, an error message will be prompted and the preference's dialog will refuse to be closed in order to ensure, that no invalid values can be stored.

addValidator(Validator):void
addAllValidators(Collection<Validator>):void
addAllValidators(Validator[]):void

Additionally, the following methods, which can be used to remove a previously added validator from the preference afterwards, are provided:

removeValidator(Validator):void
removeAllValidators(Collection<Validator>):void
removeAllValidators(Validator[]):void
removeAllValidators():void

As the library "AndroidMaterialValidation" is used for the validation, please refer to the documentation of this library for further information about how the validation works.

Furthermore, preferences, which are extended from the class AbstractValidateableDialogPreference provide a few additional getter and setter methods that can be used to customize the validation. These properties can also be specified by using XML attributes and will be discussed in the following sections.

11.1 Getter and setter methods

The following getter and setter methods are provided in order to programmatically adapt the validation of preferences, which are extended from the class AbstractValidateableDialogPreference. The methods of the base class DialogPreference can be used as well.

Getter and setter methods Description Default value Restrictions
getHelperText():int setHelperText(CharSequence):void setHelperText(int):void Returns or sets the helper text of the preference. The helper text is a text, which is shown below the view, which is contained by the preference's dialog. null When the helper text is specified by passing a resource ID, the resource ID must correspond to a valid string resource.
getHelperTextColor():int setHelperTextColor(int):void Returns or sets the text color of the preference's helper text. ?attr.textColorPrimary none
getErrorColor():int setErrorColor(int):void Returns or sets the text color of the message, which is shown when a validation has failed. R.color.default_error_color none
isValidatedOnValueChange():boolean validateOnValueChange(boolean):void Returns or sets, whether the preference's value should be automatically validated when it has been changed, for example while typing, or not. true none
isValidatedOnFocusLost():int validateOnFocusLost(boolean):void Returns or sets, whether the preference's value should be automatically validated, when the view, which is contained by the preference's dialog has lost its focus. This attribute is only relevant, if the dialog contains more than one view. true none

11.2 XML attributes

As an alternative to the setter methods, which have been shown in the previous section, various XML attributes can be used. The following attributes are available.

XML attribute Description Format
custom:helperText The helper text of the preference. string
custom:helperTextColor The text color of the helper text. color
custom:errorColor The text color of the message, which is shown when a validation has failed. color
custom:validateOnValueChange Whether the preference's value should be validated automatically, when it has changed, for example while typing, or not. boolean
custom:validateOnFocusLost Whether the preference's value should be validated automatically, when it has lost its focus, or not. boolean
⚠️ **GitHub.com Fallback** ⚠️