: Beginning Android

Tabbed Browsing, Sort Of

Tabbed Browsing, Sort Of

One of the main features of the modern desktop Web browser is tabbed browsing, where a single browser window can show several pages split across a series of tabs. On a mobile device this may not make a lot of sense, given that you lose screen real estate for the tabs themselves.

In this book, however, we do not let little things like sensibility stop us, so let me demonstrate a tabbed browser, using TabActivity and Intents.

As you may recall from Chapter 10, a tab can have either a View or an Activity as its content. If you want to use an Activity as the content of a tab, you provide an Intent that will launch the desired Activity; Androids tab-management framework will then pour the Activitys user interface into the tab.

Your natural instinct might be to use an http:Uri the way we used a geo:Uri in the previous example:

Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("http://commonsware.com"));

That way, you could use the built-in Browser application and get all of the features that it offers.

Alas, this does not work. You cannot host other applications activities in your tabs only your own activities, for security reasons.

So, we dust off our WebView demos from Chapter 13 and use those instead, repackaged as Activities/IntentTab.

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

publicclass IntentTabDemoextends TabActivity {
@Override
public voidonCreate(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)));
}
}

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 activities are simple modifications to the earlier browser demos:

publicclass CWBrowserextends Activity {
WebView browser;
@Override
public voidonCreate(Bundle icicle) {
super.onCreate(icicle);
browser =newWebView(this);
setContentView(browser);
browser.loadUrl("http://commonsware.com");
}
}
publicclass AndroidBrowserextends Activity {
WebView browser;
@Override
public voidonCreate(Bundle icicle) {
super.onCreate(icicle);
browser =newWebView(this);
setContentView(browser);
browser.loadUrl("http://code.google.com/android");
}
}

They simply load a different URL into the browser: the CommonsWare home page in one (Figure24-3), the Android home page in the other (Figure 24-4). The resulting UI shows what tabbed browsing could look like on Android.


Figure 24-3.The IntentTabDemo sample application, showing the first tab


Figure 24-4.The IntentTabDemo sample application, showing the second tab

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 that. The proof of this is left as an exercise for the reader.


: 1.477. /Cache: 3 / 0