Call Android methods from JavaScript -


i searched, didn't find answer. i'm developing android app based on webview, using html5 , javascript. can call android method, maketoast() javascript?

you can adding javascript interface webview , exposing specific methods javascript code running in web view. in other words, you'll need wrap calls android's toast class in method create in activity/fragment.

activity_main.xml

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent">      <webview android:id="@+id/web_view"                 android:layout_width="fill_parent"                 android:layout_height="fill_parent"/>  </relativelayout> 

mainactivity.java

public class mainactivity extends activity {      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          webview webview = (webview)findviewbyid(r.id.web_view);         webview.loadurl("file:///android_asset/web.html");          webview.getsettings().setjavascriptenabled(true);         webview.addjavascriptinterface(new webviewjavascriptinterface(this), "app");     }      /*      * javascript interface. web code can access methods in here       * (as long have @javascriptinterface annotation)      */     public class webviewjavascriptinterface{          private context context;          /*          * need reference context in order sent post message          */         public webviewjavascriptinterface(context context){             this.context = context;         }          /*           * method can called android. @javascriptinterface           * required after sdk version 17.           */         @javascriptinterface         public void maketoast(string message, boolean lengthlong){             toast.maketext(context, message, (lengthlong ? toast.length_long : toast.length_short)).show();         }     }  } 

assets/web.html

<!doctype html> <html> <head>     <title>javascript view</title>      <script type="text/javascript">          function showtoast(){             var message = document.getelementbyid("message").value;             var lengthlong = document.getelementbyid("length").checked;              /*                  call 'maketoast' method in java code.                  'app' specified in mainactivity.java when                  adding javascript interface.               */             app.maketoast(message, lengthlong);             return false;         }          /*              call 'showtoast' method when form gets              submitted (by pressing button or return key on keyboard).           */         window.onload = function(){             var form = document.getelementbyid("form");             form.onsubmit = showtoast;         }     </script> </head>  <body>  <form id="form">     message: <input id="message" name="message" type="text"/><br />     long: <input id="length" name="length" type="checkbox" /><br />      <input type="submit" value="make toast" /> </form>  </body> </html> 

a toast message


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -