0

I'm new in android studio. I'm working on a simple project. It uses a webview which loads the html page. I also use an action bar for searching, refreshing , ... in the page bellow:

enter image description here

By pressing the search button, findAllAsync() method is used to find and highlight all the matches in the page (matches are shown in yellow color). The next button in the action bar uses the findnext(true) method and allows user to move between the highlighted matches. Using this method, the next one is highlighted in red color:

  next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            st = et.getText().toString();
            if(!st.equals("")&& st!=null) {

               ourBrow.findNext(true);

            }//not null

        }//on click

    });// next Button click

If I only scroll or zoom in the webview everything is ok and the find next is continued from the previous one in the page. However if i touch somewhere in the webview and then click the next button, the red highlighting is not continued from the previous position in the page. For example if the previous red highlighted is at the first of the page and i touch the end of the page and when i click the next button, the middle yellow matches are escaped and the last one which is in end of the page is highlighted in red color and red highlighting is continued from this position.

Since i need the user can continue search from the previous position and touching the screen does not affect this order, i searched a lot. I tried to disable touch event of the webview but keeping zoom and scroll ability based on the Android disable WebView touch but keep zooming/scrolling. But my problem is still unsolved. Please kindly help me to solve this problem.

thanks.

Edit:

To solve this problem i tried to clear focus of the view(How to remove focus without setting focus to another control?). But it doesn't work:

View current = getCurrentFocus();
                    if (current != null) current.clearFocus();
                    ourBrow.findNext(true);
sara
  • 31
  • 5

1 Answers1

0

I would recommend the following not the best approach (and it depend on your web page) but it will work, render the content of your web page to html using :

webview.loadData(URLEncoder.encode(result).replaceAll("\\+", " "), "text/html", Encoding.UTF_8.toString());

Than do whatever you want with your text highlight the way you want and then render it back to web page like this:

myTextView.setText(Html.fromHtml("Your HTML Page"));

API after nugget:

myTextView.setText(Html.fromHtml("Your HTML Page", Html.FROM_HTML_MODE_COMPACT));

Hope it helps

Tarek
  • 708
  • 5
  • 14
  • thanks. but my problem is not searching the text in the html. using webview.findallasync() all matches are found correctly. my problem is the findnext() method. if I touch the screen, red highlighting is beginning from the position of touch in the screen. I'm searching a way to avoid this and user can move between the matches respectively. – sara May 16 '18 at 07:36