Skip to content

mdminhazulhaque/Android-JavascriptInterface-Sample

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Android-JavascriptInterface-Sample

Call Java (Android) methods from Webview using Javascript

How To

  1. Create a class named JSInterface inside any Activy or other Intent

  2. Create a class with public methods with @JavascriptInterface annotations (Not @JavaScriptInterface)

        public class JSInterface {
        ...
        @JavascriptInterface
        public void toastMe(String text) {
            Toast.makeText(mContext, text, Toast.LENGTH_SHORT).show();
        }
    }
  3. Attach the class name as the Webview's JavascriptInterface.

    webview.addJavascriptInterface(new JSInterface(this), "JSInterface");
  4. Call JSInterface.toastMe(text) from Webview's Javascript instance

    var text = "I want to be toasted";
    JSInterface.toastMe(text);

Screenshot

Toast Notification

About

Call Java (Android) methods from Webview using Javascript

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages