Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Highlight a Word In a TextView?

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.

like image 951
iTurki Avatar asked Jul 27 '11 11:07

iTurki


People also ask

How do you highlight text on Android?

Tap on the annotations icon at the bottom of the preview screen to open the annotations toolbar. You will see a set of tools appear. Next, select the highlight text tool. Tap and drag on a section of text within the file itself to highlight it.

How do you search for a word in a text android?

On an Android phone or tablet running a recent version of Google's Chrome browser, tap the menu icon in the upper-right corner of the window; the menu looks like three dots stacked up. When the menu opens, select “Find in Page” option and type in your search words with the keyboard.


2 Answers

insert HTML code for color around word and set it to your textView .

like

String newString = oldString.replaceAll(textToHighlight, "<font color='red'>"+textToHighlight+"</font>"); textView.setText(Html.fromHtml(newString)); 
like image 182
Shailendra Singh Rajawat Avatar answered Oct 01 '22 03:10

Shailendra Singh Rajawat


TextView textView = (TextView)findViewById(R.id.mytextview01);  //use a loop to change text color Spannable WordtoSpan = new SpannableString("partial colored text");         WordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 2, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); textView.setText(WordtoSpan); 

The numbers 2 and 4 are start/stop indexes for the coloring of the text, in this example "rti" would be colored.

So you would basically just find the starting index of your searching word in the title:

int startIndex = titleText.indexOf(term); int stopIndex = startIndex + term.length(); 

and then replace the numbers 2 and 4 with the indexes and "partial colored text" with your title string.

source: https://stackoverflow.com/a/10279703/2160827

like image 24
finstas Avatar answered Oct 01 '22 03:10

finstas