Skip to content

Using With Data Binding

Wellington Costa edited this page Apr 26, 2018 · 6 revisions

Step 1 - Enable data binding in your project:

dataBinding {
  enabled true
}

Step 2 - Declare the validation rules in XML layout:

<EditText
  android:id="@+id/name_field"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:hint="@string/name"
  app:requiredValidationErrorMessage="@{@string/field_required}" />

<EditText
  android:id="@+id/nickname_field"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:hint="@string/nickname"
  app:lengthValidationMin="@{3}"
  app:lengthValidationErrorMessage="@{@string/min_3_characters}" />

<EditText
  android:id="@+id/age_field"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:hint="@string/age"
  app:onlyNumberValidationErrorMessage="@{@string/only_numbers}" />

<EditText
  android:id="@+id/phone_field"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:hint="@string/phone"
  app:patternValidationPattern="@{PHONE_PATTERN}"
  app:patternValidationErrorMessage="@{@string/invalid_phone}" />

<EditText
  android:id="@+id/cpf_field"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:hint="@string/cpf"
  app:cpfValidationErrorMessage="@{@string/invalid_cpf}" />

<EditText
  android:id="@+id/initial_period_field"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:hint="@string/initial_period"
  app:betweenValidationStartErrorMessage="@{@string/initial_period_not_valid}"
  app:betweenValidationEndField="@{finalPeriodField}"
  app:betweenValidationEndErrorMessage="@{@string/final_period_not_valid}" />

<EditText
  android:id="@+id/final_period_field"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:hint="@string/final_period" />

<EditText
  android:id="@+id/email_field"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:hint="@string/email"
  app:emailValidationErrorMessage="@{@string/invalid_email}" />

<EditText
  android:id="@+id/confirm_email_field"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:hint="@string/confirm_email"
  app:confirmEmailValidationEmailField="@{emailField}"
  app:confirmEmailValidationErrorMessage="@{@string/emails_not_match}" />

<EditText
  android:id="@+id/password_field"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:hint="@string/password"
  app:passwordValidationMinLength="@{3}"
  app:passwordValidationPattern="@{MIXED_CASE_NUMERIC}"
  app:passwordValidationErrorMessage="@{@string/invalid_password}" />

<EditText
  android:id="@+id/confirm_password_field"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:hint="@string/confirm_password"
  app:confirmPasswordValidationPasswordField="@{passwordField}"
  app:confirmPasswordValidationErrorMessage="@{@string/passwords_not_match}" />

Note: The param emailField passed to app:confirmEmailValidationEmailField is generated by databinding library from EditText with id email_field.

Note: The param finalPeriodField passed to app:betweenValidationEndField is generated by databinding library from EditText with id final_period_field.

Step 3 - Declare the buttons with the validation actions in XML layout:

<Button
  android:id="@+id/validate_button"
  android:text="@string/validateFields"
  app:validationAction="@{@id/validate}" />

<Button
  android:id="@+id/clear_button"
  android:text="@string/clearValidations"
  app:validationAction="@{@id/clear}" />

Note: The use of the app:validationAction="@{@id/clear}" is not mandatory.

Step 4 - Map the methods to handle the validation results:

@OnValidationSuccess
public void onValidationSuccess() {
    Toast.makeText("Yay!", Toast.LENGTH_LONG).show();
}

@OnValidationError
public void onValidationError() {
    Toast.makeText("Something is wrong :(", Toast.LENGTH_LONG).show();
}

Note: Only the method annotated with @OnValidationSuccess is required.

Step 5 - Initialize the generated class:

Note: The generate class is localized at the same package that the source class and its name is the same name of the source class with suffix FieldsValidation.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ActivitySampleBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_sample);
    SampleActivityFieldsValidation.init(this, binding);
}
Clone this wiki locally