846

I'm just wondering how to fire up an Intent to the phone's browser to open an specific URL and display it.

Can someone please give me a hint?

0

10 Answers 10

1915

To open a URL/website you do the following:

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

Here's the documentation of Intent.ACTION_VIEW.


Source: Opening a URL in Android's web browser from within application

8
  • 129
    In production level code, you may like to check if the url begins with http or https... Would be better to check if (!url.startsWith("http://") && !url.startsWith("https://")) url = "http://" + url; Sep 25, 2012 at 5:33
  • 4
    Encode the Query String If any special characters or spaces. then It will work awesome.For Example : String query="For martin Luther King"; query=URLEncoder.encode(query); String url="en.wikipedia.org/wiki/Special:Search?search="+query; Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); startActivity(browserIntent);
    – Chaitu
    Sep 6, 2013 at 4:46
  • 23
    Funny thing here is that startActivity(i) line could produce ActivityNotFound exception so I go for wrap this line in try/catch block to prevent app crash. This could happen if really no browser app installed on target device (yeah, shoot happens) also it could be that your app was forbidden to start a browser using restrict profiles.
    – Stan
    Jun 12, 2015 at 18:10
  • 15
    From Android Developer web site: Caution: 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.
    – Paul
    Feb 9, 2016 at 22:22
  • 3
    @MahendraLiya The question was about sending a Intent to open the Browser, not about parsing the URL. If the URL is hardcoded, you wouldn't check for http(s), would you? So your argument to invalue the answer is simply invalid, and i do not understand why it has so many upvotes. Funniest thing is, no answer provided states that startActivity can only be called inside certain places, if someone wants to know that, he has to look it up in the docs - but if he wants to parse the URL, he just has to look at the comments.
    – devsteff
    Feb 7, 2018 at 11:47
222

The short version

startActivity(new Intent(Intent.ACTION_VIEW, 
    Uri.parse("http://almondmendoza.com/android-applications/")));

should work as well...

0
174

The shortest version.

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

In some cases URL may start with "www". In this case you will get an exception:

android.content.ActivityNotFoundException: No Activity found to handle Intent

The URL must always start with "http://" or "https://" so I use this snipped of code:

if (!url.startsWith("https://") && !url.startsWith("http://")){
    url = "http://" + url;
}
Intent openUrlIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(openUrlIntent);
2
  • 1
    I went with Matcher _SCHEMA_MATCHER = Pattern.compile("(https?://|mailto:).+").matcher("") and return _SCHEMA_MATCHER.reset(uri).matches()? uri : "http://" + uri. Aug 30, 2013 at 22:39
  • ActivityNotFoundException you can handle by keeping try catch. in catch block you keep toast message to get remote user clarity.
    – Mohd Qasim
    Nov 23, 2020 at 12:50
56

Sending an Intent to Browser to open specific URL:

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

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);

or even more short!

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

More info about Intent

=)

1
  • What if the uri is missing http? May 15, 2017 at 18:05
27

Is there also a way to pass coords directly to google maps to display?

You can use the geo URI prefix:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("geo:" + latitude + "," + longitude));
startActivity(intent);
16

From XML

In case if you have the web-address/URL displayed on your view and you want it to make it clikable and direct user to particular website You can use:

android:autoLink="web"

In same way you can use different attributes of autoLink(email, phone, map, all) to accomplish your task...

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

Use following snippet in your code

Intent newIntent = new Intent(Intent.ACTION_VIEW, 
Uri.parse("https://www.google.co.in/?gws_rd=cr"));
startActivity(newIntent);

Use This link

http://developer.android.com/reference/android/content/Intent.html#ACTION_VIEW

6

"Is there also a way to pass coords directly to google maps to display?"

I have found that if I pass a URL containing the coords to the browser, Android asks if I want the browser or the Maps app, as long as the user hasn't chosen the browser as the default. See my answer here for more info on the formating of the URL.

I guess if you used an intent to launch the Maps App with the coords, that would work also.

Not the answer you're looking for? Browse other questions tagged or ask your own question.