Skip to content

Instantly share code, notes, and snippets.

@simpleton
Last active December 18, 2015 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simpleton/5780804 to your computer and use it in GitHub Desktop.
Save simpleton/5780804 to your computer and use it in GitHub Desktop.
//html code
<html>
<head>
<script type="text/javascript">
function ClientSideMethod(message){
var x=document.getElementById("te");
x.innerHTML = message;
}
</script>
</head>
<body>
<div id="te"></div>
<button onClick="ClientSideMethod('client side method called')"> Call Javascript Method </button>
<button onClick="testClick('Hello aaa');">Call Java Activity Method</button>
</body>
</html>
//java code
public class MainActivity extends Activity {
private WebView webView;
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.webView = (WebView) findViewById(R.id.webview);
// requires javascript
this.webView.getSettings().setJavaScriptEnabled(true);
// set the html view to load
this.webView.loadUrl("file:///android_asset/test.html");
// make this activity accessible to javascript
this.webView.addJavascriptInterface(this, "activity");
this.webView.loadUrl("javascript:function testClick(aa) {var x=document.getElementById(\"te\");x.innerHTML = aa;activity.TestMethod(aa);}");
}
@JavascriptInterface
public void TestMethod(String aa)
{
Log.e(TAG, "TestMethod: " + aa);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment