highlight string in read via SpannableStringBuilder - Android android.text

Android examples for android.text:SpannableString

Description

highlight string in read via SpannableStringBuilder

Demo Code

import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main{

    /**/* www .j a  va  2 s . co  m*/
     * hightlight text
     * @param start
     * @param end
     * @param text
     */
    public static void highlight(int start, int end, TextView text) {
        SpannableStringBuilder spannable = new SpannableStringBuilder(text
                .getText().toString());
        ForegroundColorSpan span = new ForegroundColorSpan(Color.RED);
        spannable.setSpan(span, start, end,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        text.setText(spannable);
    }

}

Related Tutorials