Android WebView JavaScript interaction

Source: Internet
Author: User

Today, WebView with JavaScript in Android, the first is to add WebView controls to the layout file:

[HTML]View Plaincopy
    1. <WebView
    2. android:id="@+id/webview"
    3. android:layout_width="fill_parent"
    4. android:layout_height="fill_parent" />

Then add the permissions in the manifest:

[HTML]View Plaincopy
    1. <uses-permission android:name= " and

If WebView can interact with JavaScript, you first need to WebView to enable javascript:

[HTML]View Plaincopy
    1. WebSettings websettings = mywebview.getsettings ();
    2. Websettings.setjavascriptenabled (TRUE);

Then create the JavaScript interface:

[Java]View Plaincopy
  1. Public class Webappinterface {
  2. Context Mcontext;
  3. /** instantiate the interface and set the context * /
  4. Webappinterface (Context c) {
  5. Mcontext = C;
  6. }
  7. /** Show A toast from the Web page * /
  8. @JavascriptInterface
  9. public void Showtoast (String toast) {
  10. Toast.maketext (Mcontext, Toast, Toast.length_short). Show ();
  11. }
  12. }

To add a JavaScript interface to WebView:

[HTML]View Plaincopy
    1. Mywebview.addjavascriptinterface (New Webappinterface (This), "Android");

Local JavaScript files:

[JavaScript]View Plaincopy
    1. <input type="button" value="Say Hello" onclick="showandroidtoast (' Hello android! ')"/>
    2. <script type="Text/javascript" >
    3. function Showandroidtoast (toast) {
    4. Android.showtoast (toast);
    5. }
    6. </script>

The entire code is as follows:

[Java]View Plaincopy
  1. Public class Mainactivity extends Activity {
  2. private WebView Mywebview;
  3. @Override
  4. protected void OnCreate (Bundle savedinstancestate) {
  5. super.oncreate (savedinstancestate);
  6. Setcontentview (R.layout.activity_main);
  7. Mywebview = (WebView) Findviewbyid (R.id.webview);
  8. WebSettings websettings = Mywebview.getsettings ();
  9. Websettings.setjavascriptenabled (true);
  10. Mywebview.addjavascriptinterface (new Webappinterface (this), "Android");
  11. Processwebstring ();
  12. }
  13. public class Webappinterface {
  14. Context Mcontext;
  15. /** instantiate the interface and set the context * /
  16. Webappinterface (Context c) {
  17. Mcontext = C;
  18. }
  19. /** Show A toast from the Web page * /
  20. @JavascriptInterface
  21. public void Showtoast (String toast) {
  22. Toast.maketext (Mcontext, Toast, Toast.length_short). Show ();
  23. }
  24. }
  25. private void processwebstring () {
  26. //Load Asset file
  27. String TPL = getfromassets ("web_tpl.html");
  28. Mywebview.loaddatawithbaseurl (null, TPL, "text/html", "Utf-8", null);
  29. }
  30. /* 
  31. * Get HTML files
  32. */
  33. Public string Getfromassets (String fileName) {
  34. try {
  35. InputStreamReader Inputreader = new InputStreamReader (
  36. Getresources (). Getassets (). open (FileName);
  37. BufferedReader Bufreader = new BufferedReader (Inputreader);
  38. String line = "";
  39. String Result = "";
  40. While (line = Bufreader.readline ()) = null)
  41. Result + = line;
  42. return Result;
  43. } catch (Exception e) {
  44. E.printstacktrace ();
  45. }
  46. return "";
  47. }
  48. }

Operating effect:

Android WebView JavaScript interaction

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.