Via : OpenAndroid.in
Tags :
Orginal Post: How To Open An URL In Android’s Web Browser

Here’s a code snippet to show you how to use “android.content.Intent” to open an specify URL in Android’s web browser.

[sourcecode language="plain"]<br /> button.setOnClickListener(new OnClickListener()  
@Override
public void onClick(View arg0)  
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.mkyong.com"));
startActivity(intent); 
 
);</pre><br />[/sourcecode]



In Android, just use “android.widget.Button” class to display a normal button.

1. Add Button


Open “res/layout/main.xml” file, add a button.

File : res/layout/main.xml

[sourcecode language="xml"]</pre><div><div><pre>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" &gt;

&lt;Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button - Go to mkyong.com" /&gt;

&lt;/LinearLayout&gt;<br />[/sourcecode]


2. Code Code


Attach a click listener to the button.

When user click on it, open mobile browser and display URL : http://www.mkyong.com.

File : MyAndroidAppActivity.java

[sourcecode language="java"]</pre><div><div><pre>package com.mkyong.android;

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

public class MyAndroidAppActivity extends Activity

Button button;

@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

addListenerOnButton();



public void addListenerOnButton()

button = (Button) findViewById(R.id.button1);

button.setOnClickListener(new OnClickListener()

@Override
public void onClick(View arg0)

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



);



<br />[/sourcecode]

3. Demo


Run the application.

1. Result, a normal button.
android button demo1

2. Click on the button, display URL in browser.
android button demo2

Download Source Code


Download it – Android-Button-Example.zip (15 KB)

 

 

References


Android Button JavaDoc
Android Intent.ACTION_VIEW JavaDoc

Source

http://www.mkyong.com/

Via : OpenAndroid.in
Tags :
Orginal Post: How To Open An URL In Android’s Web Browser

0 comments:

Post a Comment

 
Top