ravi_user1648651
ravi_user1648651

Reputation: 33

Search & Highlight a word from html webpage shown in webview?

I want to search and highlight a word in android webview, I tried with this code but it is not working. My code is here:

webview.findAll("a");
webview.setSelected(true);
webview.findNext(true);

try {
    for (Method m : WebView.class.getDeclaredMethods()) {
        if (m.getName().equals("setFindIsUp")) {
            // m.setAccessible(true);
            m.invoke(webview, true);
            break;
        }
    }
} catch (Exception ignored) 
{
    Log.i("highlight error", ignored.toString());
}

This code is not setting any highlight on the selected word or not giving any error so please tell me how to search for and select a word in a webview ,currently i am trying for android version 3.2?

Upvotes: 0

Views: 1747

Answers (1)

Mark JW
Mark JW

Reputation: 496

Setup a search button on your WebView layout. Set up the WebView, and then a Search Button as follows. The popup dialog has a Cancel Button, a Search Button and an Edit Text for the search term. When search is pressed, each match of the EditText string will be highlighted in the WebView.

    final Activity activity = this;

    webView = (WebView) findViewById(R.id.yourWebView);  
    webView.setScrollbarFadingEnabled(false);  
    webView.setHorizontalScrollBarEnabled(false);  
    webView.getSettings().setJavaScriptEnabled(true);  
    webView.clearCache(true); 

    webView.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress) {
        progressDialog.setCanceledOnTouchOutside(true);
        progressDialog.setTitle("Loading Web Page");
        progressDialog.setIcon(R.drawable.ic_menu_allfriends);
        progressDialog.setButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {  
                webView.destroy();
                finish();
                } });
        progressDialog.show();
        progressDialog.setProgress(0);
        activity.setProgress(progress * 1000);
        progressDialog.incrementProgressBy(progress);
        if(progress == 100 && progressDialog.isShowing())
                progressDialog.dismiss();
        }
    });

    webView.loadUrl(yourStringURL);

    Button search = (Button) findViewById(R.id.butSearch);
    search.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
        //set up button for search keyword of the webView                 
            final Dialog dSearch = new Dialog(myActivity.this);
            dSearch.setContentView(R.layout.search_keyword);
            dSearch.setCancelable(true);
            dSearch.setTitle(getString(R.string.yourTitle));

            Button cancel = (Button) dSearch.findViewById(R.id.searchCancel);
            cancel.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    dSearch.dismiss();
                }
            });

            Button search = (Button) dSearch.findViewById(R.id.searchAdd);
            search.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    EditText etkw = (EditText) dSearch.findViewById(R.id.searchword);
                    String keyword = etkw.getText().toString();
                    dSearch.dismiss();
                    webView.findAll(keyword);
                    try
                        {
                        Method m = WebView.class.getMethod("setFindIsUp", Boolean.TYPE);
                        m.invoke(webView, true);
                        }
                    catch (Throwable ignored){}
                }
            }); 
            dSearch.show();
        }
    }); 

Upvotes: 1

Related Questions