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


Try this:

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("")); 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


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

String url = ""; 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(""));       startActivity(intent);  

or :

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(""));  startActivity(intent); 

the shortest! :

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(""))); 
vote vote


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 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.


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


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


Try this:

Uri uri = Uri.parse(""); 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(; 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?