Skip to content
Related Articles

Related Articles

Text Highlighter in Android

Improve Article
Save Article
  • Last Updated : 23 Feb, 2021
Improve Article
Save Article

Text Highlighter is one of the popular functions in most apps. You can get to see this function in Notes keeping application or in any educational application. The main function of this feature is that it highlights the searched word in any document. In this article, we are going to see how to implement Text Highlighter in our Android App. A sample GIF 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. 

Text Highlighter in Android Sample GIF

Application of Text Highlighter 

  • The main feature of using this Text Highlighter is that it highlights the searched word in your app.
  • It tells you how much time the searched word is repeated and it gets highlighted.
  • It is used for keyword research in an application.

Important Attributes



.setBackgroundColor()Use to set Background Color.
.setForegroundColor()Use to set Text Color.
.addTarget()Use to search a word  from certain document.
.highlight()Use to highlight the searched text.

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: Add dependency of Text Highlighter library in build.gradle file

Then Navigate to gradle scripts and then to build.gradle(Module) level. Add below line in build.gradle file in the dependencies section.

implementation ‘’

Now click on Sync now it will sync your all files in build.gradle().

Step 3: Create a new Text Highlighter in your activity_main.xml file

Go to the activity_main.xml file and refer to the following code. Below is the code for the activity_main.xml file.


<?xml version="1.0" encoding="utf-8"?>
    <!--Scroll view for scrolling-->
        <!--Linear layout to arrange elements one below another-->
            <!--TextView to display text-->
                android:text="Android is a mobile operating system based on a modified version of the Linux kernel and other open source software, designed primarily for touchscreen mobile devices such as smartphones and tablets. Android is developed by a consortium of developers known as the Open Handset Alliance and commercially sponsored by Google. It was unveiled in November 2007, with the first commercial Android device launched in September 2008.
                              It is free and open source software; its source code is known as Android Open Source Project (AOSP), which is primarily licensed under the Apache License. However most Android devices ship with additional proprietary software pre-installed,[10] most notably Google Mobile Services (GMS)[11] which includes core apps such as Google Chrome, the digital distribution platform Google Play and associated Google Play Services development platform. About 70 percent of Android smartphones run Google's ecosystem;[12] competing Android ecosystems and forks include Fire OS (developed by Amazon) or LineageOS. However the 'Android' name and logo are trademarks of Google which impose standards to restrict 'uncertified' devices outside their ecosystem to use Android branding.[13][14]
                              The source code has been used to develop variants of Android on a range of other electronics, such as game consoles, digital cameras, portable media players, PCs and others, each with a specialized user interface. Some well known derivatives include Android TV for televisions and Wear OS for wearables, both developed by Google. Software packages on Android, which use the APK format, are generally distributed through proprietary application stores like Google Play Store, Samsung Galaxy Store, and Huawei AppGallery, or open source platforms like Aptoide or F-Droid.
                              Android has been the best-selling OS worldwide on smartphones since 2011 and on tablets since 2013. As of May 2017, it has over two billion monthly active users, the largest installed base of any operating system, and as of August 2020, the Google Play Store features over 3 million apps.[15] The current stable version is Android 11, released on September 8, 2020. " />
    <!--Button to search text-->
        android:text="Search" />
    <!--EditText to give text input-->
        android:text="" />

Step 4: Working with the file

Go to the file and refer to the following code. Below is the code for the file. Comments are added inside the code to understand the code in more detail.


import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
    // Variable for button,
    // edit text and text view given
    Button button;
    EditText editText;
    TextView textView;
    protected void onCreate(Bundle savedInstanceState) {
        // Accepted through Id's
        button = (Button) findViewById(;
        editText = (EditText) findViewById(;
        textView = (TextView) findViewById(;
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                new TextHighlighter()
                        .highlight(editText.getText().toString(), TextHighlighter.BASE_MATCHER);

Step 5: Working with the AndroidManifest.xml file

Add the following line to the AndroidManifest.xml file inside the <activity> tag.


Below is the complete code for the AndroidManifest.xml file.


<?xml version="1.0" encoding="utf-8"?>
        <activity android:name=".MainActivity"
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!