Load a div into a webview on Android - android

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.

Related

Why img tag has no src value after parsing with jsoup?

I Want to get src value from html img tag .
by chrome and inside of inspect element i can see value of src ,but when i parse it with jsoup library, src has no value , here's my code :
document = Jsoup.connect("http://estelam.rahvar120.ir/index.jsp?
pageid=2371666&p=1").userAgent(USERAGENT).method(Connection.Method.GET)
.execute().parse();
Element element = document.select("img[id=capimg]").first(); //img
tag element
String absoluteUrl = element.absUrl("src"); // absoluteUrl = ""
String srcValue = element.attr("src"); // srcValue = ""
the website isn't reachable from other countries, but where I want to parse from html is :
<img id="capimg" alt="Enter Captcha :"
src="" width="200" height="60">
The Problem is that jsoup get html content right before javascript set src value, What Should I Do ?
Welcome to SO!
The problem you are facing is not resolvable with Jsoup because Jsoup is a HTML parser not a browser. And since it's not a browser, any content rendered by javascript will not be rendered with Jsoup.
What you need is another tool that simulates web browser such as Selenium
There are multiple way to do this.
Use Selenium to handle page retrieval and scraping.
Use Selenium to get the dynamic pages and use JSoup to parse and scrape the content.
I personally recommend 2nd approach because I am more comfortable using Jsoup to scrape.

How to get editable web view content to string in Android.?

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"];

What is baseUrl in android web view?

In loadDataWithBaseURL method from Android WebView, there are "baseUrl" and "historyUrl".
What are they used for?
I have read the android documentation but still don't know what they are.
Loading HTML Into a WebView With a Base URL
If the HTML you load directly into the WebView in your Android web app contains links with relative URLs, then these links may not work correctly. When you load HTML directly into the WebView the HTML has no base URL from which to interpret the relative URLs. The Android WebView component has a solution for that.
You can load HTML directly into the WebView with a base URL. The base URL is then used to resolve all relative URLs in the HTML. To load HTML with a base URL you have to use the loadDataWithBaseURL() method. Here is a WebView loadDataWithBaseURL() example:
String baseUrl = "http://tutorials.jenkov.com";
String data = "Relative Link";
String mimeType = "text/html";
String encoding = "UTF-8";
String historyUrl = "http://tutorials.jenkov.com/jquery/index.html";
webView.loadDataWithBaseURL(baseUrl, data, mimeType, encoding, historyUrl);
The loadDataWithBaseURL() method takes 5 parameters. The data parameter is the HTML to load into the WebView. The mimeType is the mime type of the data loaded into the WebView (in this example text/html). The encoding is the binary encoding of the data (in this example UTF-8). Note: I tried using UTF-16 as encoding but the content displayed in the WebView looked pretty strange (like Asian characters).
The baseUrl parameter is the base URL from which all relative URLs in the loaded HTML is interpreted.
The historyUrl parameter is the URL to write into the WebView's internal navigation history for the HTML loaded into the WebView. If the user navigates from the loaded HTML to another page, and then clicks the "back" button, then it is this URL the WebView will navigate back to. You may have to intercept the loading of this URL, since navigating back the WebView's history will not take you to the loaded HTML, but to the URL specified in the historyUrl parameter (or about:blank if historyUrl is set to null).
For more information go through this tutorial and this stackoverflow answer.
What android document says :
Loads the given data into this WebView, using baseUrl as the base URL for the content.

Display local content on a WebView starting at a given anchor

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?

Android webview image fullscreen on click

I am working on a developing an Android application that displays news articles from a database through JSON. The article is in HTML format because the database is used for both web and the app. The code I have (below) works great. The format is the same on both web and phone when displayed in a webview, but I would like the images to be clickable, so they can can be loaded in a separate activity, and the user can zoom and such.
I guess I am just not using the proper wording when looking for an answer, because I cannot find anything that relates to this. I am assuming I would have to find the tags on click and capture the url somehow, and then pass it to another activity. I am not sure if this is the best way to do this or not. Any insight on this would be greatly appreciated.
web = (WebView) findViewById(R.id.WebView01);
final String mimeType = "text/html";
final String encoding = "UTF-8";
web.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
web.setScrollbarFadingEnabled(false);
web.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
web.loadDataWithBaseURL("", product.getString(TAG_CONTENT), mimeType, encoding, "");
This code runs within a Async task that queries the database for info.
Since it is HTML, you can use the onclick attribute of the img tag
<img src="myimage.png" onclick="javascript:window.location=this.src;" />
This will open the image up as the current window.
Or you can do something similar to this answer and send the URL to another activity.

Categories

Resources