Phone validation is useful for users to check whether the number entered by the user is a valid number or not. If the user has entered the number is not valid then it will show a Toast message to re-enter the number. In this article, we are going to discuss how to implement the phone number validator in Android step by step. A sample video is given below to get an idea about what we are going to do in this article.
Note: This Android article covered in both Java and Kotlin languages.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. The code for that has been given in both Java and Kotlin Programming Language for Android.
Step 2: Working with activity_main.xml
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
<!-- XML file for phone number validation --> <? xml version = "1.0" encoding = "utf-8" ?>
< androidx.constraintlayout.widget.ConstraintLayout
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:background = "@color/white"
tools:context = ".MainActivity" >
< LinearLayout
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_weight = "1"
android:orientation = "vertical"
tools:ignore = "MissingConstraints" >
<!-- edittext field to enter mobile number -->
< EditText
android:id = "@+id/tt"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:textSize = "30dp"
android:layout_margin = "20dp"
android:textColor = "@color/purple_700"
android:textAlignment = "center"
android:layout_weight = "1"
android:ems = "10"
android:inputType = "textPersonName"
android:hint = "mobile number"
android:textColorHint = "@color/purple_700" />
<!-- Button for checking validation -->
< Button
android:id = "@+id/button"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_weight = "1"
android:textSize = "20dp"
android:text = "Validate"
android:layout_gravity = "center"
android:textColor = "@color/white"
android:backgroundTint = "@color/purple_700"
android:onClick = "check" />
</ LinearLayout >
</ androidx.constraintlayout.widget.ConstraintLayout >
|
Step 3: Working with the MainActivity File
Go to the MainActivity File and refer to the following code. Below is the code for the MainActivity File. Comments are added inside the code to understand the code in more detail.
package com.example.gfgfirstapp;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText textView;
Button button;
String phone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// registering with id's
textView = findViewById(R.id.tt);
button = findViewById(R.id.button);
// for change the background color of title bar
ActionBar aBar; aBar= getSupportActionBar();
ColorDrawable cd = new ColorDrawable(Color.parseColor( "#FF00FF00" ));
aBar.setBackgroundDrawable(cd);
}
// this function is called on clicking validate button
public void check(View view) {
// checking text is entered or empty
if (!textView.getText().toString().isEmpty()) {
// storing the entered number in to string
phone= textView.getText().toString().trim();
} else {
Toast.makeText(MainActivity. this , "Please enter mobile number " , Toast.LENGTH_LONG).show();
}
if (android.util.Patterns.PHONE.matcher(phone).matches())
// using android available method of checking phone
{
Toast.makeText(MainActivity. this , "MATCH" , Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(MainActivity. this , "NO MATCH" , Toast.LENGTH_LONG).show();
}
}
} |
package com.example.gfgfirstapp;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// implementation of onMeasure Custom View in java
textView = findViewById<EditText>(R.id.tt)
button = findViewById<Button>(R.id.button)
// for change the background color of title bar
// for change the background color of title bar
var aBar: ActionBar aBar = getSupportActionBar()
val cd = ColorDrawable(Color.parseColor( "#FF00FF00" ))
aBar.setBackgroundDrawable(cd)
}
fun check(view: View?) {
if (!textView.getText().toString().isEmpty()) {
phone = textView.getText().toString().trim({ it <= ' ' })
} else {
Toast.makeText( this @MainActivity , "Please enter mobile number " , Toast.LENGTH_LONG)
.show()
}
if (Patterns.PHONE.matcher(phone).matches()) {
Toast.makeText( this @MainActivity , "MATCH" , Toast.LENGTH_LONG).show()
} else {
Toast.makeText( this @MainActivity , "NO MATCH" , Toast.LENGTH_LONG).show()
}
}
} |
Output: