Android examples for User Interface:TextView
set Search Font Color in TextView
//package com.java2s; import android.graphics.Color; import android.text.Spannable; import android.text.SpannableStringBuilder; import android.text.TextUtils; import android.text.style.BackgroundColorSpan; import android.text.style.ForegroundColorSpan; import android.widget.TextView; public class Main { public static void setSearchFontColor(TextView view, String text, String word, boolean showBG) { if (!TextUtils.isEmpty(text)) { if (!TextUtils.isEmpty(word)) { int index = text.indexOf(word); if (-1 != index) { SpannableStringBuilder span = new SpannableStringBuilder( text);/*from w w w . j a v a 2 s.co m*/ span.setSpan(new ForegroundColorSpan(Color.BLACK), index, index + word.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); if (showBG) { span.setSpan(new BackgroundColorSpan(Color.RED), index, index + word.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } view.setText(span); } } else { view.setText(text); } } } public static void setSearchFontColor(TextView view, String text, String word) { setSearchFontColor(view, text, word, true); } }