Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android webview +javascript not showing output in android 4.0.x,3.x

Tags:

This is my main activity

package com.example.mathjax_issues;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.Menu;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {


    private WebView webView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webView=(WebView)findViewById(R.id.webView1);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new MyWebViewClient());
        webView.loadUrl("file:///android_asset/MathJax/test/sample-asciimath.html");
//      String html="<!DOCTYPE html> <html> <head> <title>MathJax AsciiMath Test Page</title> <!-- Copyright (c) 2012-2013 The MathJax Consortium --> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" /> <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\" /> <script type=\"text/javascript\" src=\"file:///android_asset/MathJax/MathJax.js?config=AM_HTMLorMML-full\"></script> </head> <body> <p> When `a != 0`, there are two solutions to `ax^2 + bx + c = 0` and they are </p> <p style=\"text-align:center\"> `x = (-b +- sqrt(b^2-4ac))/(2a) .` </p> </body> </html> ";
//      Log.e("html",html);
//      webView.loadDataWithBaseURL("file:///android_asset/MathJax", html, "text/html","utf-8", "");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    private class MyWebViewClient extends WebViewClient
    {

        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);
        }




    }



}

This my sample-asciimath.html

<!DOCTYPE html>
<html>
<head>
<title>MathJax AsciiMath Test Page</title>
<!-- Copyright (c) 2012-2013 The MathJax Consortium -->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />

<script type="text/javascript" src="../MathJax.js?config=AM_HTMLorMML-full"></script>

</head>
<body>

<p>
When `a != 0`, there are two solutions to `ax^2 + bx + c = 0` and they are
</p>
<p style="text-align:center">
`x = (-b +- sqrt(b^2-4ac))/(2a) .`
</p>

</body>
</html>

below is the image of my project

enter image description here

when compiling this code in 2.3.3,2.2,4.1.2,4.2.2 latest versions

I get the correct output from the source

enter image description here

but when i compiled the same source in 3.0,3.1 and 4.0.3,4.0.4 android version

I am getting the wrong output like this

enter image description here

Friends pls help me ... I am unable to solve this error ... I think there may be a javascript problem.Hope you guys give me some idea

I am storing the mathjax locally .. in assets

Thanks in advance ...

like image 423
Venkatesh S Avatar asked Jun 29 '13 04:06

Venkatesh S


People also ask

How do I enable JavaScript on Android WebView?

JavaScript is disabled in a WebView by default. You can enable it through the WebSettings attached to your WebView . You can retrieve WebSettings with getSettings() , then enable JavaScript with setJavaScriptEnabled() . WebView myWebView = (WebView) findViewById(R.

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.

Is WebView deprecated in Android?

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

How do I get data from WebView?

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


2 Answers

I had a similar problem. What I did was I copied all the .js files in the same directory as where the html is and added it as follows.

<script type="text/javascript" src="MathJax.js"></script>

I know it sounds a bit stupid, but it worked.

If that doesn't work, copy the .js file into the html file itself and try it like in the header:

<script type="text/javascript">
copy the whole MathJax.js and paste it here
</script>
like image 67
Navin Avatar answered Sep 20 '22 19:09

Navin


Some people already had this problem (actually... a lot of them...).

One post (and also one answer) got my attention : https://stackoverflow.com/a/7197748/1387484

It appears you have to inject your Javascript call in the WebViewClient manually, after the document was fully loaded. I doesn't have more explanation but maybe you can try this way !

like image 45
mithrop Avatar answered Sep 18 '22 19:09

mithrop