I have a database search query
which search in the database for a word entered by the user and return a Cursor
.
In my ListActivity
, I have a ListView
which will hold the items (the Cursor items). The ListView
items layout is basically a TextView
. I mean, the ListView
will be a list of TextView
's.
What I want is to highlight the search term
wherever it appears in the TextView
. I mean by highlighting: different color or different background color or anything makes it different than the rest of the text.
Is this possible? and how?
Update:
cursor = myDbHelper.search(term); //term: a word entered by the user.
cursor.moveToFirst();
String[] columns = {cursor.getColumnName(1)};
int[] columnsLayouts = {R.id.item_title}; //item_title: the TextView holding the one raw
ca = new SimpleCursorAdapter(this.getBaseContext(), R.layout.items_layout, cursor,columns , columnsLayouts);
lv = getListView();
lv.setAdapter(ca);
For @Shailendra: The search()
method will return some titles. I want to highlight the words in those titles that matches the term
word. I hope this is clear now.
You do so in
xml strings
if your strings are staticTry this library Android TextHighlighter.
Implementations
TextView.setText()
gets a parameter asSpannable
not onlyCharacterSequence
. SpannableString has a methodsetSpan()
which allows applying styles.See list of direct subclass form CharacterStyle https://developer.android.com/reference/android/text/style/CharacterStyle.html
I haven't done it but this looks promising:
More Easy Way
You can use Spannable class for highlighting/formatting part of Text.
Here is the method.
You can check this answer for clickable highlighted text.
insert HTML code for color around word and set it to your textView .
like
I know it's old question but i have created a method to highlight a repeated-word in string\paragraph.
usage: