Android WebView loading part of a website - android

Some websites have comments portion and many other unnecessary portions. I want to just display the main article excluding the other portions in my custom Android WebView.
I have tried the example from Display a part of the webpage on the webview android
But it still shows the full website.
There is also the loaddatawithbaseurl class in Android but it is not working too as you can't specify till which portion of a website to show using this class.
Is there any other way to do this?

Here it is with some changes...
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url)
{
String javaScript = "javascript: var form = document.getElementsByClassName('university_corner');"
+ "var body = document.getElementsByTagName('div');"
+ "body[0].innerHTML = form[0].innerHTML";
webView.loadUrl(javaScript);
}
});
webView.loadUrl(url);

Related

Display a pdf with dynamic generating url in WebView / Google Docs

so I just started with Android programming and I am trying to make a little app using WebView. There is a url that redirects you to a pdf, I know WebView does not render pdf. So I want to use intent and display the pdf in Google Docs. However, the pdf address is randomly generated so I cant link it with
WebView.loadUrl("http://docs.google.com/gview?embedded=true&url=" + pdfURL);
How can I send an intent to Google Docs without using the exact pdf address?
I don't know what "randomly generated" means.
But the first thing that comes to my mind is to set a WebViewClient and override shouldOverrideUrlLoading:
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.endsWith(".pdf") == true) {
view.loadUrl("http://docs.google.com/gview?embedded=true&url=" + url);
return true;
}
return false;
}
});
Some more info in this thread.

Images not loading in android webview

I am trying to load the site in an android app web view.
The site loads without the images ,all the images from the site are not loaded what could be the problem.
The code for onCreate is shown below.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
String url = getResources().getString(R.string.web_url);
web = (WebView) findViewById(R.id.webview01);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
web.getSettings().setUseWideViewPort(true);
web.loadUrl(url);
}
One more note, when i set javascript to false using web.getSettings().setJavaScriptEnabled(false); the images load giving a warning that javascript should be enabled to run the site properly.
This site is protected with CloudFlare ,could this be the reason images are not loaded in the android web view ?
Add
web.getSettings().setDomStorageEnabled(true);
This should work. Keep the rest as is.
Although a caveat, I don't think its recommended to enable this by default, its disabled by default. The setting allows a website to store data and reuse it. So the images are not being loaded, because this site wasn't allowed to store them on your device. This opens up a can of security concerns.
Just use this line of code. I think your problem will be solved.
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
You may need this one as well, if you are lazy-loading content:
webView.getSettings().setJavaScriptEnabled(true);
Just this will suffice
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
I had similar problem in Android 9.0. The images in the site's html were using http instead of https.
Then I changed all the http with https and everything worked!
It was very easy to change the http to https using a sql query in mySql.
I am giving the query if it helps anyone!
UPDATE table_name SET column_name = replace(column_name, '<img src="http://', '<img src="https://')
I suspect that the issue might be your webview client.
Use this instead:
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i(TAG, "Processing webview url click...");
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
Log.i(TAG, "Finished loading URL: " +url);
if (pDialog.isShowing()) {
pDialog.dismiss();
}
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.e(TAG, "Error: " + description);
Toast.makeText(FundCardWeb.this, "Page Load Error" + description, Toast.LENGTH_SHORT).show();
}
});
for me the combination of adding those did it:
webSettings.setDomStorageEnabled(true);
webSettings.setAppCacheEnabled(true);
webSettings.setLoadsImagesAutomatically(true);
I don't like adding unneccesery cod when so I didn't use:
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
I have faced the same issue. In the web view, the Image with the http URL was not loading in my app. The following solution fixed my issue,
webview.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
But in Android 9, the above solution has thrown the issue sometimes, in such case add the following line in the manifest file.
android:usesCleartextTraffic="true"
My problem was just one image, not loading in webview. I just add this two thing
In Manifest
android:usesCleartextTraffic="true"
In java oncreate() just add
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mywebView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}

Android - Image Loading from WebPage in WebView

I am trying to load a wiki web page into a native Android WebView with WebView.loadUrl(String url). It works and displays all of the web content, except for the images. However, I have not found a reason for this. Does anyone know why image resources would not load automatically? Is there a method I need to overwrite or a parameter that needs to be set? I can't find anything in the documentation - it looks like the default settings should allow for automatic image loading. Any information would be appreciated. Thanks!
mWebView = (WebView)findViewById(R.id.webView);
setWebViewClient(mWebView);
setWebSettings(mWebView);
mWebView.loadUrl("http://my_wiki_url_goes_here/");
private void setWebViewClient(WebView mWebView) {
WebViewClient client = new WikiWebViewClient();
mWebView.setWebViewClient(client);
}
private void setWebSettings(WebView mWebView) {
WebSettings settings = mWebView.getSettings();
settings.setJavaScriptEnabled(true);
}
public class WikiWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
}
Printing the url onLoadResource(), I see that the image urls generally start with "data:image/png;base64,". I don't think that should makes a difference or not - it works in the web browser.
Huh, looks like simply enabling DOM storage did the trick.
settings.setDomStorageEnabled(true);
If I don't receive a better explanation or solution, I will accept this answer once I am able to

Force WebView Video Controls

