Four different ways of opening a web page in Android
FIRST POST OF 2009!!
Threw this Android Activity together really quickly to see if I understood the different ways of getting a web page to launch. The first three examples perform the same functionality, the Linkify.addLink call builds a means of launching an Intent.ACTION_VIEW. The fourth example loads the url into a WebView in the layout.
Just as a note, it seems that the API is pretty sensitive to using “www.mgmblog.com” vs “http://www.mgmblog.com”, I think I’ve got it correct where it matters, you may have to do some experimenting on your own to make sure you know where it makes a difference…works correctly.
Here’s the layout, code in one fell swoop:
layout/goto_web.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:id="@+id/link1_lbl"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Link 1"
android:paddingRight="5dip"/>
<!--
autoLink='web' is the attribute that does the magic
of turning the text into a web link
-->
<TextView
android:id="@+id/link1_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:autoLink="web"
android:text="www.mgmblog.com"/>
</TableRow>
<TableRow>
<TextView
android:id="@+id/link2_lbl"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Link 2"
android:paddingRight="5dip"/>
<TextView
android:id="@+id/link2_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</TableRow>
<TableRow>
<Button
android:id="@+id/link3_btn"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Link 3"/>
</TableRow>
<TableRow>
<Button
android:id="@+id/link4_btn"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Link 4"/>
<WebView
android:id="@+id/link4_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</TableRow>
</TableLayout>
</LinearLayout>
GotoWeb.java
public class GotoWeb extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView( R.layout.goto_web );
// method 2
TextView tv = (TextView)findViewById( R.id.link2_view );
// make sure that setText call comes BEFORE Linkify.addLinks call
tv.setText("www.mgmblog.com");
Linkify.addLinks( tv, Linkify.WEB_URLS );
// method 3
Button link3Btn = (Button)findViewById( R.id.link3_btn );
link3Btn.setOnClickListener( new View.OnClickListener()
{
public void onClick(View v)
{
Uri uri = Uri.parse( "http://www.mgmblog.com" );
startActivity( new Intent( Intent.ACTION_VIEW, uri ) );
}
});
// method 4
Button link4Btn = (Button)findViewById( R.id.link4_btn );
link4Btn.setOnClickListener( new View.OnClickListener()
{
public void onClick(View v)
{
WebView wv = (WebView)findViewById( R.id.link4_view );
wv.loadUrl( "http://www.mgmblog.com" );
}
});
}
}
Don’t forget to add the following to your AndroidManifest.xml file, and it must be a child of the <manifest> element
<uses-permission android:name="android.permission.INTERNET" />
There ya’ go, Link 1 seems like the easiest, you simply use the android:autoLink to state that the TextView holds a URI to launch. Obviously you’ll want to externalize all the strings into your values/strings.xml file, I didn’t want to have to include another file. For more info, check out the WebView API

January 7th, 2009 at 5:07 am
These two posts describe Android Intent Playground, a simple application to help you test and play with intents:
http://dtmilano.blogspot.com/2009/01/android-intent-playground-20.html
http://dtmilano.blogspot.com/2008/12/android-intent-playground-20.html
March 1st, 2010 at 9:59 am
I have just used your code for ‘Four different ways of opening a web page in Android’ which was really helpful, thanks! But when I try to run it through my emulator I get an error on the ‘GotoWeb.java page which says ‘link4btn cannot be resolved’. I am quite new at writing code for Android, can you offer any suggestion as to what I have done wrong or missed out please??
Thanks very much.