Android examples for android.widget:TextView
set Keys Color for TextView
import android.content.Context; import android.text.Spannable; import android.text.SpannableStringBuilder; import android.text.TextUtils; import android.text.style.ForegroundColorSpan; import android.text.style.RelativeSizeSpan; import android.widget.TextView; import java.net.URLEncoder; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; public class Main{ public static void setKeysColor(TextView textView, String text, String key, int color) { SpannableStringBuilder builder = new SpannableStringBuilder(text); for (int i = 0; i < key.length(); i++) { char tem = key.charAt(i); String _skey = String.valueOf(tem); for (int j = 0; j < text.length(); j++) { char temp_content = text.charAt(j); String _sContent = String.valueOf(temp_content); if (_skey.equals(_sContent)) { builder.setSpan(new ForegroundColorSpan(color), j, j + _sContent.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); continue; }//from w w w. j av a 2s. co m } } textView.setText(builder); } }