2

I have tried this code and stuck form many days.
Actually waht i want is to integrate mathjax library for mathametics formula and to get output form it.

 <html>
<head>
<style>
body{
background-color: #FA5858;
color:#fff;
}
input{
background-color: #F7D358;
width: 300px;
padding:10px;
color: #000;
}
div#content{
padding:20px;
background-color: #F7D358;
color: #000;
}
</style>
<script type="text/javascript"  src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">

    function showAndroidToast(toastmsg) {
        var x= Android.showToast(toastmsg);
        return x;
    }

function testcall(toastmsg){
 alert(showAndroidToast(toastmsg));
}
</script>
</head>
<body>
<center>
<h3>Binding JavaScript code to Android code</h3>
<div id="content">

</div>
<div>
Here are few examples: 
When \(a \ne 0\), there are two solutions to \(ax^2 + bx + c = 0\) and they are
$$x = {-b \pm \sqrt{b^2-4ac} \over 2a}.$$
</div>
<div>
<input type="button" value="Make Toast" onClick="testcall('Toast made by Javascript New:)')" /><br/>

</div>
</center>
</body>
</html>

And this in my android code

    public class WebAppInterface {
    Context mContext;

    /** Instantiate the interface and set the context */
    WebAppInterface(Context c) {
        mContext = c;
    }

    /**
     * Show Toast Message
     * @param toast
     */
    public String showToast(String toast) {
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
        String test="\\hspace1ex \\color{blue} {Simplify:} \\hspace1ex 2x(3x+2xy)";
        return test;
    }
    }

But my android code doesn't return any vaule to javascript function.
Any help is appreciated.

Like ,

this is my android function

   public String showToast() {

    String test="\\hspace1ex \\color{blue} {Simplify:} \\hspace1ex 2x(3x+2xy)";
    return test;
}

and in javascript i want the value that is return by this showToast() function

Thanks

2
  • How do you init your Webview? Please post a little bit more code :) Commented Apr 2, 2015 at 10:06
  • Hello, I'm with the same problem, did you solved it? Commented Feb 5, 2020 at 12:36

1 Answer 1

1

This is what i did in an App:

Set up the Webview:

  final WebView webView = new WebView(context);
  webView.getSettings().setJavaScriptEnabled(true);
  webView.addJavascriptInterface(new WebViewInterface(), "Android");
  webView.loadUrl("file:///android_asset/web/page.html");

with this WebViewInterface implementation:

import android.webkit.JavascriptInterface;

public class WebViewInterface {

    @JavascriptInterface
    public void log(String text) {
       Log.d("TAG", text);
    }
}

The page.html is located in the assets/web folder and looks like this:

<!DOCTYPE html>

<html>
<head>
    <script language="javascript" src="script.js">  </script>
</head>
<body>
</body>
</html>

The script.js file is located in the assets/web folder too:

function logText(text) {
    Android.log(text);
}

function toBeCalledFromAndroid(text) {
    // Do something in javascript
}

The logText function from javascript calls the Navive log method in Android.

To call a Javascript function from native Android code you have to do the following:

String loadingUrl = "javascript:toBeCalledFromAndroid('" + text + "')";
webView.loadUrl(loadingUrl);
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your reply .. But what i want is to pass string form WebAppInterface function and get its value in javascript
@ PaMADo u misunderstand my question..can u tell me how i ll get string form interface to javascript? have a look i have updated my question..
This should show a toast in your example: String loadingUrl = "javascript:showAndroidToast('" + text + "')"; webView.loadUrl(loadingUrl);
This could help you: codelixir.com/…
Thank .. i ll go through it.. and let u know

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.