Giter Site home page Giter Site logo

bindingjs's Introduction

BindingJS

This is a sample Android application to show how to bind Javascript code to Android code.

ScreenShot

For getting data from the webpage via Javascript follow the below step-by-step guidelines.

  1. To use WebView, declare the following the following code in the xml file.
<WebView
    android:id="@+id/webView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
  1. Then, add the following permissions to your AndroidManifest.xml file:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  1. For the client, to receive callbacks from JS there must be a Javascript interface class inside it which will implement the callbacks. You must add @JavascriptInterface annotation to all the methods inside the JS interface class.
public class JavaScriptReceiver
{
   Context mContext;
 
   /** Instantiate the receiver and set the context */
   JavaScriptReceiver(Context c) {
      mContext = c;
   }
 
   @JavascriptInterface
   public void showShopping(){
     Intent intent = new Intent(mContext, ShoppingActivity.class);
     mContext.startActivity(intent);
   }
 
   @JavascriptInterface
   public void showOrders(int orderid){
     Intent intent = new Intent(mContext, MyOrdersActivity.class);
     intent.putExtra("order",orderid);
     mContext.startActivity(intent);
   }
}
  1. Use the following snippet for initialising the webview.
  WebView webView;
 
  webView = (WebView) findViewById(R.id.webView);
  embed_link = "http://droidmentor-app-callback.surge.sh/";

  webView.setWebChromeClient(new WebChromeClient());
  webView.getSettings().setDomStorageEnabled(true);
  webView.getSettings().setJavaScriptEnabled(true);
  1. Use the same interface and function name in the calling statement inside the Javascript.
<script>
 
   function getRandomNumber(min,max) {
     return Math.floor( Math.random() * ( 1 + min - max ) ) + max;
   }
 
   function showOrders() {
     var number = getRandomNumber(10000,50000)
     JSReceiver.showOrders(number);
   }
 
   function showShopping() {
     JSReceiver.showShopping();
   }
</script>
 
<a href="#" class="btn-view-orders" onclick="showOrders()">View Orders</a>
<a href="#" onclick="showShopping()">Continue Shopping</a>

  1. For binding the JS into your source code, use addJavascriptInterface() method.
JavaScriptReceiver javaScriptReceiver;
javaScriptReceiver = new JavaScriptReceiver(this);
webView.addJavascriptInterface(javaScriptReceiver, "JSReceiver");

For more information, check out my detailed guide here : http://droidmentor.com/bind-javascript-to-android/

bindingjs's People

Contributors

jaisonfdo avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.