We will extend Android's TextInputLayout and explain how to implement ValidationTextInputLayout with validation function. ValidationTextInputLayout allows you to specify required, validation items, and error messages from xml by adding custom attributes.
In the following, the input field of the email address is required, and it is checked whether the input content is in the email format. If it is not the content of the email, the character string specified by error_text is displayed.
<ValidationTextInputLayout
app:required="true"
app:validation_type="email"
app:error_text="Email value is invalidated"
>
<EditText
android:hint="Email (Required)"
/>
</ValidationTextInputLayout>
Define custom attributes in res / values / attrs.xml. ValidationTextInputLayout, which will be defined later, receives attributes and changes the behavior.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ValidationTextInputLayout">
<attr name="required" format="boolean" />
<attr name="validation_type">
<enum name="post_code" value="0" />
<enum name="email" value="1" />
</attr>
<attr name="error_text" format="string" />
</declare-styleable>
</resources>
Define ValidationType as an enum. This process is not required, but it improves the visibility of the source code. The value of the enum and the value defined in attrs.xml must match.
public enum ValidationType {
PostCode(0), Email(1),
Null(9999);
private int value;
ValidationType(int value) {
this.value = value;
}
public int getValue() {
return this.value;
}
public static ValidationType valueOf(final int value) {
ValidationType type = Null;
for (ValidationType validationType : ValidationType.values()) {
if (validationType.getValue() == value) {
type = validationType;
break;
}
}
return type;
}
}
Gets the attribute value specified by initAttrs (). updateError () validates and outputs an error message if necessary. From the outside, you can determine if validation has passed by calling isValidated ().
public class ValidationTextInputLayout extends TextInputLayout {
private static final String PATTERN_POST_CODE = "\\d{7}";
private boolean isRequired = false;
private ValidationType validationType = ValidationType.Null;
private String errorText;
public ValidationTextInputLayout(Context context) {
super(context);
}
public ValidationTextInputLayout(Context context, AttributeSet attrs) {
super(context, attrs);
initAttrs(context, attrs);
}
public ValidationTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initAttrs(context, attrs);
}
private void initAttrs(final Context context, AttributeSet attrs) {
final TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ValidationTextInputLayout);
isRequired = typedArray.getBoolean(R.styleable.ValidationTextInputLayout_required, false);
final int validationTypeValue = typedArray.getInt(
R.styleable.ValidationTextInputLayout_validation_type, ValidationType.Null.getValue());
validationType = ValidationType.valueOf(validationTypeValue);
String errorText = typedArray.getString(R.styleable.ValidationTextInputLayout_error_text);
if (TextUtils.isEmpty(errorText)) {
errorText = getContext().getString(R.string.error_default_text);
}
this.errorText = errorText;
}
public boolean isValidated() {
updateError();
final boolean isValidated = TextUtils.isEmpty(getError());
setErrorEnabled(!isValidated);
return isValidated;
}
private void updateError() {
final String text = getEditText().getText().toString();
final boolean isEmpty = TextUtils.isEmpty(text);
setError(null);
switch (validationType) {
case PostCode:
if (!isPostCode(text)) {
setError(errorText);
}
break;
case Email:
if (!isEmail(text)) {
setError(errorText);
}
break;
default:
break;
}
if (isEmpty) {
if (isRequired) {
setError("Fill in this form");
} else {
setError(null);
}
}
}
private boolean isPostCode(final String str) {
return Pattern.compile(PATTERN_POST_CODE).matcher(str).matches();
}
private boolean isEmail(final String str) {
return Patterns.EMAIL_ADDRESS.matcher(str).matches();
}
}
I have a project that works on Validation-Text-Input-Layout @ github.
Similar processing is implemented in iOS. Please see Text-Input-Layout @ github.
Recommended Posts