9

I have a webview in my activity. Now when I use WebView.findAll() method to search text in webview it is not highlighting the matching words.

It works fine in Android 1.6 but is not working in 2.2.

peSHIr
  • 6,279
  • 1
  • 34
  • 46
Brijesh Masrani
  • 1,439
  • 3
  • 16
  • 27

2 Answers2

15

There is an issue in Android issue tracker about this: http://code.google.com/p/android/issues/detail?id=9018

I placed this code right after WebView.findAll(), and it made highlighting working:

try
{
    Method m = WebView.class.getMethod("setFindIsUp", Boolean.TYPE);
    m.invoke(webView, true);
}
catch (Throwable ignored){}
Ilya Izhovkin
  • 3,455
  • 4
  • 22
  • 25
  • 1
    This sounds like a nasty way to do it. But it does work! Thanks. – Peter Fortuin Jul 12 '11 at 13:07
  • I am getting a problem here. My text is highlighted but the findAll() method returns 0. I am not able to understand why?? Any string i may take, it highlights them properly but the findAll() method always returns 0. Any help would be appreciated :) – Antrromet Feb 29 '12 at 05:06
8

In android 4.0.3, seems the setFindIsUp is a private method. So above code won't work. As getMethod() method won't return the private methods. Following is a work-around to call the private method which works for 4.0.3:

try{
    //Can't use getMethod() as it's a private method
    for(Method m : WebView.class.getDeclaredMethods()){
        if(m.getName().equals("setFindIsUp")){
            m.setAccessible(true);
            m.invoke(view, true);
            break;
        }
    }
}catch(Exception ignored){}  
Marijn
  • 10,367
  • 5
  • 59
  • 80
jianwu_chen
  • 81
  • 1
  • 1
  • Although this code successfully find "setFindIsUp", but it seems not working for me. Highlighting is not appearing in 4.0.3, but it did with 2.2 (with this code). – Mike Keskinov Sep 21 '12 at 13:48
  • Sorry, it works for 2.2 and 3.2, but not for 4.0.3! I need for solution for 4.0.3! – Mike Keskinov Sep 21 '12 at 13:58