I have a android app which contains jsp page.
On this jsp page one hyperlink is created and linked another jsp page which contains download pdf code.
When I click on hyperlink it will open in android default browser and download starts.
I want to download pdf inside web view without opening web browser.
For that you can use like this:
Every time you click a link WebViewClient's shouldOverrideUrlLoading method will be called. Check that url points to pdf file and do what you want. For example, you can view pdf.
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading (WebView view, String url) {
if (url.endsWith(".pdf")) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
// if want to download pdf manually create AsyncTask here
// and download file
return true;
}
return false;
}
});
Related
[enter image description here][1]I want to open a pdf in WebView, not by directly passing the pdf URL. This means there is a URL that opens in WebView and when that page opens in WebView it contains a link that opens pdf.
How can I open that pdf in WebView?
For example:
This is the screenshot of webpage loaded in WebView (Open the below image).
[1]: https://i.stack.imgur.com/Z71pr.png
In side this image their is a option of view in Action row. So How to open that pdf which is coming from a URL loaded in WebView?
Thanks
You can open any pdf on internet using google drive link. For example, if your pdf url is:
String pdf = "https://mywebsite/mypdf.pdf"
Just embed this link in following google drive link:
String gDrive = "http://drive.google.com/viewerng/viewer?embedded=true&url="
So now your URL is: "http://drive.google.com/viewerng/viewer?embedded=true&url=https://mywebsite/mypdf.pdf"
and open this in your webview. Remember to include this URL in your WebViewClient if you are filtering the urls to open in app or chrome.
[Edit]
To open url after taping on the link, you need to create a WebView Client which overrides each url like this:
private class MyWebViewClient extends WebViewClient {
//link opener
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.endsWith(".pdf") )
{
//create another webview activity and load the url as mentioned above so that it does not keeps on reloading
myWebView.load(gDrive);
}
return false;
}
}
and set this webview client as:
myWebview.setWebViewClient(new MyWebViewClient())
or if you want to load in the same activity/fragment you can modify the condition as:
if(url.endsWith(".pdf") && !url.contains("http://drive.google.com/viewerng/viewer?embedded=true&url="))
and do the same. Hope this is what you want to do.
Suppose I want to create an android app it can load a website with webview. It also can load a local HTML page. I can create a hyperlink of a website from a local HTML page. Can I create a hyperlink from an online page to a local HTML page?
Just point your link using the right protocol
Offline
file:///
Online
http://
Example
<!-- This opens a page online -->
<a href='http://domian.com/test.html'>Test Online</a>
<!-- This opens a page offline in your assets directory-->
<!-- Make sure the file 'test.html' exists in your assets directory-->
<a href='file:///android_asset/test.html'>Test Offline</a>
The above offline access would work but only for version SDKs lesser than 24 (Marshmallow and below). Security grew stronger starting from Nougat to access offline html from anchor links. You can use the following strategy in your code to handle the protocol yourself.
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView webView, WebResourceRequest webResourceRequest) {
if (android.os.Build.VERSION.SDK_INT >= 24) {
if (webResourceRequest.getUrl().getScheme().equals("file")) {
//pass the actual target to webview
webView.loadUrl(webResourceRequest.getUrl().toString());
} else {
// If the URI is not pointing to a local file, open with an ACTION_VIEW Intent
webView.getContext().startActivity(new Intent(Intent.ACTION_VIEW, webResourceRequest.getUrl()));
}
}
else{
if (Uri.parse(url).getScheme().equals("file")) {
webView.loadUrl(url);
} else {
// If the URI is not pointing to a local file, open with an
//ACTION_VIEW Intent
webView.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
}
}
return true; // in both cases we handle the link manually
}
});
I have an Android app with that displays a mobile website (WebView), in the mobile website there are links redirecting to a PDF, Excel and video files.
When try to open it in my regular browser my phone asks to open it with another app or it start a download, so I can open it afterwards.
But in my WebView app it either doesn't work, no response or it displays a "Page unavailable" error.
Is it even possible?
To handle links in WebView, you can use the shouldOverrideUrlLoading method of WebViewClient class. Consider the following example;
WebView webView = (WebView) findViewById(R.id.infoView);
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// Assuming you are giving link to some PDF file.
if (url.contains(".pdf")) {
// Now do what you want to with the url here
}
return true;
}
}
This way, you can intercept any link tapped in WebView and then do whatever you want.
I have an application in which i have added a menu.
Clicking on this menu opens up a website.
There is a list of links(zip files) available on this website.
Clicking on a particular link should result in that zip file to be downloaded to the assets folder of my application.
I am able to load the website.
Code for this:
String url = "http://almondmendoza.com/android-applications/";
Intent k = new Intent(Intent.ACTION_VIEW);
k.setData(Uri.parse(url));
startActivity(k);
I am referring to the example given on this website
What i am curious to know is that whether it is possible to perform an action on click of a particular link available on website. If it is possible then how can i accomplish this task?
Use WebView to load webpage, you can recognize URL using following code
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url){
webView.loadUrl(url);
// Here the String url hold 'Clicked URL'
return false;
}
});
I'm using a WebView to display a page in which the html includes an iframe where src="xxxxx.php".
This iframe loads as an ad image with an underlying link. If I click on that image (link), it tries to load the new page within original iframe (which doesn't show much in that little space). What I want to happen is clicking on the link to open the referred page in a new browser window, leaving my app as is.
If I use the Android browser to display the original page and click on this iframe, it loads the link as a new page. How do I get the same behavior with a WebView? Using a WebViewClient with shouldOverrideUrlLoading() doesn't seem to be called by the iframe link.
I had a similar issue with google ads in a WebView source, since they load in an iframe as well. This is how I resolved it:
Try this in your WebViewClient, typically under your shouldOverrideUrlLoading()
#Override
public void onLoadResource (WebView view, String url) {
if (url.contains("googleads")) {
if(view.getHitTestResult().getType() > 0){
view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
view.stopLoading();
Log.i("RESLOAD", Uri.parse(url).toString());
}
}
}
I can propose one fix to previous code:
#Override
public void onLoadResource (WebView view, String url) {
if (url.contains("googleads")) {
if(view.getHitTestResult() != null &&
(view.getHitTestResult().getType() == HitTestResult.SRC_ANCHOR_TYPE ||
view.getHitTestResult().getType() == HitTestResult.SRC_IMAGE_ANCHOR_TYPE)){
view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
view.stopLoading();
}
}
}
To detect links clicks inside of the iframe. The links should have additional parameters. I've found that:
shouldOverrideUrlLoading of the WebViewClient will be executed for a case when iframe link has target parameter target ="_parent" or target="_top",
onCreateWindow of the WebViewClient, in case, if the iframe link contains target="_blank" parameter
Seems the iframe links without target parameter not possible to track exactly via WebViewClient