I want to display my data's in a html page, I make a static html page in assest folder .but don't know how to insert my data's in that html page programmatically.
You'll need to have a WebView in your activity. Then load the html into the webview, here are some instructions.
Loading an html page from an asset is relatively simple:
WebView webView = (WebView)view.findViewById(R.id.myWebView);
webView.loadUrl("file:///android_asset/index.html");
But to programatically alter the content of the page you'd have to first read the asset file into a string, insert your data with a string replace, or use java.text.MessageFormat.
Here's the code to get an inputstream from an asset:
AssetManager mgr = this.getAssets();
InputStream is = mgr.open("index.html");
BufferedInputStream in = new BufferedInputStream(is);
// read the contents of the file
Once you have your html string populated, you can programmatically set the content of the webview:
webView.loadData(htmlString, "text/html", "UTF-8");
Related
I have String with html format, not url, that send through from api. Those include image and bootstrap css. I would like to download those things to show in offline including image and css to show right format.
Is there any library to download html string content?
All You Have to do is get your Json Data convert Json to String json.toString();
and pass it to data=json.String();
and check if data is already stored or not in Shared Preferences
DataBaseHelper DB;
if(DB.getHTMLStoredData(context)){
data=DB.getHTMLStoredData(context)
}else{
data=json.String();
}
//data == html data which you want to load
WebView webview = (WebView)this.findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadDataWithBaseURL("", data, "text/html", "UTF-8", "");
I want to implement a Rich text editor by using a webview in android. Content can be loaded by using a HTML file (which resides in assets) without any problem. But if user has edited the content of the webview (with all the formatting), I need to get the modified content as a HTML string and save it in the database. How can I do this?
I tried in many ways but it seems that we need to pass a URL to get the content of the webview. But after editing the webview content, how can we get the edited URL? or current updated webview content to HTML formatted string?
Using below code I made editable web view.
String msgBody = "<html>\n"+
"<body>\n"+
"<div id=\"content\" contenteditable=\"true\" style=\"font-family:Helvetica;font-size:14px\">" + a +" </div>\n"+
"</body>"+
"</html>";
// wbview = (WebView)this.findViewById(R.id.wbview);
wbView.getSettings().setJavaScriptEnabled(true);
wbView.loadDataWithBaseURL("", msgBody, "text/html", "UTF-8", "");
wbView.setHorizontalScrollBarEnabled(true);
wbView.setVerticalScrollBarEnabled(true);
In iOS we can get it easily by using below code line.
NSString* html=[_tbEmail.webView stringByEvaluatingJavaScriptFromString:#"document.getElementsByTagName('body')[0].innerHTML"];
I'm displaying an HTML document using a WebView. The HTML document is contained in an HTML string. I pass the HTML string to the WebView by using its loadDataWithBaseURL() method, as follows (I got it from this answer):
webView.loadDataWithBaseURL("fake://not/needed", getResources().getString(R.string.htmlString), "text/html", "utf-8", "");
Now, I'd like to show the HTML document contained in that HTML string starting not at the document's beginning, but at a given HTML anchor contained in the HTML string.
Can I do that with webView.loadDataWithBaseURL()? Or more gnerally: is there any method to show local content on a WebView starting at an anchor?
I'm using a WebView to display a html page which is stored in the \raw\ directory so I can easily localize it.
I load the page using
InputStream inputStream = getResources().openRawResource(R.id.htmlPage1);
StringWriter writer = new StringWriter();
try {
IOUtils.copy(inputStream, writer);
} catch (IOException e) {
e.printStackTrace();
}
String x = writer.toString();
WebView w = (WebView) myFragmentView.findViewById(R.id.webView);
w.loadDataWithBaseURL("file:///android_asset/", x, "text/html", "utf-8", null);
which works fine. Images and css files used in the html page are stored in the \assets\ directory.
Now I want to add links to another local page (< a href="htmlPage2.html">Click here< /a>) in my html page. The WebView tries to load the page from assets (which seems to be the correct behavior). Unfortunately the \assets\ folder isn't localized.
How can I link to other local html pages without giving up the feature of localization?
Thanks!
Step #1: Probably use a more distinctive URL pattern for the links to help you distinguish them (e.g., <a href="/this/is/another/raw/resource/htmlPage2.html">)
Step #2: Register a WebViewClient with your WebView
Step #3: In shouldOverrideUrlLoading() of your WebViewClient, if the URL matches the pattern you use for these links (see Step #1), go through your load-the-resource logic and return true, else do nothing and return false to pop up a Web browser on that URL
I have an Android app, which contains a WebView, and I would like to display in it not a webpage, but only a div from that webpage. I should mention that I do not have access to that page.
I would recommend Jsoup. It's larger than tagsoup but provides inbuilt functionality to pull the HTML from the URL and is super easy to use. For example if you want to pull a div with id example you would do the following:
Document doc = Jsoup.connect(url).get();
Elements ele = doc.select("div#example");
Then to load the HTML you've extracted into your web view you would do:
String html = ele.toString();
String mime = "text/html";
String encoding = "utf-8";
webView.loadData(html, mime, encoding);
You'll need to load the HTML of the page yourself, extract the div contents and pass it to the WebView as a string. You may find an HTML parser library (e.g. tagsoup) useful for this.