Code Monkey home page Code Monkey logo

webviewclickevents's Introduction

WebViewClickEvents

This project is an example that shows a way to handle multiple clicks in a JavaScript button placed inside a WebView

Screenshot

Here we are providing the HTML code that will be displayed in the WebView

private val data = """
<script language="javascript" type="text/javascript">
    function buttonClick1() {

        androidButton.onCapturedButtonClicked1();
    }
    function buttonClick2() {

        androidButton.onCapturedButtonClicked2();
    }
</script>

<button type='button' id='button1' onclick='buttonClick1();'>Click Button 1</button>
<button type='button' id='button2' onclick='buttonClick2();'>Click Button 2</button>
"""

As you see, we are referring an object called androidButton. To make this object exist inside the WebView, we need to add a @JavascriptInterface

  binding.webView.settings.javaScriptEnabled = true
      binding.webView.webChromeClient = WebChromeClient()
      binding.webView.addJavascriptInterface(CaptureClickJavascriptInterface(), "androidButton")
      binding.webView.loadData(data, "text/html", "UTF-8")


private inner class CaptureClickJavascriptInterface {
    @JavascriptInterface
    fun onCapturedButtonClicked1() {
        Toast.makeText(
            this@MainActivity,
            "Button1 is clicked.",
            Toast.LENGTH_SHORT
        ).show()
    }

    @JavascriptInterface
    fun onCapturedButtonClicked2() {
        Toast.makeText(
            this@MainActivity,
            "Button2 is clicked.",
            Toast.LENGTH_SHORT
        ).show()
    }

}

Once this is executed, we can refer to androidButton and its onCapturedButtonClicked1 and onCapturedButtonClicked2 method inside the JavaScript code

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.