How to open a link in a browser that was clicked on a Webview on Android

Asked

Viewed 139 times

0

I want to open a link that will be clicked on Webview, I want this open in the browser. I have here the code:

mWebView.setWebViewClient(new WebViewClient()
{
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if(url.contains(url)) {
            Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(i);
        }
        return true;
    }
});

But the first time it is clicked opens in the application itself!

1 answer

-1

You have to create a Intent to open the link in the browser.

1 - Create a String with your URL
2 - Create an object Intent passing as argument Intent.ACTION_VIEW, this will make android search apps on the device that can open this URL (the browser is a).
3 - Pass your string containing your URL as a method parameter setData(url) of the Intent class (use the Uri.parse(sua_string_url) method to convert the string to a URI).
4 - Call the method startActivity(intent) passing the intent properly configured with the previous steps.

String url = "http://www.yourlink.com"; // Cria a string - URL
Intent intent = new Intent(Intent.ACTION_VIEW); // Cria o Intent
intent.setData(Uri.parse(url)); // Passa a string para url e passa como um argumento do metodo setData(url)
startActivity(intent); //chama o metodo startActivity(intent) passando o intent e o navegador é aberto aqui. 



  • 1

    Hi @Carlos. Further develop your answer. For example, explain what you do.

  • @Joãomartins, It’s okay like this? :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.