How to Validate the Edit Text in Android Studio

Introduction

 
In this article, you will learn how to validate edit text in Android.
 
Validation
 
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.
 
Step 1
 
Create a new project.
 
9.jpg
 
Step 2
 
Now open the XML file:
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.    xmlns:tools="http://schemas.android.com/tools"  
  3.    android:layout_width="match_parent"  
  4.    android:layout_height="match_parent"  
  5.    android:paddingLeft="@dimen/activity_horizontal_margin"  
  6.    android:paddingRight="@dimen/activity_horizontal_margin"  
  7.    android:paddingTop="@dimen/activity_vertical_margin"  
  8.    android:paddingBottom="@dimen/activity_vertical_margin"  
  9.    tools:context=".MainActivity">  
  10.    <LinearLayout  
  11.       android:layout_width="wrap_content"  
  12.       android:layout_height="wrap_content"  
  13.       android:layout_alignParentTop="true"  
  14.       android:orientation="horizontal" android:id="@+id/linearLayout2">  
  15.       <TextView  
  16.          android:layout_width="wrap_content"  
  17.          android:layout_height="wrap_content"  
  18.          android:textAppearance="?android:attr/textAppearanceLarge"  
  19.          android:text="Person_Name"  
  20.          android:id="@+id/textView1"  
  21.          android:layout_gravity="center_horizontal|top"  
  22.          android:layout_alignParentTop="true"  
  23.          android:layout_toRightOf="@+id/linearLayout2"/>  
  24.       <EditText  
  25.          android:layout_width="200dp"  
  26.          android:layout_height="wrap_content"  
  27.          android:inputType="textPersonName"  
  28.          android:text=""  
  29.          android:id="@+id/editText1"  
  30.          android:layout_gravity="center_horizontal|top"  
  31.          android:layout_alignTop="@+id/linearLayout2"  
  32.          android:layout_alignParentRight="true"/>  
  33.    </LinearLayout>  
  34.    <LinearLayout  
  35.       android:layout_width="wrap_content"  
  36.       android:layout_height="wrap_content"  
  37.       android:layout_marginTop="80dp"  
  38.       android:orientation="horizontal" android:id="@+id/linearLayout">  
  39.       <TextView  
  40.          android:layout_width="wrap_content"  
  41.          android:layout_height="wrap_content"  
  42.          android:textAppearance="?android:attr/textAppearanceLarge"  
  43.          android:text="word"  
  44.          android:id="@+id/textView"  
  45.          android:layout_gravity="center_horizontal|top"  
  46.          android:layout_alignParentTop="true"  
  47.          android:layout_toRightOf="@+id/linearLayout2"/>  
  48.       <EditText  
  49.          android:layout_width="200dp"  
  50.          android:layout_height="wrap_content"  
  51.          android:inputType=""  
  52.          android:text=""  
  53.          android:id="@+id/editText2"  
  54.          android:layout_marginLeft="40dp" android:layout_gravity="center_horizontal|top"  
  55.          android:layout_alignTop="@+id/linearLayout2"  
  56.          android:layout_alignParentRight="true"/>  
  57.    </LinearLayout>  
  58.    <Button  
  59.       android:layout_width="wrap_content"  
  60.       android:layout_height="wrap_content"  
  61.       android:text="Register"  
  62.       android:id="@+id/button"  
  63.       android:layout_below="@+id/linearLayout"  
  64.       android:layout_centerHorizontal="true"  
  65.       android:layout_marginTop="31dp"/>  
  66.    >  
  67. </RelativeLayout> 
Step 3
  1. package com.edittextvalidation;  
  2. import android.os.Bundle;  
  3. import android.app.Activity;  
  4. import android.view.Menu;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  7. import android.widget.EditText;  
  8. import android.widget.TextView;  
  9. import android.widget.Toast;  
  10. public class MainActivity extends Activity {  
  11. EditText NameEditText,editTextPhoneNumber,wordEditText;  
  12. TextView textViewName,textViewAge,textViewword;  
  13. Button RegistrationButton;   
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.activity_main);  
  18.         RegistrationButton=(Button)findViewById(R.id.button);  
  19.         NameEditText=(EditText)findViewById(R.id.editText1);  
  20.         wordEditText=(EditText)findViewById(R.id.editText2);  
  21.         RegistrationButton.setOnClickListener(new View.OnClickListener() {  
  22.         @Override  
  23.         public void onClick(View view) {  
  24.         final String Name=NameEditText.getText().toString();  
  25.         final String word=wordEditText.getText().toString();  
  26.         if(Name.length()==0)  
  27.         {  
  28.           NameEditText.requestFocus();  
  29.           NameEditText.setError("FIELD CANNOT BE EMPTY");  
  30.         }  
  31.         else if(!Name.matches("[a-zA-Z ]+"))  
  32.         {  
  33.              NameEditText.requestFocus();  
  34.              NameEditText.setError("ENTER ONLY ALPHABETICAL CHARACTER");  
  35.         }  
  36.         else if(word.length()==0)  
  37.         {  
  38.              wordEditText.requestFocus();  
  39.              wordEditText.setError("FIELD CANNOT BE EMPTY");  
  40.         }  
  41.         else  
  42.         {  
  43.             Toast.makeText(MainActivity.this,"Validation Successful",Toast.LENGTH_LONG).show();  
  44.         }  
  45.  }  
  46. });  
  47.     }  
Step 4
 
When we entered no value for the person's name:
 
3.jpg
 
Step 5
 
When we entered no value for the word:
 
4.jpg
 
Step 6
 
When we entered a numeric value:
 
2.jpg
 
Step 7
 
When we entered a valid value:
 
6.jpg


Similar Articles