Android TabActivity with two list views
Often times when creating an activity with tabs, it’s easy to have a separate activity for the tab content. However, things get tricky when those activities need to interact with one another.
So the solution is to create a tabactivity with views (instead of activities) as the tab content. We see a very simple example of this in the official Android tutorial Hello, TabWidget.
In this tutorial, we will create a slightly more advanced application. We will have two listviews as the content of a tabactivity and have them interact with one another (basically by clicking the items of one list view will add them to the second list view).
Step 1: Create Layout
First we need to create a layout that features a TabHost, TabWidget, and our two List Views:
Step 2: Create Activity
Next, we will write the Java source for our TabActivity. First, we create an Activity that extends TabActivity
. We want to get the tabhost from the tabactivity to add our two list views.
Next, we need to add the views to the tabhost by setting the content of each tab as an anonymous class of TabContentFactory
.
Step 3: Customize
If you want to programmatically change the tab, you should call setCurrentTab(index)
on the tabhost where index is the tab number. If you want to add a listener to know when the user changes the tab, you need to add a OnTabChangeListener
like this: tabHost.setOnTabChangedListener(this)
Full Source
The full source for this example is here:
Now when you run this app, you will have a tab activity with two tabs (each list view). Clicking on one item in the first tab will add that item to the second tab.
More advanced tutorials with tabs to come, including a tab activity with a map view and list view and the interaction between them.
Check out Android Tabs with Map and List View Tutorial.
To see a mult-list tab activity in action, check out my “Draft Punk” Android app.