Tabs With Intents

If you look at the Android widgets and classes related to tabs, you’ll see that a tab can have either a View or an Activity as its contents. Most of the time, people use Views, as they are simpler to implement and lighter-weight to run. However, there may be circumstances where you feel putting your activities in tabs would be useful, so let’s take a look at how that is done, in a modified excerpt from Version 1.9 of The Busy Coder’s Guide to Android Development.

If you want to use an Activity as the content of a tab, you provide an Intent that will launch the desired Activity; Android’s tab-management framework will then pour the Activity’s user interface into the tab. So, let’s create a tabbed Web browser.

Your natural instinct might be to use an http: Uri as the target of the Intents, one Intent per tab. That way, you could use the built-in Browser application and get all of the features that it offers. Alas, this does not work. It appears you cannot host other applications’ activities in your tabs, only your own activities, presumably for security reasons.

But, we can always use the WebView widget to provide a simplified browsing experience.

Here is the source to the main activity, the one hosting the TabView:

[sourcecode language=”java”]
public class IntentTabDemo extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

TabHost host=getTabHost();

host.addTab(host.newTabSpec(“one”)
.setIndicator(“CW”)
.setContent(new Intent(this, CWBrowser.class)));
host.addTab(host.newTabSpec(“two”)
.setIndicator(“Android”)
.setContent(new Intent(this, AndroidBrowser.class)));
}
}
[/sourcecode]

As you can see, we are using TabActivity as the base class, and so we do not need our own layout XML — TabActivity supplies it for us. All we do is get access to the TabHost and add two tabs, each specifying an Intent that directly refers to another class. In this case, our two tabs will host a CWBrowser and an AndroidBrowser, respectively.

Those latter two activities are simple wrappers around WebView:

[sourcecode language=”java”]
public class CWBrowser extends Activity {
WebView browser;

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);

browser=new WebView(this);
setContentView(browser);
browser.loadUrl(“http://commonsware.com”);
}
}
[/sourcecode]

[sourcecode language=”java”]
public class AndroidBrowser extends Activity {
WebView browser;

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);

browser=new WebView(this);
setContentView(browser);
browser.loadUrl(“http://code.google.com/android”);
}
}
[/sourcecode]

They simply load a different URL into the browser: the CommonsWare home page in one, the Android home page in the other.

The resulting UI shows what tabbed browsing could look like on Android:

Using distinct subclasses for each targeted page is rather wasteful. Instead, we could have packaged the URL to open as an “extra” in an Intent and used that Intent to spawn a general-purpose BrowserTab activity, which would read the URL out of the Intent “extra” and use this. The proof of this is left as an exercise for the reader.

About author

AndroidGuys
AndroidGuys 4627 posts

Founded on November 5, 2007, we've enjoyed bringing you the latest in Android news and rumors. Updated daily, we strive to deliver reviews, opinions, and updates on all things related to Android.

You might also like

News and Rumors

Another Look at X10 Mini Pro Successor

Another pair of images for Sony Ericsson’s purported X10 Mini Pro successor have made their way online, giving a decent look at both the front and back of the device. As one might expect, the phone still looks to be among the smallest Android phones whenever it debuts.

News and Rumors

ZTE Mustang outed for AT&T

Sprint doesn’t look to be the only carrier set to soon offer an Android smartphone from ZTE. A ZTE Mustang has been tipped for AT&T courtest of @evleaks. Details are

News and Rumors

Dell’s 5-Inch Streak Hits UK in June, US in Summer

After months of waiting, Dell finally officially divulged their plans for the 5-inch Streak tablet today.¬† Starting next month, the “iPad Killer” will become available for customers in the UK