android – Highlight Text in TextView or WebView-ThrowExceptions

Exception or error:

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?

How to solve:

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


TextView myTV = (TextView)findViewById(;
String  textString = "StackOverFlow Rocks!!!"; 
Spannable spanText = Spannable.Factory.getInstance().newSpannable(textString);
spanText.setSpan(new BackgroundColorSpan(0xFFFFFF00), 14, 19, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);


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

webview.findAllAsync(((EditText) findViewById(;

than add FindListener to WebView

    webview.setFindListener(new FindListener() {

        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 –


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:

Leave a Reply

Your email address will not be published. Required fields are marked *