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.
Related
I'm trying to implement the following behavior into my Android App.
The app is a native app with some parts using WebView, in this WebView there is the possibility of using different payment methods.
If the user has the app of that bank installed I want to be able to open the corresponding app. In some cases, the website loaded into the WebView launch a specific intent:// that I can easily intercept and try to redirect the user to the app, but for the cases where the website use the new Android App link I'm not able to intercept them because they are normal https:// calls.
I tried to load the Android App link into Chrome and this way the app is opened.
My question at this point is how I can replicate the behavior of Chrome into my WebView??
I didn't find that much informations on this specific case besides the Android Documentation
Thanks for your help :)
You can intercept the url click by using custom WebViewClient for the webview.
1)Set the custom webview client for the webview
2)Override the below method and return true (you can do this based on particular url also. )
shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
3)Override below method and handle the click and start the activity using either package name or playstore url with intent.
public void onLoadResource (WebView view,
String url)
WebView mWebView;
WebViewClient customWebClient = new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request){
//This can be modified based on the url . URL can be retrieved using request.getUrl()
return true;
}
#Override
public void onLoadResource(WebView view, String url){
if( url.equals("") ){
// launch playstore activity
final String appPackageName = "package name of the application"
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
} catch (android.content.ActivityNotFoundException anfe) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
}
}
}
}
mWebView.setWebViewClient(customWebClient);
I have a webview that loads a real URL something like https://example.com
I would like to open some links according to a pattern in a second webview.
I have the following code:
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// Handle external url
boolean openInNew = manageUrl (url);
Log.i("openurl", url + " : " + openInNew);
if (openInNew) {
this.callBack.openWebView2 (url);
return true;
} else {
return false;
}
}
The second webview shows correctly the external url I want to load.
The problem I found is about what happens with the original webview, it is also redirected to an empty blank page (with a loading icon).
Is possible to keep the first webview in the original page? I thought that with shouldOverrideUrlLoading should be enough.
Maybe I forgot something, any help would be appreciate.
Thanks a lot.
Ok, there will be two types of links in the content of my WebView. The behavior will be defined by the format of the link.
(1) Open the link in a browser. (The url begins with "openbrowser:")
(2) According to the link, open another Activity in the same project.
(The url will be "openactivity")
I am not sure if it is possible to create a map for the WebView which maps from a url pattern to an intent. For example, by default, if the url begins with "mailto:" the WebView will create an intent to open the mailbox. Could I define other mappings for my WebView?
I know there is a way to set a WebViewClient and override the shouldOverrideUrlLoading() method. But in API level 19, the function is not guaranteed to be called:
shouldOverrideUrlLoading() not called
So is it possible to set this url pattern to intent mapping as a general settings of the WebView?
Using a WebViewClient should be enough. We've had no problems with API level 19. For example:
WebView webView = new WebView(this);
String html = "<html><body>Test it</body></html>";
webView.loadData(html, "text/html", "utf-8");
webView.setWebViewClient(new WebViewClient()
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if (url.startsWith("showmessage"))
{
Toast.makeText(MainActivity.this, url, Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
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);
I'm trying to display PDF in Android using WebView, I'm using google docs for this purpose. It was working fine till this morning. But all of a sudden it's throwing 502 error.
Is there any alternative to display PDF's in WebView.
This is the URL I have been using to render PDF in WebView
https://docs.google.com/gview?embedded=true&url=+URL
It looks like https://docs.google.com/gview?embedded=true&url= is down.
Try this way
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if (url.endsWith(".pdf"))
{
// Load "url" in google docs
String googleDocs = "https://docs.google.com/viewer?url=";
mWebView.loadUrl(googleDocs + url);
}
else
{
// Load all other urls normally.
view.loadUrl(url);
}
return true;
}
UPDATE try this
webview.loadUrl("http://docs.google.com/gview?embedded=true&url=" + url);