Tuesday, March 15, 2016

android - Highlight text in a TextView

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity"
    android:background="#eae8e4"
    >
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="40dp"
        android:layout_margin="5dp"
        android:padding="5dp"
        android:fontFamily="sans-serif-condensed"
        android:textColor="@android:color/black"
        android:text="Click the button to highlight the TextView specific text/word."
        />
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Highlight Text 'The'"
        android:layout_below="@id/tv"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {

    private Context mContext;
    private Activity mActivity;

    private RelativeLayout mRelativeLayout;
    private TextView mTextView;
    private Button mButton;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the application context
        mContext = getApplicationContext();

        // Get the activity
        mActivity = MainActivity.this;

        // Get the widgets reference from XML layout
        mRelativeLayout = (RelativeLayout) findViewById(R.id.rl);
        mTextView = (TextView) findViewById(R.id.tv);
        mButton = (Button) findViewById(R.id.btn);

        // Set a click listener for button
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Do the task
                doTask();
            }
        });
    }

    // Custom method to do a task
    protected void doTask(){
        // Specify the text/word to highlight inside TextView
        String textToHighlight = "the";

        // Construct the formatted text
        String replacedWith = "<font color='red'>" + textToHighlight + "</font>";

        // Get the text from TextView
        String originalString = mTextView.getText().toString();

        // Replace the specified text/word with formatted text/word
        String modifiedString = originalString.replaceAll(textToHighlight,replacedWith);

        // Update the TextView text
        mTextView.setText(Html.fromHtml(modifiedString));
    }
}