highLight String - Android android.text

Android examples for android.text:SpannableString

Description

highLight String

Demo Code

import android.app.Activity;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageManager;
import android.text.Layout;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.style.ForegroundColorSpan;
import android.text.style.URLSpan;
import android.text.style.UnderlineSpan;
import android.widget.TextView;
import java.math.BigDecimal;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main{

    public static SpannableStringBuilder highLightStr(String str, int color) {
        SpannableStringBuilder style = new SpannableStringBuilder(str);
        style.setSpan(new ForegroundColorSpan(color), 0, str.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return style;
    }/*from   w  ww .j a  v a2 s  . c o  m*/
    public static SpannableStringBuilder highLightStr(String str,
            String highLightStr, int color) {
        int start = str.indexOf(highLightStr);
        if (start == -1) {
            return null;
        }
        SpannableStringBuilder style = new SpannableStringBuilder(str);

        style.setSpan(new ForegroundColorSpan(color), start, start
                + highLightStr.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return style;
    }

}

Related Tutorials