31

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);
}

4
  • 4
    I'm not experienced enough to answer but have you read this: developer.android.com/guide/webapps/… Commented May 6, 2012 at 18:08
  • 1
    Ok I just didn't see anywhere where you enabled the javascript in the webview and though it was relevant. Commented May 6, 2012 at 18:13
  • u didnt get my question , i want to call an intent instead of a toast . i already made that toasts example working. thx anyway Commented May 6, 2012 at 18:50
  • I want to do it without any webview . just internally is it possible. Commented Apr 17, 2016 at 9:09

2 Answers 2

73

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...

Sign up to request clarification or add additional context in comments.

9 Comments

Thank you my friend . it almost helped me to solve my problem .
@Common i should take the above html code in separate html file ?
This is useful to call android functions from a javascript, but there is any way to call javascript functions from android code?
what is the location of this html file ?
I have one question, where to put the HTML page ?
|
18

You also need to add the @android.webkit.JavascriptInterface annotation on top of your changeActivity method in your android code, should you run on Android 4.2 or higher. See this link for more.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.