Display certain contents of a website into webview - android

I'm new to Android programming.
I would like to know if I can load a certain part of a website into webview? The website is made from CSS. It contains headers and buttons that I do not want to be displayed into the webview. I would only like to display the contents in the website, like images and texts. Is this possible? If so, how?
Many thanks in advance!!!

You can use the public void loadDataWithBaseURL (String baseUrl, String data, String mimeType, String encoding, String historyUrl) method to load a customised html, maybe your app can get the html from the site, modify it, and loadDataWithBaseUrl then.

Related

Reading web and replace some words

I want to read web pages (not mine and specified) and replace some words with other words in the web page loaded.
For example, let's suppose that there is a webpage that shows string "Hello, World! 07-17-2015". And I want to replace all "07" with "08". Then this page will be shown like this: "Hello, World! 08-17-2015". (There is only a string in this example, but I want to execute at any page)
I want to do this with Android. Can I make an app with this feature?
Just make a request to get the HTML content of the page (bazillions methods to do it, lots and lots of libraries, or plain HttpURLConnection).
Then take that output as String and replace what you want:
// Your network implementation
String htmlContent = response.data.replaceAll("07", "08");
Then use a WebView and load this String inside it:
webView.loadData(htmlContent, "utf-8");

Android : How to get HTML content in String from WebView

I just want to get the HTML String from my webview for that i am trying with the following code
webviewTxt.loadUrl("javascript:HTMLOUT.processHTML(document.documentElement.outerHTML);");
But i don't know how can i get this code to string. I even don't know whether this is true code to grab the HTML String or not.
In my webview i have following String
This is a test Demo
Where "test" is written in Italic and "Demo" is written in Bold So i want to get the HTML string from above code.
Above string is just an Example, The string may be anything not the same as above string each time.
If anyone is having any idea then please guide me, I already checked so many link links from SO and other google stuff but i am not able to grab the String in HTML from Webview.

White Spaces are removed from string when loading in web view in android

I want to load string in webview , below is the String :-
Hi,
Hw are u ?
Thanks,
When i use loadData of web view it displays the string in single line , spaces are removed.
I want it in same format , hw is it possible ?
If you are using a webview, you should format the string into html.
I think that you already have a html file., if that is the case you only need put a format in the file.
Hi,<br>
Hw are u ?<br>

Json parsing converts html tags to escape sequence

I am fetching few html content from my server for which I am using JSON parsing. But this converts my html content to unicode values.
For Eg: <p>Spend minimum $10 (in a single same-day receipt) at any outlet<\/p> is getting converted to,
;p>Spend minimum $10 (in a single same-day receipt) at any outlet </p>
Now if I try to set this to my WebView it displays with HTML tags itself. If I try to encode the data using TextUtils.encode it displays the text with unicode values.
Can anyone help me with this.
How should I fetch a HTML content and display it in WebView?
I am not getting your question exactly but, If you want to load HTML in web view in you can use
webView.loadDataWithBaseURL(null, html, "text/html", "UTF-8", null);
and if you want to convert &lt and &gt like notation you can use Jsoup Library
Guys thanks for your help. But I have solved this issue myself. I have elaborated my way of solving the issue.
What I did is,
1)convert the unicode value to Spanned like this,
Spanned ss=Html.fromHtml(;p>Spend minimum $10 (in a single same-day receipt) at any outlet </p>");
2)Now convert this Spanned to String like this,
String tempString=ss.toString();
3)And now set this to WebView which solved the problem,
webView.loadData(tempString, "text/html","UTF-8");
Actually this isn't JSON encoder converts data to HTML entities but some other layer, before it passed to JSON encoder.
JSON have nothing to do with HTML tags, usually only quotes encoded by parser (Unicode is supported by most parsers).
You probably need to change the way data is returned by server, to omit encoding of HTML tags braces to HTML entities or decoding entities backin your app.
Update:
To decode HTML entities used in HTML tags (and others too) you may use StringEscapeUtils.unescapeHTML()
To show the HTML page inside the Webview why you require the JSON. create web view inside the XML and write below code Inside the Activity you can see the HTML page.
webView = (WebView)findViewById(R.id.webView);
FrameLayout mContentView = (FrameLayout) getWindow().
getDecorView().findViewById(android.R.id.content);
final View zoom = this.webView.getZoomControls();
mContentView.addView(zoom, ZOOM_PARAMS);
zoom.setVisibility(View.GONE);
webView.loadUrl("http://www.google.co.in/");

WebView.loadDataWithBaseUrl doesn't show images

I have an HTML string of data that i parse from an ATOM Feed. Obviously there are references to images inside, that I'd like to show on a WebView. But using
webview.loadDataWithBaseURL("", data,"text/html", "utf-8","");
only the text is displayed and the images are not shown.
What I am missing?
Thank you in advance.
You are not passing the BASE URL, you are passing the emptry string. As you can see in the docs loadDataWithBaseURL() is expecting as the first parameter the Base URL of your Html Data. So what you should have is something like this :
webview.loadDataWithBaseURL("www.mypage.html",data,"text/html","utf-8",null);

Categories

Resources