Open a URL in a browser

suggest change

Opening with the default browser

This example shows how you can open a URL programmatically in the built-in web browser rather than within your application. This allows your app to open up a webpage without the need to include the INTERNET permission in your manifest file.

public void onBrowseClick(View v) {
    String url = "";
    Uri uri = Uri.parse(url);
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    // Verify that the intent will resolve to an activity
    if (intent.resolveActivity(getPackageManager()) != null) {
        // Here we use an intent without a Chooser unlike the next example

Prompting the user to select a browser

Note that this example uses the Intent.createChooser() method:

public void onBrowseClick(View v) {
    String url = "";
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    // Note the Chooser below. If no applications match, 
    // Android displays a system message.So here there is no need for try-catch.
    startActivity(Intent.createChooser(intent, "Browse with"));

In some cases, the URL may start with “www”. If that is the case you will get this exception:

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

The URL must always start with “http://” or “https://”. Your code should therefore check for it, as shown in the following code snippet:

if (!url.startsWith("https://") && !url.startsWith("http://")){
    url = "http://" + url;
Intent openUrlIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
if (openUrlIntent.resolveActivity(getPackageManager()) != null) {

Best Practices

Check if there are no apps on the device that can receive the implicit intent. Otherwise, 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.

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents