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

0

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

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 it opens in the app itself!

    
asked by anonymous 18.12.2018 / 18:53

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 a Intent object passing as argument Intent.ACTION_VIEW , this will cause the android to search for apps on the device that can open that URL (the browser is one). 3 - Pass your string containing your URL as a parameter of the setData(url) method of the Intent class (use the Uri.parse (your_string_url) method to convert the string to a URI). 4 - Call the startActivity(intent) method by 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. 



    

19.12.2018 / 05:16