Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display a part of the webpage on the webview android

I want to make an app which loads the content from the webpage into webview. I want to show only a particular thing in the entire webview, not the whole content of the webpage.

Here is an example. If I use: http://us.m.yahoo.com/w/search%3B_ylt=A2KL8xs0vUBQMg0AwAkp89w4?submit=oneSearch&.intl=us&.lang=en&.tsrc=yahoo&.sep=fp&p=digital+cameras&x=0&y=0 as the URL for the webview, it loads all the contents of the page on the webview. But I want to remove the banner of the page and show it on the webview of my application.

I have tried using adblocker using CSS tags, but that is not helping me. Please give me some idea for overcoming this problem.

Thanks.

like image 539
Anupam Avatar asked Sep 04 '12 06:09

Anupam


People also ask

How do I display HTML content in WebView?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken web view to show html content.

Which of the following can you use to display an HTML Web page in an Android application?

Android WebView is used to display HTML in an android app. We can use android WebView to load HTML page into android app.

How do I enable JavaScript on WebView?

Enable JavaScript JavaScript is disabled in a WebView by default. You can enable it through the WebSettings attached to your WebView . You can retrieve WebSettings with getSettings() , then enable JavaScript with setJavaScriptEnabled() . WebView myWebView = (WebView) findViewById(R.

What is WebView component?

Android System WebView is a system component for the Android operating system (OS) that enables Android apps to display web content directly inside an application.


2 Answers

Thank You for the answer Zyber. I have solved it using injection of JavaScript in the code for WebView in android.

final WebView webview = (WebView)findViewById(R.id.browser);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient() {
 @Override
public void onPageFinished(WebView view, String url)
{
    webview.loadUrl("javascript:(function() { " +
            "document.getElementsByTagName('header')[0].style.display="none"; " +
            "})()");
}
});
webview.loadUrl("http://code.google.com/android");

This solved my purpose and it is easy to use to.

like image 74
Anupam Avatar answered Oct 31 '22 23:10

Anupam


check Jsoup it provides an library which gives an easy way of extracting Html elements from a webpage

DefaultHttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url.toURI());
HttpResponse resp = client.execute(get);

String content = EntityUtils.toString(resp.getEntity());
Document doc = Jsoup.parse(content);
Elements ele = doc.select("div.classname");

This example executes an Http GET and then extracts an Div element with the class "classname" which you can then load in your webview

like image 37
Zyber Avatar answered Oct 31 '22 22:10

Zyber