Hey there Stackoverflow!
I’m trying to create an application that uses a WebView to display a JavaScript widget but I seem to have hit a wall in which I cannot get the widget to display at all. I’ve done a fair amount of research and I think what I’m trying to do is possible.
This is my main activity
public class WebViewAppTestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//grab the webview
WebView webview = (WebView) findViewById(R.id.webview);
//grab the webview settings
WebSettings websettings = webview.getSettings();
//enable javascript
websettings.setJavaScriptEnabled(true);
//add a js interface to display the widgets
webview.addJavascriptInterface(new JavaScriptInterface(this), "Android");
//load a webpage
webview.loadUrl("file:///android_asset/javascript/index.html");
}
}
It loads the file index.html which looks like this
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width; user-scalable=0;" />
<title>WebView test!</title>
</head>
<body>
<h1>MyHTML</h1>
<div id="panel">
<section class="episode"></section>
</div>
<script src="https://sourceurl1.js" />
<script src="https://sourceurl2.js" />
<script src="https://sourceurl3.js" />
<script src="https://sourceurl4.js" />
<script src="./widget.js" />
<script src="./top-episodes.js" />
<script>
var TopEpisodes = new Company.TopEpisodes({
$element: $('#panel section.episodes')
});
TopEpisodes.init({
mode : 'realtime',
range : { from: 1341911040, to: 1341907200 },
path : ["4FB159EB613033AA710006DF"],
isEpisode : true
});
</script>
</html>
</body>
</html>
The only thing that displays when I test my application (Nexus S 4G ICS 4.0.4) is the MyHTML header on a blank white screen. Internet access is enabled in the xml.
Is there something else I need to enable for the WebView settings, or does something look really wrong with my HTML file? Do I need to do something with the addJavaScriptInterface method/within a JS interface?
Anyways I am very stuck and open to any suggestions! Thank you for your time!
EDIT There haven’t been any response on this question for a couple days, let me know if there is anything I can do to clarify this question or any other details that would help! Or if it is even possible. Thank you!
So I’ve decided to go a different direction with my application due to my lack of experience with both HTML and Javascript.
I am instead going to develop the widget natively in Java using this chart library: http://achartengine.org/ and then update the data using an HttpClient rather than creating it within a WebView in Javascript, because I think that this solution will be easier and quicker for an Android application.
Thank you all for your time!