Highlight Text in TextView or WebView
Asked Answered
H

4

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?

Hebdomad answered 22/1, 2010 at 19:51 Comment(3)
Check this out as well #6309593Gemination
possible duplicate of Is it possible to have multiple styles inside a TextView?Orlov
What's the way to do it in a webview?Consequential
C
62

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);
Chlorenchyma answered 22/1, 2010 at 20:14 Comment(4)
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().Berrie
Is there any way to create a justified text-view and then set multiple spans to it?Uranometry
Is there a way to display the highlighted matches in a webview?Consequential
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.Swivet
V
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

Vacua answered 28/10, 2013 at 11:0 Comment(0)
P
0

For TextView

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

Pugnacious answered 8/3, 2021 at 16:3 Comment(0)
G
-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://mcmap.net/q/49620/-copy-text-from-textview-on-android

Gates answered 14/6, 2012 at 3:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.