AnonymousZA
AnonymousZA

Reputation: 131

Add javascript to Webview

I have created a javascript script and would like to run it in a webview in my android app. How do I go about doing this so when the user clicks on the money button the javascript runs.

Code Below

Javascript

<script type="text/javascript" src="https://static.leaddyno.com/js"></script>
<script>
  LeadDyno.key = "hg5kjh5j4h6jhjhjkk7hjhjhjkh56vhjfgcg4gjd";
  LeadDyno.recordVisit();
  LeadDyno.autoWatch();
</script>

Button Click

btnMoney.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           //Run Javascript
          myWebView.loadUrl("javascript:LeadDyno.recordVisit()");
          myWebview2.loadUrl("javascript.LeadDyno.autoWatch()");
        }
    });

Upvotes: 0

Views: 61

Answers (1)

aolphn
aolphn

Reputation: 2998

  1. create assets folder in app/src/main;
  2. create a new file html.html;
  3. add your javascript code into html.html;
  4. load it by webview like:

    webview.load("file:///android_asset/html.html");

  5. Do not forget to add android.permission.INTERNET in your manifest file

Upvotes: 1

Related Questions