i'm trying to load two different url in a webview, but it seems something went wrong or maybe webview does not allow override multiple url in single webview?. first url is my site url, the second one is my community facebook page, the first one is opened in a webview but not the other one.
this is my codes:
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("mysite.com")){
return false;
} else if (Uri.parse(url).getHost().equals("facebook.com")){
return false;
}
else {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
And that was my first attempt, the second is:
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if ((Uri.parse(url).getHost().equals("mysite.com"))|| (Uri.parse(url).getHost().equals("facebook.com"))){
return false;
} else {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
I tried to replace facebook url with another site url but still no luck.
I am new to android so any explanations would be appreciated, thank you in advance..
err, maybe the problem is inside here? since loadUrl only load mysite url and not the other url?:
mwebView.loadUrl("mysite full url");
//handle link inside app
mwebView.setWebViewClient(new MyWebviewClient());
Related
I've found an app's source code and it opens web pages using external browsers, like Chrome or others. I want to make it access those pages from inside the app, without using an external browser. I think that I should change the code below somehow, but I don't know how.
private class MyWebviewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("http://dmc.teiion.gr")) {
//open url contents in webview
return false;
} else {
//here open external links in external browser or app
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
yourWebView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return true;
}
});
Or change
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
to
view.loadUrl(url);
What i want is to keep links in the web view as long as they are part of the site, but external links should launch into an external web browser. Also on the site i have phone link's with tel:555-323-2323 if i use the code below the phone number work and launch the phone app but external links are not working.
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
if (url.contains("tel:")) {
startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(url)));
return true;
} else {
return true;
}
if (Uri.parse(url).getHost().equals("www.example.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
The main reason things are not working is because you have an if-else and both branches return true. All code after the else statement is unreachable. Remove the else statement or change that logic.
Remove first statement (view.loadUrl(url)).
Remove else part of first condition.
Your code will look something like this :
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains("tel:")) {
startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(url)));
return true;
}
if (Uri.parse(url).getHost().equals("www.example.com"))
// This is my web site, so do not override; let my WebView load the page
return false;
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); startActivity(intent);
return true;
}
I use this solution:
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains("tel:")) {
startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(url)));
return true;
}
if (Uri.parse(url).getHost().equals(URL)) {
// This is my web site, so do not override; let my WebView load the page
return super.shouldOverrideUrlLoading(view, url);
}
return super.shouldOverrideUrlLoading(view, url);
}
I have an app with a webview. The webview links to a mobile webpage where all links work correctly. When I click on a link in this webview, the link opens up in google maps inside the webview. Instead, I want the link to open up the native google maps app. Can anyone tell me how to do this? There is literally nothing online about it. Thank you!
EDIT: Based on Matiash's response. here is the code I have added:
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.MYURL.com")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
You should add a WebViewClient as the other response indicates, but the correct method to override is shouldOverrideUrlLoading().
Return true to indicate that the link has been handled, otherwise false.
i used this code cause ihad maps links and phone numbers links :
#Override
public boolean shouldOverrideUrlLoading(WebView wv, String url) {
if(url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse(url));
startActivity(intent);
return true;
}
if(url.startsWith("https://maps.google")){
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
wv.loadUrl(url);
return false;
}
I have a webview to show a webpage. In that webpage button click event i'm redirecting to another page. From there another button click event im redirection to 3rd page. Instead of redirecting to 3rd page i need to show my next activity.
I'm using the following code but my first page itself not loading
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_page);
WebView webview = (WebView) findViewById(R.id.wvLogin);
setContentView(webview);
webview.setWebViewClient(new WebViewClient()
{
// Override URL
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if(url.equals("http://My 3rd page redirecting URL"))
{
Intent i = new Intent(getApplicationContext(), APImages.class);
startActivity(i);
}
return true;
}
});
webview.loadUrl("http://Default URL to load first time");
}
Please tell me how to preceed it?
Try returning false when you are not handle the event. This is from the WebClient reference.
True if the host application wants to leave the current WebView and handle the url itself, otherwise return false.
Your code should be like this.
webview.setWebViewClient(new WebViewClient()
{
// Override URL
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if(url.equals("http://My 3rd page redirecting URL"))
{
Intent i = new Intent(getApplicationContext(), APImages.class);
startActivity(i);
return true;
}
return false;
}
});
Try this variant:
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if(url.equals("http://My 3rd page redirecting URL"))
{
Intent i = new Intent(getApplicationContext(), APImages.class);
startActivity(i);
} else {
webview.loadUrl(url);
}
return true;
}
Try onLoadResource. This should work for webview. Sometimes onOverride function doesnt work especially when you are calling an intent.
I've created a web view app, the page that is displayed features market:// links but upon clicking them I get the 404 screen along with the error that the protocol is not supported. I've tried looking through documentation but was unable to find anything relating to this. Any help is much appreciated.
For me the JavaScript thing wasn't a solution as the HTML is not under my control. So if you need to control this from the application side, then there is a relative simple solution: Derive from WebViewClientand inject the implementation using WebView.setWebViewClient(). All you need to override in your WebViewClientimplementation is the shouldOverrideUrlLoading method as shown here:
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null && url.startsWith("market://")) {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
} else {
return false;
}
}
For me this works fine.
HOPE THIS HELPS YOU
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if (url.startsWith("market://")||url.startsWith("vnd:youtube")||url.startsWith("tel:")||url.startsWith("mailto:"))
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
return true;
}
else{
view.loadUrl(url);
return true;
}
}
For the links to work you have to have the market app installed on your device/emulator.
Also your app need to request a permission to access network.
UPD:
as a workaround you can call java code from within the webview, for example if you generate links like this:
..
Define a javascript function named go():
<script type="text/javascript">
function go(link) {
if (handler) {
handler.go(link);
} else {
document.location = link;
}
}
</script>
You then can pass in a handler object into the WebView:
webview.addJavascriptInterface(new Handler() {
#Override
public void go(String marketUrl) {
//start market intent here
}
}, "handler");
Handler interface can be defined as follows:
public interface Handler{
public void go(String url);
}
Work for me:
webView = (WebView) findViewById(R.id.webView);
webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.getSettings().setPluginState(WebSettings.PluginState.ON_DEMAND);
webView.setWebViewClient(new MyWebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://myweb.com");
private class MyWebViewClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null && url.startsWith("whatsapp://")) {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
} else {
return false;
}
}
}
It is important to understand how the webview and its clients (webviewclient and webchromeclient) works. Please go through the http://therockncoder.blogspot.in/2014/04/understanding-androids-webchromeclient.html
In the webviewclient's shouldOverrideUrlLoading() method, you can decide if you want to open the link in new browser or within the webview. If you don't override this method, it will by default open the link in a new browser outside of the your android application.
If you want to open within webview, override the method as below
public boolean shouldOverrideUrlLoading(WebView view, String url) { <br>
Log.v("activity", "INSIDE WEBVIEW CLIENT ON shouldOverrideUrlLoading");
view.loadUrl(url);
return false; //need to understand return value based on usage
}
Schemes like whatsapp://send?text=Hello%20World! or market://details?id=xx.xx.xx will open the corresponding apps automatically if they are opened outside the webview and if that app is installed on the handset.
If you want to open certain links within webview and specific schemes outside webview, you need to override WebChromeClients onCreateWindow() method as explained in the link provided above. It should solve the purpose.
Instead of adding check for particular scheme, Modifying #sven solution, this will work for all schemes
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String host= Uri.parse(url).getHost();
if (host == null) {
view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
}
view.loadUrl(url);
return false;
}
Simplest solution
Intent newApp = new Intent(Intent.ACTION_VIEW, Uri.parse(URL));
startActivity(newApp);