Webview Link Click Open Default Browser

Clicking URLs opens default browser

If you're using a WebView you'll have to intercept the clicks yourself if you don't want the default Android behaviour.

You can monitor events in a WebView using a WebViewClient. The method you want is shouldOverrideUrlLoading(). This allows you to perform your own action when a particular URL is selected.

You set the WebViewClient of your WebView using the setWebViewClient() method.

If you look at the WebView sample in the SDK there's an example which does just what you want. It's as simple as:

private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}

WebView open apps/default browser

It is definitely possible in react native. You can do it as follows:

import {Linking} from "react-native"  

<WebView

source={{uri:"https://mashupguide.net/1.0/html/ch02s05.xhtml"}}
onShouldStartLoadWithRequest={request => {
let url = request.url;
if (url.includes("http://maps.google.com/")) {
Linking.openURL(url);

return false
} else {
return true
}
}}
/>

This will prevent the WebView from browsing to the next page and instead open the phone's default browser.

reference: https://github.com/react-native-community/react-native-webview/blob/master/docs/Reference.md#onshouldstartloadwithrequest

working example: https://snack.expo.io/@ammarahmed/sadistic-almond

Android Webview - How I can open external links in default browser?

You have to send an Intent to to the default web browser, in order to open the link in the browser, so maybe something like this:

Create a class that extends WebViewClient, in this way:

private class MyCustomWebViewClient extends WebViewClient {

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("https://mywebsite.domain.com")) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
} else {
view.loadUrl(url);
}
return true;
}
}

In your MainActivity, in the onCreate method, set the MyCustomWebViewClient before the call to loadUrl method, like this:

public class MainActivity extends AppCompatActivity {

DrawerLayout drawerLayout;
ActionBarDrawerToggle drawerToggle;
NavigationView navigation;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OneSignal.startInit(this).init();
initInstances();
WebView myWebView = (WebView) findViewById(R.id.webView1);
myWebView.setWebViewClient(new MyCustomWebViewClient());
myWebView.loadUrl("http://mywebsite.domain.com/myprofile/");
}

// the rest of your code

Open link in default browser from Android WebView with HTML or Javascript only

No. Opening default browser by WebView requires an implementation on native App. In particular the App developer should override the method WebViewClient.shouldOverrideUrlLoading and launch an intent to load the url in a separate browser instead through the WebView.

Open external links in the browser with android webview

The problem is you need to send an Intent to the default web browser to open the link. What you are doing is just calling a different method in your Webview to handle the link. Whenever you want another app to handle something you need to use Intents. Try this code instead.

private class CustomWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("message2space.es.vu")) {
view.loadUrl(url);
} else {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
}
return true;
}
}


Related Topics



Leave a reply



Submit