7

i have implemented a webview in my android app and trying to highlight or to mark element when user click in the layout.

The webview is initialized as following :

myWebView.getSettings().setJavaScriptEnabled(true);
//myWebView.getSettings().setGeolocationEnabled(true);
//myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
myWebView.getSettings().setBuiltInZoomControls(true);
myWebView.getSettings().setDomStorageEnabled(true);
myWebView.setWebViewClient(new WebViewController());

Trying to mark the element which is clicked by user for example like in this screenshot :

Selection with dot

I'm getting all the page divs via jsoup :

doc = Jsoup.connect(url).get();
final Elements alldivs = doc.select("div");
ArrayList<String> list = new ArrayList<String>();
for (org.jsoup.nodes.Element e : alldivs) {
if (!e.id().equals(""))
list.add(e.id());
}

But how to mark the selection as the photo above, and after that select marked content from div id.

How can make some thing like this ?

ADM
  • 20,406
  • 11
  • 52
  • 83
EAK TEAM
  • 5,726
  • 4
  • 30
  • 52
  • 1
    `myWebView.loadUrl("javascript: your javascript code to select and highlight the element");` highlight clicked element clicked by the user https://stackoverflow.com/a/30811005 – Arpan Sarkar Mar 18 '18 at 15:01
  • @Arpanßløødyßadßøy , thank you for your explanation. `A detailed canonical answer is required to address all the concerns.` so add it as answer and please with more info and how to select all the divs too :) – EAK TEAM Mar 18 '18 at 15:21

1 Answers1

11

extend the WebViewClient and override the onPageFinished() here you need to add your own js to add click event listener on each div element

Kotlin:

class MyWebViewClient : WebViewClient() {
        override fun onPageFinished(view: WebView, url: String) {
            view.loadUrl("javascript: " +
                    """
            Object.prototype.each = function (fn, bind) {
                console.log(bind);
                for (var i = 0; i < this.length; i++) {
                    if (i in this) {
                        fn.call(bind, this[i], i, this);
                    }
                }
            };

            var _addListener = document.addEventListener || document.attachEvent,
                _eventClick = window.addEventListener ? 'click' : 'onclick';

            var elements = document.getElementsByTagName("div");

            elements.each(function (el) {
                _addListener.call(el, _eventClick, function () {
                    // todo process the clicked div element
                    el.style.cssText = "border-color:  black;border-style:  dashed;"
                }, false);
            })
                    """.trimIndent())
        }
    }

Java:

public class MyWebViewClient extends WebViewClient {
    @Override
    public void onPageFinished(WebView view, String url) {
        view.loadUrl("javascript: "
                + "Object.prototype.each = function (fn, bind) {\n" +
                "                console.log(bind);\n" +
                "                for (var i = 0; i < this.length; i++) {\n" +
                "                    if (i in this) {\n" +
                "                        fn.call(bind, this[i], i, this);\n" +
                "                    }\n" +
                "                }\n" +
                "            };\n" +
                "\n" +
                "            var _addListener = document.addEventListener || document.attachEvent,\n" +
                "                _eventClick = window.addEventListener ? 'click' : 'onclick';\n" +
                "\n" +
                "            var elements = document.getElementsByTagName(\"div\");\n" +
                "\n" +
                "            elements.each(function (el) {\n" +
                "                _addListener.call(el, _eventClick, function () {\n" +
                                 // todo process the clicked div element
                "                    el.style.cssText = \"border-color:  black;border-style:  dashed;\"\n" +
                "                }, false);\n" +
                "            })");
    }
}

now set your webViewClient to the webView

  webview.webViewClient = MyWebViewClient()

enter image description here

Arpan Sarkar
  • 2,301
  • 2
  • 13
  • 24
  • Hello , Thanks for your answer i have accepted it, but one more question : How to show a toast after selected div and show the div id for example ? – EAK TEAM Mar 23 '18 at 17:15