highlight String with SpannableStringBuilder and check empty values - Android android.text

Android examples for android.text:SpannableString

Description

highlight String with SpannableStringBuilder and check empty values

Demo Code

import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main{

    public static SpannableStringBuilder hightLinghtString(String destStr,
            String subStr, int color) {
        SpannableStringBuilder style = new SpannableStringBuilder(
                null == destStr ? "" : destStr);
        if (null == destStr || "".equals(destStr) || "".equals(subStr)) {
            return style;
        }/*from  ww  w .ja  v a2 s . co m*/
        int start = destStr.lastIndexOf(subStr);
        if (start != -1) {
            style.setSpan(new ForegroundColorSpan(color), start, start
                    + subStr.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        }
        return style;
    }

}

Related Tutorials