Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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.</p> <p>Here is the working solution for you:</p> <pre><code>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(); } } } </code></pre> <p>Here is the html page</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;script type="text/javascript"&gt; function displaymessage() { JSInterface.changeActivity(); } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form&gt; &lt;input type="button" value="Click me!" onclick="displaymessage()" /&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Hope this helps...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload