I am loading my android WebView using
mywebview.loadDataWithBaseURL("file:///android_asset/", new String(result), "text/html", "UTF-8", null);
HTML rendered successfully in WebView. now I want to open another HTML file from that HTML using button for that I am using below code in HTML.
<button onclick="location.href='data/1/quiz_adjective.html'" > Start</button>
but it does not work. my asset html file directory is - assets\data\1\htmlfile.html
When you make Android applications, you can parse HTML data or HTML pages got from the Web by JSoup libraly.
Before loading url you should enable JS like this. Then, It should work. Or there is a more advanced way with JavascriptInterfacedocs
mywebview.getSettings().setJavaScriptEnabled(true);
Related
I have a webview showing a local html page resource in a string like this:
<p>Hi.</p>
<p><img alt="" src="http://ADRESS.COM/image.jpg" style="height:300px; width:400px" /></p>
and doing that with this code:
WebView web = (WebView) findViewById(R.id.web);
web.getSettings().setJavaScriptEnabled(true);
web.loadDataWithBaseURL("",notice,"text/html","UTF-8","");
The page contains some images with online src. but in android WebView the images doesn't load. (in chrome it works).
What I should do?
thanks.
Add these two lines in your code.
web.getSettings().setLoadsImagesAutomatically(true);
web.getSettings().setDomStorageEnabled(true);
Instead of using web.loadDataWithBaseURL("",notice,"text/html","UTF-8",""); simply load your website url as follows
web.loadUrl("http://www.google.com");
if you want load your local html page. Create asset directory android then put your html file in asset folder and and set it as follows
web.loadUrl("file:///android_asset/yourFile.html");
so i'm trying to load some HTML from my sqlite database into a webview.
This works splendid as the html is rendered beautifully but here comes the problem. The local file added in the html is not being load. I added it to the assets folder in this hierarchy: assets/folder1/folder2/placeholder.png but i get a Not allowed to load local resource: error. How can i get past this please.
Please note that the data is being populated from the db.
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setAllowFileAccess(true);
webView.loadData(data, "text/html", "UTF-8");
That's the code for the web view above.
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><h2 style="color:red;">Hello Chief</h2><p>This is just a demo HTML content rendered beautifully</p><br><img src="file:///android_asset/folder1/folder2/placeholder.png"></body></html>
That's code above is for the data coming from the sqlite database.
My Assests folder looks something like this
-assets
- folder1
- folder2
-file
Thanks
There is one method in webview to load html file with base url
webview.loadDataWithBaseURL(String baseUrl, String data, String mimeType, String encoding, String historyUrl)
In baseUrl give it as
file:///android_asset/
Or else parse the html document with Jsoup and replace the src of required tags using Jsoup Parser.
working on a native Android app. I am able to load a local file index.html into a WebView:
The web url loads fine. Now, I would like to load the web with some parameters, in the same way one types this in the browser:
So I can get those values inside the javascript of the html file. Is there any way to send parameters to a local html file?
html:
<input type="hidden" name="deviceid" id="deviceid"/>
i pass the device_id on local html file.
You can send them as get parameters while loading your html in WebView itself, and then later catch it in your JavaScript using window.location.href property. Something like this:
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("/assets/index.html?foo=bar");
edit
To send the Device-ID, use the below code instead:
TelephonyManager tm=(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
String did = tm.getDeviceId();
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("/assets/index.html?DeviceID=" + did);
I am trying to show some basic html elements in a webview. I can do it with load datawithbaseurl method. But is there any way to check if content loaded correctly ?
Here is my html code coming from webservice.
<div style="float:left;margin:0;padding:0;top:0;bottom:0"><img src="http://exam.exam.com/upload/images/Activities/poskbhov.jpg" alt="" /></div>
and my webview code :
webView.loadDataWithBaseURL("", content.Content, "text/html", "UTF-8", "");
I just want to show an image. But i want to make sure its loaded. Right now i dont have an image. WebView just shows an img object in a div right now. I would like to put this image can not shown alert.
To link an image, build an String (HTML format) and code an IMG element:
String htmlContent = "<HTML>.....<img id=\"main_image\" src=yourImage.png alt=\"\"></br>.....</HTML>");
Load the content telling the webView were your resources are, and put your IMG in your "assets" folder with the name "yourImage.png"
webView.loadDataWithBaseURL("file:///android_asset/", htmlContent, "text/html", "utf-8", null);
You can also Link css file from here too!
If you want to see a full example, take a look to this project:
https://github.com/matessoftwaresolutions/AndroidHttpRestService
It consists on an HTTPService and, in the class HttpServiceFragment you can see a full (specific) implementation.
I hope it helps!!!
I am trying to load a local javascript file from a webview.
The file "search.js" is located under the assets folder in my project.
I used this line to load my webpage :
mWebView.loadDataWithBaseURL("file:///android_asset/", data, "text/html", "utf-8", null);
on my webpage, I am using this line :
<script type="text/javascript" src="search.js"></script>
to load my javascript file.
When I run the program, I just get a white page..............
I don't know what to do. Do you have any ideas ?
Thanks !!
Try this:
wv.loadUrl("file:///android_asset/yourfile.html");
wv.getSettings().setJavaScriptEnabled(true);