Android examples for User Interface:TextView
set Word Color And Typed Face for TextView
//package com.java2s; import android.text.Spannable; import android.text.SpannableStringBuilder; import android.text.TextUtils; import android.text.style.ForegroundColorSpan; import android.text.style.StyleSpan; import android.widget.TextView; public class Main { public static void setWordColorAndTypedFace(TextView view, int color, int style, Object... words) { setWordColor(view, color, words); setWordTypedFace(view, style, words); }/*from w w w.j a v a 2 s .c om*/ public static void setWordColor(TextView view, int color, Object... words) { CharSequence info = view.getText(); if (!TextUtils.isEmpty(info) && null != words) { SpannableStringBuilder span = new SpannableStringBuilder(info); for (int i = 0; i < words.length; i++) { if (null != words[i]) { int start = info.toString() .indexOf(words[i].toString()); if (-1 != start) { span.setSpan(new ForegroundColorSpan(color), start, start + words[i].toString().length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); } } } view.setText(span); } } public static void setWordTypedFace(TextView view, int style, Object... words) { CharSequence info = view.getText(); if (!TextUtils.isEmpty(info) && null != words) { SpannableStringBuilder span = new SpannableStringBuilder(info); for (int i = 0; i < words.length; i++) { if (null != words[i]) { int start = info.toString() .indexOf(words[i].toString()); if (-1 != start) { span.setSpan(new StyleSpan(style), start, start + words[i].toString().length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); } } } view.setText(span); } } }