I have an Android app that plays embedded html5 videos. The html code that I receive does not include the "controls" attribute in the video tag. I'm wondering if there is a way to force the controls to be shown while the video is in the WebView. In 2.3 you can use WebChromeClient to display the full video. However in 4.x it appears you need to show the full controls first in order to take advantage of this class.
You could add the controls attribute:
webView.getSettings().setJavascriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView webView, String url) {
super.onPageFinished(webView, url);
webView.loadUrl(
"javascript:(function() {" +
"var video = document.getElementsByTagName('video')[0];" +
"video.controls = 'controls';" +
"})()"
);
}
});

Android Webview Anchor Link (Jump link) not working

I have a WebView in my Android App that is loading an HTML string using the loadDataWithBaseURL() method. The problem is that local anchor links (<a href="#link">...) are not working correctly. When the link is clicked, it becomes highlighted, but does not scroll to the corresponding anchor.
This also does not work if I use the WebView's loadUrl() method to load a page that contains anchor links. However, if I load the same URL in the browser, the anchor links do work.
Is there any special handling required to get these to work for a WebView?
I am using API v4 (1.6).
There isn't much to the code, here are the relevant parts of some test code I've been working with:
WebView detailBody = (WebView) findViewById(R.id.article_detail_body);
String s = "LINK!<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><a name=\"link\"></a>Testing!";
detailBody.loadDataWithBaseURL(API.HomeURL(this), s, "text/html", "utf-8", "");
It looks like the problem is that I had a WebView within a ScrollView. The WebView isn't able to scroll to an anchor link when configured like this. After refactoring my layout to eliminate the ScrollView, the anchor links work correctly.
Android Webview Anchor Link (Jump link) Not Working
True, WebView Anchor Links, or Jump Links initiated through the #LINK extension to the URL will not work when the WebView is inside of a ScrollView(*).
Still, the problem for me and apparently others is that the #LINK does work when launched from a touch in an href, but is ignored when launched via the URL. Other symptoms include navigating to the link only on the first time in a session or navigating to the bottom of the html file.
The Solution is to load the url after a short delay.
Here is an example:
My html is saved in assets: res/assets/help.html
With anchors like this:
<a name="helplinkcontacts"/>
And loaded like this:
final String baseUrl = "file:///android_asset/help.html#helplinkcontacts";
final WebView helpTextView = (WebView)findViewById(R.id.help_dialog_text);
helpTextView.loadUrl(baseUrl); // Ignores Anchor!!
I added the timer like this:
final String baseUrl = "file:///android_asset/help.html#helplinkcontacts";
final WebView helpTextView = (WebView)findViewById(R.id.help_dialog_text);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
helpTextView.loadUrl(baseUrl);
}
}, 400);
Note: Shorter delays, such as 100ms failed to navigate to the link.
(*) It turns out that so many of us have our WebViews inside of ScrollViews because we started out with a TextView rendering Spannable text which both supports some HTML and requires a ScrollView. Anyways, remove the ScrollView as soon as you convert your TextView into a WebView.
My Solution is , Check this Answer
public class MainActivity extends Activity {
WebView myWebView;
public static boolean flag = false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myWebView = new WebView(this);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl("file:///android_asset/chapters.html");
setContentView(myWebView);
myWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
if (url.contains("#") && flag == false) {
myWebView.loadUrl(url);
flag = true;
} else {
flag = false;
}
}
});
}
}
I had a similar problem. Nothing would jump to anchor tags in the html. I didn't have my WebView within a ScrollView. Instead the problem was the base url I passed into loadDataWithBaseURL did not have a colon (':') in it. I believe the baseUrl needs to have some text, then a colon, then some more text, for example "app:htmlPage24".
So here's the initial call to my WebView, just to load the data in the string HTML_24:
wv.loadDataWithBaseURL("app:htmlPage24", HTML_24, "text/html", "utf-8", null);
Then I have a list that jumps to sections on the screen, depending on the list item you tap:
sectionsLV.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
wv.loadUrl("app:htmlPage24#section" + arg2);
}
});
HTML_24 is something like:
<html>
...
<a name="section1"/>
...
<a name="section2"/>
...
<a name="section3"/>
...
</html>
I was also facing the same issue. Anchor link was jumping to arbitrary position. Make sure not to hide webview when loading.
Use Invisible instead of gone.
This change fixed my issue.
try this
String myTemplate = "LINK!<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><a name=\"link\"></a>Testing!";
myWebView.loadDataWithBaseURL(null, myTemplate, "text/html", "utf-8", null);
the word "Testing!" must be outside of the screen to see it works.
WebView in Android 4.0 fails to open URLs with links in them.
e.g.
"file:///android_asset/help.html#helplinkcontacts"
Here is how I got around it
WebView wv = (WebView) nagDialog.findViewById(R.id.wv);
wv.getSettings().setJavaScriptEnabled(true);
wv.setWebViewClient(new MyWebViewClient(link));
wv.loadUrl("file:///android_asset/help.html");
And define the custom WebViewClient class
class MyWebViewClient extends WebViewClient {
private String link;
public MyWebViewClient(String link) {
this.link = link;
}
#Override
public void onPageFinished(WebView view, String url) {
if (!"".equals(link) && link != null)
view.loadUrl("javascript:location.hash = '#" + link + "';");
}
}

Categories

Resources