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.
Related
I have android app hosting a web site in WebView. It works fine, but has one issue. Whenever users search in Google and click on page of this site, the application is opening not at that page but on home page of the web site.
For example: they click on www.example.com/blog - WebView app is open but it shows homepage, not blog.
Links (navigation) works fine inside of application when it starts.
try these code
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
return false;
}
});
when you click on any link on webview then view.loadurl() method again load new url.
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;
}
});
In my app I am trying to open web application which needs flash player but as I am new to android I only know opening web view with chrome client. is there any method to open webview with any other browser instead of chrome.
I am planing to open that web activity in puffin browser as it supports flash player
Write this to open URL in your own webview :
web.setWebViewClient(new myWebClient());
To enable Javascript :
web.getSettings().setJavaScriptEnabled(true);
When the user clicks a link from a web page in your WebView, the default behavior is for Android to launch an application that handles URLs. Usually, the default web browser opens and loads the destination URL. However, you can override this behavior for your WebView, so links open within your WebView.
EDIT
Add this to your code and see if its work:
Make sure shouldOverrideUrlLoading(...) should return false to prevent the default browser from being opened.
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
}
There are 2 condition first open all links within App and second is that open all links in mobile browser.
But I want to open web site internal links within App and external link in mobile browser.
For Example:
My app is a WebView of https://www.123.com and I am using amazon affiliate links in my web site(https://www.123.com)
Now I want that all links starting with https://www.123.com open within App and all amazon affiliate (external) links starting https://www.amazon.com open in mobile browser.
Just override shouldOverideURLoading in WebViewClient.
You will get the links clicked inside the webview, then process accordingly.
WebView webView = findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
Log.d(TAG, "Request URL is " + request.getUrl());
if (request.getUrl().toString().equalsIgnoreCase("www.123.com/affiliated")) {
//DO SOMETHING
} else {
//DO SOMETHING
}
return super.shouldOverrideUrlLoading(view, request);
}
});
webView.loadUrl("https://stackoverflow.com/");
Please mark the answer as accepted if it worked.Thanks.
I am trying to write an app I want it to open a web page and auto login I am not sure how to go about sending the info to the browser from the app code.
So basically you are going to need to load in the webpage within a WebView (You can find instructions for that here and then probably push javascript into the WebView that will fill in the fields and load the page.
In your activity's onCreate:
WebView webview = new WebView(this);
setContentView(webview);
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean onPageFinished(WebView view, String url) {
// Check here if url is equal to your site URL.
}
});
webview.loadUrl("http://yourwebsite.com/");
This line enables javascript in your WebView:
webView.getSettings().setJavaScriptEnabled(true);
Then you can use the WebViewClient to detect when the page you want has fully loaded. When that happens, you can use:
webView.loadUrl("javascript:document.getElementsByName('username').value = 'username'");
webView.loadUrl("javascript:document.getElementsByName('password').value = 'password'");
webView.loadUrl("javascript:document.forms['login'].submit()");
And it should automatically log you in. It's worth noting that this generally isn't easy to do on a lot of sites since they will randomize the login control ids and it also doesn't generally sit well with users if an application is logging into a website automatically for them.