Search code examples
androidandroid-webview

Android Webview - how to navigate to the NEXT occurence of the searched text


Searching for a text in a WebView is easy. Navigating to the first occurence of the searched text goes like this:

 webView.findAllAsync(whatToFind);
    webView.setFindListener((i, i1, b) -> {
        if (b) {
            webView.findNext(true);
        }
    });

How can I navigate to the NEXT occurence of the searched text?

I tried this but this will not navigate to the next occurence.

webView.findNext(true);

I loaded the webview in this way:

webView.loadDataWithBaseURL("", formatHtmlContent(content), "text/html", "UTF-8", "");

SOLUTION: Thanks to Du.Fantasy: Don't use the 'setFindListener' - unless you really need it. Take care of the timing when you use the findAllAsync.


Solution

  • It's enough to just call webView.findNext(true); But you need to be sure that calling webView.findAllAsync("string"); before when page is fully loaded. e.g. call it in onPageFinished or even delay a few seconds. I tested on Pixel 3 and it's working fine. I have recorded a video but I don't know how to post it here.

    eidt: please see the gif below:

    https://ftp.bmp.ovh/imgs/2021/04/166643b9c504af01.gif