How to open browser in an ANDROID application? How to open a URL from an ANDROID application?

By | March 28, 2011

Hi all,In this tutorial I will show you how to open browser from an ANDROID application.
This is done through intents. You know that intents are used to invoke other activities.
So we here invoke the browser activity.
The following code will open the default browser in your ANDROID phone.

package com.myPackage;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

public class OpenURL extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String url = "http://www.google.com/";
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse(url));
        startActivity(i);
    }
}

This is the manifest file for the above code.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.Open"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".OpenClass"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Please leave your valuable comments.

3 thoughts on “How to open browser in an ANDROID application? How to open a URL from an ANDROID application?

  1. Zamani

    I can browse , then select what i want , so how can i go back to my apps

    Reply
  2. lester

    Hi,
    I’m want to open a url from an icon and was hoping to use this snippet. I hace defined all my icons bu when I put the above code into the SDK I get the error: ‘The public type OpenURL must be defined in its own file;.

    Any thoughts

    Thanks.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *