get High Light Text - Android android.text

Android examples for android.text:SpannableString

Description

get High Light Text

Demo Code

import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.CharacterStyle;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.util.Locale;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

public class Main{

    public static CharSequence getHighLightText(String content, int color,
            int start, int end) {
        if (TextUtils.isEmpty(content)) {
            return "";
        }/*from ww  w.jav a 2  s . co  m*/
        start = start >= 0 ? start : 0;
        end = end <= content.length() ? end : content.length();
        SpannableString spannable = new SpannableString(content);
        CharacterStyle span = new ForegroundColorSpan(color);
        spannable.setSpan(span, start, end,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannable;
    }

}

Related Tutorials