0

I have a WebView contains some html code, this html code is a code that displays five buttons created through it, I want to listen to the onClick or onTouch action of these buttons so that when the user clicks on it some action happens, all I want to know is how to assign onClick for these views which is created by html, and for sure the onClick process can be added to buttons through the html code itself so it will OK also if there is a way to assign an onClick regarding to html code.

Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118

2 Answers2

1

You need to write JavaScript for HTML button click listeners.

<button type="button" id="exit" onclick="exitButtonClick();">Exit</button>

<script language="javascript">

   function exitButtonClick()
   {
      // Do Something
   }

</script>

If you want to call native java code from HTML's buttons then you need to add JavaScriptInterface to your WebView. For details please check below link

http://developer.android.com/guide/webapps/webview.html

Ganesh AB
  • 4,652
  • 2
  • 18
  • 29
0

You can't add listener for html's buttons in android code. Instead, you can use Javascript Interface with Webview.

public final class MyJSInterface
{
    public void button1function(){
        //do something
    }
}

Write all functions in this Interface.

Add that JSInterface to your webview.

myWebView.addJavascriptInterface(new MyJSInterface(), "MyJSInterface");  //name you want that will be used in javascript file.

Now in your html's javascript, you can call these functions using JavaScriptInterface name:

function method1() {
    MyJSInterface.button1function();
}

Hope this helps.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124