Android: Events from JavaScript to Android

//Add interface to webview

webView.addJavascriptInterface(new WebAppInterface(getApplicationContext()), "Android");

class WebAppInterface {
    Context mContext;

    /**     * Instantiate the interface and set the context     */    
    WebAppInterface(Context c) {
        mContext = c;
    }

    /**     * Show a toast from the web page     */    
  @JavascriptInterface    
  public void showToast(String toast) {
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
 }

//Create a toast / event in JavaScript

<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />

<script type="text/javascript">
    function showAndroidToast(toast) {
        Android.showToast(toast);
    }
</script>

Comments

Popular posts from this blog

Working with Android Hierarchy Viewer

Android: Standalone login with SQLiteOpenHelper