Loading...

Android WebView Native Bridge

WebView represents a URL inside the android application. This makes users stay in your application for a longer period. Android webView provide a proper display of website on the application with also being able to access the website feature.

In the process of using a WebView, we might need to input(get) data from the user of the following URL. For the ease of getting data from the webView to java, the WebViewNativeBridge Library comes into use. The library is developed in JAVA language and a bit of HTML. 

The WebViewNativeBrige Library allows a developer to send simple data(parameters) from the WebVIew platform inside the android application to your Java program. The library uses a URL to send data from webView to Java.



Let us now know how the WebVIewnativeBridge sends data via url:

Url Structure

nativebridge://functionName/paramter1/parameter2/parameter3
  • Few requirements to be followed in the URL are:

    • The URL must start with nativebridge://

    • The functions name and parameters defined in the code should be valid for URL standards.

    • The parameter is needed to be decoded twice be in order to pass URL as a parameter

    The examples below can be used as a sample on how to pass parameters:

Examples:
nativebridge://quit  #no parameter
nativebridge://showToast/Toast%20Test! # parameter1 = Toast Test!
nativebridge://showAlertDialog/Alert%20Title/Alert%20Message # parameter1 = Alert Title , parameter2 = Alert Message
nativebridge://goUri/https%253A%252F%252Fgithub.com%252Fslm #parameter1 = https:/github.com/slm | url parameter decoded two times

For use in android:

      NativeBridgeBuilder builder = new NativeBridgeBuilder()
                .addFunction("showToast", new OnActionListener() {
                        @Override
                        public void onAction(NativeBridgeFunction function, String... parameters) {
                            Toast.makeText(MainActivity.this,parameters[0],Toast.LENGTH_SHORT).show();
                        }
                })
                .addFunction("quit", new OnActionListener() {
                    @Override
                    public void onAction(NativeBridgeFunction function,String... parameters) {
                        finish();
                    }
                });
      WebViewClient client = builder.buildWebViewClient();
      webView.setWebViewClient(client);

For use in custom WebViewClient:
Don’t worry we have coded it to work in custom webViewClient as well, just follow these steps:
...

final NativeBridgeBuilder.SimpleChecker checker =  builder.buildChecker();

#In WebViewClient
             ...
             ...
             @Override
             public boolean shouldOverrideUrlLoading(WebView view, String url) {
                 return checker.check(url);
             }
             ...
             ...

Gradle

For use, add it to your root build Gradle at the end of repositories:
  allprojects {
     repositories {
     ...
     maven { url "https://jitpack.io" }
    }
  }
Add the dependency
  dependencies {
         compile 'com.github.slmyldz:WebViewNativeBridge:1.0'
 }
These simple steps will help you in implementing the WebViewNativeBraidge in your code. Hope to have helped you through this awesome yet simple Library.


Download complete project source code from GitHub
WebView 4206229752873689699
Home item