How can I open a URL in Android's web browser from my application?

ID : 743

viewed : 142

Tags : androidurlandroid-intentandroid-browserandroid

Top 5 Answer for How can I open a URL in Android's web browser from my application?

vote vote

95

Try this:

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")); startActivity(browserIntent); 

That works fine for me.

As for the missing "http://" I'd just do something like this:

if (!url.startsWith("http://") && !url.startsWith("https://"))    url = "http://" + url; 

I would also probably pre-populate your EditText that the user is typing a URL in with "http://".

vote vote

86

A common way to achieve this is with the next code:

String url = "http://www.stackoverflow.com"; Intent i = new Intent(Intent.ACTION_VIEW); i.setData(Uri.parse(url));  startActivity(i);  

that could be changed to a short code version ...

Intent intent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://www.stackoverflow.com"));       startActivity(intent);  

or :

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.stackoverflow.com"));  startActivity(intent); 

the shortest! :

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.stackoverflow.com"))); 
vote vote

70

Simple Answer

You can see the official sample from Android Developer.

/**  * Open a web page of a specified URL  *  * @param url URL to open  */ public void openWebPage(String url) {     Uri webpage = Uri.parse(url);     Intent intent = new Intent(Intent.ACTION_VIEW, webpage);     if (intent.resolveActivity(getPackageManager()) != null) {         startActivity(intent);     } } 

How it works

Please have a look at the constructor of Intent:

public Intent (String action, Uri uri) 

You can pass android.net.Uri instance to the 2nd parameter, and a new Intent is created based on the given data url.

And then, simply call startActivity(Intent intent) to start a new Activity, which is bundled with the Intent with the given URL.

Do I need the if check statement?

Yes. The docs says:

If there are no apps on the device that can receive the implicit intent, your app will crash when it calls startActivity(). To first verify that an app exists to receive the intent, call resolveActivity() on your Intent object. If the result is non-null, there is at least one app that can handle the intent and it's safe to call startActivity(). If the result is null, you should not use the intent and, if possible, you should disable the feature that invokes the intent.

Bonus

You can write in one line when creating the Intent instance like below:

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
vote vote

68

In 2.3, I had better luck with

final Intent intent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse(url)); activity.startActivity(intent); 

The difference being the use of Intent.ACTION_VIEW rather than the String "android.intent.action.VIEW"

vote vote

53

Try this:

Uri uri = Uri.parse("https://www.google.com"); startActivity(new Intent(Intent.ACTION_VIEW, uri)); 

or if you want then web browser open in your activity then do like this:

WebView webView = (WebView) findViewById(R.id.webView1); WebSettings settings = webview.getSettings(); settings.setJavaScriptEnabled(true); webView.loadUrl(URL); 

and if you want to use zoom control in your browser then you can use:

settings.setSupportZoom(true); settings.setBuiltInZoomControls(true); 

Top 3 video Explaining How can I open a URL in Android's web browser from my application?

Related QUESTION?