
Validation is a process to ensure that the value entered by the user is within the accepted boundaries of the application. For example if a user enters a name then the programmer validates the edit text in such a way that it consists of only letters but no numeric values.
Step1: Add Data Binidng in Depaency
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
apply plugin: 'com.android.application' android { compileSdkVersion 28 defaultConfig { applicationId "com.androindian.validation" minSdkVersion 15 targetSdkVersion 28 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } dataBinding.enabled=true } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' } |
Step2: Create xml file
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <EditText android:id="@+id/name" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="textPersonName" android:hint="Name" /> <EditText android:id="@+id/mobile" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="phone" android:hint="mobile"/> <EditText android:id="@+id/mail" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:hint="Mail" android:inputType="textEmailAddress" /> <EditText android:id="@+id/age" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="number" android:hint="age"/> <EditText android:id="@+id/add" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:hint="Address" android:inputType="textPostalAddress" /> <EditText android:id="@+id/Pass" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:hint="Pass" android:inputType="textPassword" /> <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button" /> <CheckBox android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/checkbox" android:text="test"/> </LinearLayout> </layout> |
Step3: Created Java file
MainActiity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
package com.androindian.validation; import android.databinding.DataBindingUtil; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.TextUtils; import android.util.Patterns; import android.view.View; import com.androindian.validation.databinding.ActivityMainBinding; public class MainActivity extends AppCompatActivity { ActivityMainBinding binding; String MobilePattern = "[6-9]{10}"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); binding= DataBindingUtil.setContentView(MainActivity.this, R.layout.activity_main); binding.button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(TextUtils.isEmpty(binding.name.getText())){ binding.name.setError("Name is not empty"); return; } if(TextUtils.isEmpty(binding.mobile.getText())){ binding.mobile.setError("Mobile number not empty"); return; } if(binding.mobile.getText().toString().length()!=10){ binding.mobile.setError("valid number"); return; } if(binding.mobile.getText().toString()==MobilePattern) if(TextUtils.isEmpty(binding.mail.getText())){ binding.mail.setError("Mail is not empty"); } if (!Patterns.EMAIL_ADDRESS.matcher(binding.mail.getText().toString()).matches()){ binding.mail.setError("Please enter valid "+binding.mail.getHint()); return; } if(TextUtils.isEmpty(binding.age.getText())){ binding.age.setError("Age is not empty"); return; } if(TextUtils.isEmpty(binding.add.getText())){ binding.add.setError("Address is not empty"); return; } if(TextUtils.isEmpty(binding.Pass.getText())){ binding.Pass.setError("Password is not empty"); return; } if(binding.checkbox.isChecked()==false){ binding.checkbox.setError("please select"); } } }); } } |
output: