Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using javascript in android webview

I'm trying to start an activity from a javascript interface in my webview. The example shows a toast. How could i call a class instead of a toast?

public class JavaScriptInterface { Context mContext;  /** Instantiate the interface and set the context */ JavaScriptInterface(Context c) {     mContext = c; }  /** Show a toast from the web page */ public void showToast(String toast) {     Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show(); } } 

this for the html page.

<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />  <script type="text/javascript"> function showAndroidToast(toast) {     Android.showToast(toast); } 

like image 755
Common Avatar asked May 06 '12 17:05

Common


People also ask

Can WebView run JavaScript?

WebView is a special component in Android which serves as kind of built-in browser inside Android applications. If you want to execute HTML, CSS or JavaScript code in your Android app, or you need to allow users visit a URL without leaving your application, WebView is the way to go.

Can I run JavaScript on Android?

We can execute Js function using Android. But only logic code. We cannot do any UI changes kind of things(as I know) using this libraries. The library which we are using to execute JS is Rhino.

How do you communicate between WebView and native Android?

2.1 To receive data from webview ,we can create an interface, which will enable webview to connect the native layer and pass data. From native layer, create a class and replicate the following. While configuring web view, we need to set JavaScript interface as above JSBridge class.

Is Android WebView deprecated?

This interface was deprecated in API level 12. This interface is now obsolete.


Video Answer


1 Answers

You have to first register the JavaScriptInterface on your webview. JavaScriptInterFace can be a inner class as shown below. This class will have a function that you can call from html page( via javaScript ) and inside this function you can write code to change activity.

Here is the working solution for you:

public class JavascriptInterfaceActivity extends Activity {     /** Called when the activity is first created. */       WebView wv;      JavaScriptInterface JSInterface;      @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);         wv = (WebView)findViewById(R.id.webView1);          wv.getSettings().setJavaScriptEnabled(true);         // register class containing methods to be exposed to JavaScript          JSInterface = new JavaScriptInterface(this);         wv.addJavascriptInterface(JSInterface, "JSInterface");           wv.loadUrl("file:///android_asset/myPage.html");      }       public class JavaScriptInterface {         Context mContext;          /** Instantiate the interface and set the context */         JavaScriptInterface(Context c) {             mContext = c;         }          @android.webkit.JavascriptInterface         public void changeActivity()         {             Intent i = new Intent(JavascriptInterfaceActivity.this, nextActivity.class);             startActivity(i);             finish();         }     } } 

Here is the html page

<html> <head> <script type="text/javascript"> function displaymessage() { JSInterface.changeActivity(); } </script> </head>  <body> <form> <input type="button" value="Click me!" onclick="displaymessage()" /> </form> </body> </html> 

Hope this helps...

like image 69
Akshay Avatar answered Sep 28 '22 11:09

Akshay