How to Open Google Maps from Webview click - android

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;
}

Related

My websites internal urls still open in external browser even i have implemented well code in activity.java

before my app was working well in webview using a website link but now when i changed domain and url it doesn't working it opens in external browser all internal links too
please someone help me ,i need to upload it into playstore fast..`
private class WebViewClientDemo extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.startsWith("tel:")){
Intent intent =new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(intent);
return true;
}else if (Uri.parse(url).getHost().equals("https://imovers.in") ) {
//open url contents in webView
return false;
} else{
//here open external links in external browser,phone or app
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}`

How can I open a web page from inside my app?

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);

WebView handle redirect click link with Intent.ACTION_VIEW

I've a difficult managing open link on webview content click with Intent.ACTION_VIEW on different device.
My scenario is: i've some html code displayed within a webview inside my app, this content have some link (href), then on click event i want to open the link with external app and not inside the same web view.
I've achieved this by:
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(request.getUrl().toString()));
activity.startActivity(intent);
return true;
}
});
So, the problem is that this doesn't happen an all devices.
With Android 7 it's working but on version 6 doesn't. Anyway to manage different version i've already used different implementation.
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
activity.startActivity(intent);
return true;
}
});
I've already look around on stackoverflow but the only answer i've found it's to use the code above.
Thanks for the support
UPDATE - 15/02/18
I've solved the problem, putting the two #Override method together like this:
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(request.getUrl().toString()));
activity.startActivity(intent);
return true;
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
activity.startActivity(intent);
return true;
}
});
That is because the method you are using was added only in API 24 (7.0), so it doesn't exist on older APIs, check here. What you need to do is to use the old one in this case. It is declared as Deprecated, but you still can use it.

webview shouldOverrideUrlLoading multiple url

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());

How to open Dialer Activity from a webviewclient?

I am using webviewclient to open the html page. The html page is having a anchor tag.
when i click on the anchor tag my phone dialer activity should be launched.
when i click on this anchor tag in external browser (android default browser ), it is launching the phone dialer, but as i am using the webviewclient (browser with in my application). i am unable to launch the phone dialer.
is there any way to achieve this using webviewclient ?
You should override this method
public boolean shouldOverrideUrlLoading(WebView wv, String url)
{
if(isNumber)
{
//Get the number from the URL
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:12345"));
startActivity(intent);
return true;
}
return false;
}
in the WebViewClient, and return ture that means you want to handle this by yourself instead of the webView.
The document is here.
I think is the best way to do this. So Please try this, I know i am too late to reply this:
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if( URLUtil.isNetworkUrl(url) ) {
view.loadUrl(url);
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity( intent );
return true; /*
return false;*/
}

Categories

Resources