Open In App

Text Highlighter in Android

Last Updated : 23 Feb, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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

Attributes

Description

.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 ‘com.xeoh.android:text-highlighter:1.0.3’

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




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <!--Scroll view for scrolling-->
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true">
          
        <!--Linear layout to arrange elements one below another-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="30dp">
  
            <!--TextView to display text-->
            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                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. " />
        </LinearLayout>
    </ScrollView>
  
    <!--Button to search text-->
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/search"
        android:layout_centerInParent="true"
        android:layout_margin="20dp"
        android:padding="10dp"
        android:text="Search" />
  
    <!--EditText to give text input-->
    <EditText
        android:id="@+id/search"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_margin="10dp"
        android:ems="10"
        android:hint="Search"
        android:inputType="text"
        android:text="" />
      
</RelativeLayout>


Step 4: Working with the MainActivity.java file

Go to the MainActivity.java file and refer to the following code. 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.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
  
import androidx.appcompat.app.AppCompatActivity;
  
import com.xeoh.android.texthighlighter.TextHighlighter;
  
public class MainActivity extends AppCompatActivity {
  
    // Variable for button,
    // edit text and text view given
    Button button;
    EditText editText;
    TextView textView;
      
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // Accepted through Id's
        button = (Button) findViewById(R.id.button);
        editText = (EditText) findViewById(R.id.search);
        textView = (TextView) findViewById(R.id.textView);
          
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new TextHighlighter()
                        .setBackgroundColor(Color.parseColor("#FFFF00"))
                        .setForegroundColor(Color.GREEN)
                        .addTarget(textView)
                        .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.

android:windowSoftInputMode=”adjustNothing|stateHidden”>

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

XML




<?xml version="1.0" encoding="utf-8"?>
    package="com.example.emoji_slider">
  
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Emoji_slider">
        <activity android:name=".MainActivity"
                    
            android:windowSoftInputMode="adjustNothing|stateHidden">
            
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
  
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
  
</manifest>


Output:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads