How to Detect Text Type Automatically in Android?
Last Updated :
26 Apr, 2021
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:android="http://schemas.android.com/apk/res/android"
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("[email protected]");
// 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:
Similar Reads
How to Change Typeface of TextView in Android? A typeface is a particular design for alphabets that separates it from other typefaces in terms of style, size, and weight variations. In general, there are a lot of local typefaces available on your device or software for use. However, many more typefaces are available on the Internet that can be d
2 min read
Android | Auto Complete TextBox and How to Create it Pre-requisites: Android App Development Fundamentals for BeginnersGuide to Install and Set up Android StudioAndroid | Starting with first app/android projectAndroid | Running your first Android app Android AutoComplete TextViews is used for Completing the words automatically. Whenever the user write
4 min read
How to Detect Cellular Network Type (2G, 3G, 4G and 5G) in Android? Android devices like mobiles and tablets are available with SIM card slots. Users can use them to connect to a particular network. Over time, telecommunication networks have changed rapidly giving users the extent of a new band with higher data speed. Devices are manufactured according to the advanc
4 min read
How to Convert Text to Speech in Android? Text to Speech App converts the text written on the screen to speech like you have written "Hello World" on the screen and when you press the button it will speak "Hello World". Text-to-speech is commonly used as an accessibility feature to help people who have trouble reading on-screen text, but it
3 min read
How to Convert Speech to Text in Android? In this article, speech to text feature is implemented in an application in Android. Speech to text means that anything that the user says is converted into text. This feature has come out to be a very common and useful feature for the users. In various places where search feature is implemented lik
5 min read
How to Read a Text File in Android? A text file is a type of file that can store a sequence of characters or text. These characters can be anything that is human-readable. Such kind of files does not have any formatting for the text like Bold, Italics, Underline, Font, Font Size, etc. A Text file in Android can be used for accessing o
3 min read