Android Snippet – How to open a browser in android application

Android Snippet – How to open a browser in android application

In this android code snippet example we are going to learn how to open a browser in android development.

There are many use cases where it might become necessary to open a new browser in your application. Imagine if you wanted to redirected your app users when they take certain action to an online website. The code will show you how easy it is to open the website in a browser.

In this example android snippet when a user click a button the app will load Google home page to the browser. You can open any website you are interested in but make sure that you have changed the URL in the code to the URL you wish to use.

Before we start, the first thing I will do is to list the environment and tools I used in this android code snippet but feel free to use whatever environment or tools you are familiar with.

Windows 7

Android Studio

Samsung Galaxy Fame Lite

Min SDK 14

Target SDK 19

To create a new android application project, following the steps as stipulated below.

Go to File menu

Click on New menu

Click on Android Application

Enter Project name: OpenBrowser

Package: com.inducesmile.openbrowser

Keep other default selections.

Continue to click on next button until Finish button is active, then click on Finish Button

Once you are done with creating your project, copy and paste the following code snippet to your project. Replace the code in your file with the following code. Make sure you change the package name if you did not use the same package.


import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class MainActivity extends ActionBarActivity {

protected void onCreate(Bundle savedInstanceState) {

Button browserButton = (Button)findViewById(;
browserButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(""));

public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(, menu);
return true;

public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == {
return true;

return super.onOptionsItemSelected(item);

The activity_main.xml file content is shown below.

<RelativeLayout xmlns:android=""
xmlns:tools="" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

android:text="Start New Broswer"
android:layout_centerHorizontal="true" />

Save the file and run your project.

You can download the code for this tutorial below. If you are having hard time downloading the tutorials, kindly contact and I will send it to you.

Remember to subscribe with your email so that you will be among the first to receive our new post once it is published

Add a Comment