I assume that you have a custom Adapter with getCount()
and getView()
implemented and already filtering items, and you just need the bold part.
To achieve that, you need to use a SpannableString
, which is basically text with markup attached. For example, a TextAppearanceSpan
can be used to change typeface, font style, size, and color.
So, you should update your adapter's getView()
to change the part where you use textView.setText()
into something more or less like this:
String filter = ...;
String itemValue = ...;
int startPos = itemValue.toLowerCase(Locale.US).indexOf(filter.toLowerCase(Locale.US));
int endPos = startPos + filter.length();
if (startPos != -1) // This should always be true, just a sanity check
{
Spannable spannable = new SpannableString(itemValue);
ColorStateList blueColor = new ColorStateList(new int[][] { new int[] {}}, new int[] { Color.BLUE });
TextAppearanceSpan highlightSpan = new TextAppearanceSpan(null, Typeface.BOLD, -1, blueColor, null);
spannable.setSpan(highlightSpan, startPos, endPos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannable);
}
else
textView.setText(itemValue);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…