Opening a web browser from an Android application

June 25, 2012
By

In this article we will create an application which will open a user input website in a web browser. In order to open a web browser, we will create an intent object with ACTION_VIEW as action and data with “http” in the url. Since http request is invoked by the external browser, no explicit permission is needed for this application.


1. Create a new Android project

New Android Project

Figure 1: New Android Project


2. Select build target for this application

Select build target

Figure 2: Select build target


3. Enter application details

 

Enter application details

Figure 3:Enter application details


4. res/values/strings.xml


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, MainActivity!</string>
    <string name="app_name">BrowserDemo</string>
    <string name="hnt_te_url">Enter a website here ...</string>
    <string name="lbl_btn_browse">Browse</string>
</resources>


5. res/layout/main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/te_url"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="@string/hnt_te_url"
        android:inputType="text"

    />

    <Button
        android:id="@+id/btn_browse"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/lbl_btn_browse"

    />
</LinearLayout>


6. src/in/wptrafficanalyzer/browserdemo/MainActivity.java


package in.wptrafficanalyzer.browserdemo;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        /** Getting a reference to the button object of the main.xml */
        Button btn = (Button) findViewById(R.id.btn_browse);

        OnClickListener listener = new OnClickListener() {

            @Override
            public void onClick(View v) {
                /** Getting a reference to the textedit object of the main.xml */
                EditText txt = (EditText) findViewById(R.id.te_url);

                /** Creating a view action to display the website */
                Intent intent = new Intent("android.intent.action.VIEW");

                /** Setting up a uri object with a web address  */
                Uri data = Uri.parse("http://"+txt.getText().toString());

                /** Setting web address to the intent object as data */
                intent.setData(data);

                /** Start an activity that matches intent action and intent data */
                startActivity(intent);

            }
        };

        btn.setOnClickListener(listener);
    }
}


7. Application  in execution

 

Main Activity

Figure 4: Main Activity

  •  Enter web address and click Browse button of the Main Activity
  • Then the entered website will be opened in a web browser.
  • If there are more than one web browser is available in the device, then system will prompt which browser should be used to open the site
Browser Activity

Figure 5: Browser Activity


8. Download


9. Reference

http://developer.android.com/guide/index.html

How to hire me?

I am George Mathew, working as software architect and Android app developer at wptrafficanalyzer.in

You can hire me on hourly basis or on project basis for Android applications development.

For hiring me, please mail your requirements to info@wptrafficanalyzer.in.

My other blogs
store4js.blogspot.com


Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :



Tags: , , , ,

One Response to Opening a web browser from an Android application

  1. kazi Ejaj on December 27, 2013 at 11:26 pm

    Thanks !!!! Bro

Leave a Reply

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

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner