21

Is it possible to Highlight text in a TextView or WebView?

I see it is possible in a EditText

Highligh text in a EditText

I'd like to do the same in TextView or WebView.
Thats Possible?

Community
  • 1
  • 1
  • Check this out as well http://stackoverflow.com/questions/6309093/how-to-allow-the-user-to-select-a-text-range-in-a-textview-similar-to-edittext – hassanadnan Sep 06 '11 at 17:08
  • possible duplicate of [Is it possible to have multiple styles inside a TextView?](http://stackoverflow.com/questions/1529068/is-it-possible-to-have-multiple-styles-inside-a-textview) – blahdiblah Oct 09 '13 at 19:00
  • What's the way to do it in a webview? – AnupamChugh Dec 18 '17 at 09:11

4 Answers4

61

enable TextView's Spannable storage! by default Spannable storage in EditText is true.

so

TextView myTV = (TextView)findViewById(R.id.textView1);
String  textString = "StackOverFlow Rocks!!!"; 
Spannable spanText = Spannable.Factory.getInstance().newSpannable(textString);
spanText.setSpan(new BackgroundColorSpan(0xFFFFFF00), 14, 19, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
myTV.setText(spanText);
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
  • 3
    Thank you, it's work fine. But I think create Spannable Text by: Spannable spanText = Spannable.Factory.getInstance().newSpannable(textString); is better than textView.setText() and textView.getText(). – VAdaihiep May 03 '13 at 03:03
  • Is there any way to create a justified text-view and then set multiple spans to it? – Hamid Reza Aug 16 '15 at 07:46
  • Is there a way to display the highlighted matches in a webview? – AnupamChugh Dec 18 '17 at 09:11
  • Great answer that helped me but two things strike me about the Android API related to this: 1) this is still the best way to do this after 9 years? Seems like there would be a better/cleaner API call. 2) It is very odd that you have to set the actual text value of the view item to set the style. This feels like a Conflict of Concerns (versus Separation of Concerns). Feels like style and content mixed together. – raddevus Mar 01 '19 at 18:25
1

As well you could use this one solution for WebView. Call findAllAsync

webview.findAllAsync(((EditText) findViewById(R.id.edit)).getText().toString());

than add FindListener to WebView

    webview.setFindListener(new FindListener() {

        @Override
        public void onFindResultReceived(int activeMatchOrdinal, int numberOfMatches, boolean isDoneCounting) {
            Toast.makeText(getApplicationContext(), "Matches: " + numberOfMatches, Toast.LENGTH_LONG).show();
        }
    });

Iterate in result with webview.findNext(false); where false/true shows the direction.

But this solution was added in API level 16!!! Instead of you can setup JavaScript for higlihting - http://www.nsftools.com/misc/SearchAndHighlight.htm

validcat
  • 6,158
  • 2
  • 29
  • 38
0

For TextView

You can use textView.setTextIsSelectable(true) in your activity or fragment or adapter.

Kishan Solanki
  • 13,761
  • 4
  • 85
  • 82
-1

Actually, you do not have to develop this feature by yourself. You just need to use EditText instead TextView, while you set the android:editable of EditText to false.

My answer is here, hope it may help you:

https://stackoverflow.com/a/11026292/966405

Community
  • 1
  • 1
Ruobin Wang
  • 358
  • 3
  • 9