Open In App

How to Detect Text Type Automatically in Android?

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to implement a feature related to TextView which is very important in every perspective. While using any social Media App or Notes app you may have seen that when we type something, it automatically detect the text type like email, phone, or a URL. Here we are going to implement that feature. A sample video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.  

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.

Step 2: Working with the activity_main.xml file

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




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">
  
    <TextView
        android:id="@+id/mobile"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:textColor="@color/black"
        android:textSize="32sp" />
  
    <TextView
        android:id="@+id/email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:textColor="@color/black"
        android:textSize="32sp" />
  
    <TextView
        android:id="@+id/google"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:textColor="@color/black"
        android:textSize="32sp" />
      
</LinearLayout>


Step 3: Working with the MainActivity.java file

Go to the MainActivity.java file and refer to the following code. This is how we are implementing auto-detect text type of our text

Linkify.addLinks(email, Linkify.ALL);

Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.

Java




import android.os.Bundle;
import android.text.util.Linkify;
import android.widget.TextView;
  
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
          
        //initialising the layout
        TextView mobile = (TextView) findViewById(R.id.mobile);
        TextView google = (TextView) findViewById(R.id.google);
        TextView email = (TextView) findViewById(R.id.email);
          
        // setting the email
        email.setText("kumarianni1213@gmail.com");
          
        // setting the mobile number
        mobile.setText("+919065239713");
          
        // setting the web url to visit
        google.setText("www.google.com");
          
        // linking all type of text
        Linkify.addLinks(email, Linkify.ALL);
        Linkify.addLinks(mobile, Linkify.ALL);
        Linkify.addLinks(google, Linkify.ALL);
    }
}


Output:



Last Updated : 26 Apr, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads