Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Highlight text in WebView above android version 4.0.3

I have a webview that performs a search, this code works great for below android version 4.0.3, at this version and higher the highlight function fails to highlight. The search itself works and jumps the page around but nothing is shown for the result (Unless you look hard enough!).

I want to know if anyone knows of a work-around that supports higher than 4.0.3?

People in other answers have linked to these webpages as possible solutions but I am not sure how to adapt this to my webview in Android.

WebPages:

http://4umi.com/web/javascript/hilite.php#thescript
http://www.nsftools.com/misc/SearchAndHighlight.htm

Code I am using currently to search and highlight:

        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if ((event.getAction() == KeyEvent.ACTION_DOWN)
                    && ((keyCode == KeyEvent.KEYCODE_ENTER))) {
                wv.findAll(findBox.getText().toString());

                try {
                    Method m = WebView.class.getMethod("setFindIsUp",
                            Boolean.TYPE);
                    m.invoke(wv, true);
                } catch (Exception ignored) {
                }
            }
            return false;
        }
like image 447
user1561757 Avatar asked Dec 14 '25 16:12

user1561757


1 Answers

In Android 4.1(jellybean), WebView.findAll() is deprecated, we should use WebView.findAllAsync instead.

reference

Wish this help:)

like image 86
Joy Wen Avatar answered Dec 16 '25 08:12

Joy Wen