0

I have implemented a search list It outputs the matched strings from pattern in a given list view. Now I want to color only that part of my string which matches the pattern. Please help me. Thanks in advance.

Suraj
  • 165
  • 2
  • 11

2 Answers2

1

You're looking for spannable: http://developer.android.com/reference/android/text/Spannable.html

Example:

SpannableString demotext = new SpannableString("All your base are belong to me");
// Make 'All' (chars 0 to 3) red
demotext.setSpan(new ForegroundColorSpan(Color.RED), 0, 3, 0);
((TextView) getView().findViewById(R.id.fragment_disclaimer_title)).setText(demotext);

http://www.chrisumbel.com/article/android_textview_rich_text_spannablestring http://eazyprogramming.blogspot.co.uk/2013/06/spannable-string-in-android-url-span.html

James
  • 3,485
  • 3
  • 20
  • 43
0

for color a part of your string you can do like this :

String styledText = "This is <font color='red'>simple</font>.";
textView.setText(Html.fromHtml(styledText), TextView.BufferType.SPANNABLE);

just after modify this.

u53r
  • 994
  • 7
  • 6
  • String styledText = "This is simple."; textView.setText(Html.fromHtml(styledText), TextView.BufferType.SPANNABLE); Not working if I assign my string to text view which is in listview. – Suraj Oct 05 '14 at 09:51