TabHost in Android Apps

TabHost displaying statistics in Android device

TabHost displaying statistics in Android device

The ‘Fitness Manager’ App provides several statistics for the user in order to check the workout progress. The different statistics should be displayed in a TabHost. A TabHost is a container for a tabbed window view. The TabHost holds two children. One child contains a set of tab labels that the user clicks to select a specific tab. The other child contains a FrameLayout object that displays the contents of that page. The TabHost integrated in the Fitness App should meet the following further requirements:

  • Every tab displays a button bar which contains a different amount of buttons depending on the selected tab.

  • Every tab contains an area where the corresponding statistics are plotted.

First I had to define the XML Layout for the TabHost in a XML Layout file. In Android Studio the container for TabHost is provided in the palette of the Android Studio designer. The TabHost can be simply inserted in an empty activity by drag and drop.

When integrating the container for TabHost, first the TabHost itself is defined in the XML Layout definition. The TabHost contains a RelativeLayout of the same size.

TabHost XML Layout definition

TabHost XML Layout definition

The RelativeLayout of the TabHost contains the definition of a TabWidget. The TabWidget displays a list of tab labels representing each page in the parent’s tab collection. The container object for this widget is TabHost. When the user selects a tab, this object sends a message to the parent container TabHost, to tell it to switch the displayed page.

TabWidget XML Layout definition

TabWidget XML Layout definition

The XML Layout definition for TabWidget contains the attribute android:id set to the value @android:id/tabs. This value cannot be changed and therefore the TabWidget cannot be accessed programmatically by using the function findViewbyId.

Additionally the RelativeLayout of the TabHost contains a FrameLayout. The FrameLayout is used for displaying the content of the TabHost.

FrameLayout XML Layout definition

FrameLayout XML Layout definition

The XML Layout definition for FrameLayout also contains the attribute android:id. The value for the attribute android:id is set to the value @android:id/tabs. This value cannot be changed either and therefore cannot be accessed programmatically by using the function findViewbyId. For every tab added to the container TabHost a LinearLayout is defined in the XML Layout definition of the FrameLayout. There you have the base frame for a TabHost. Now the LinearLayouts in the FrameLayouts can be filled with view elements like every other convenient view. GridLayouts or LinearLayouts can be inserted into the LinarLayout for ButtonBars or mobile forms. ImageViews, ScrollViews or ListViews can also be embedded by the LinearLayouts of the FrameLayout.

After finishing the XML Layout definition of the TabHost, the TabHost has to be instantiated programmatically in the onCreate method of the corresponding activity. The TabHost instantiation includes the definition of the single tabs. The tab of a TabHost has a tab indicator, content and a tag that is used to keep track of it. After declaring objects for every tab, the content of the tab is assigned by specifing the id of the corresponding view and the label is set as tab indicator.

TabHost instantiation

TabHost instantiation

In order to handle the user input action, when a different tab is selected, a setOnTabChangedListener must be implemented in the onCreate function of the corresponding activity. In this Listener the content of the selected tab can be set.

TabHost: setOnTabChangedListener

TabHost: setOnTabChangedListener

Now the basic implementation of the TabHost is finished.

But if your app should resize the content of the TabHost depending on the Android device or the orientation of the Android device, then you have to implement a runnable with post functionality. There the content of the tab must be accessed and the tab must be resized, which cannot be handled easily by calling findViewbyId as I already mentioned. The content of the tab can only be accessed via the container TabHost by host.getTabContentView().getChildAt(int i). The same access point is valid for the TabWidget of the TabHost: host.getTabWidget(). getChildAt(int i). Then for example the LayoutParams of the TabWidget or the content of the TabHost can be set.

TabContent LayoutParams definition

TabContent LayoutParams definition

Last issue I want to point out concerning TabHost is the Android device orientation. If the user rotates the Android device while displaying a TabHost in an App, then the tab is always set to the first tab of the TabHost with every rotation, no matter which tab was selected previously to the screen rotation. In order to solve this issue I did overwrite the function onSaveInstanceState of the activity and saved the current tab value.

Activity onSaveInstanceState implementation

Activity onSaveInstanceState implementation

Therefore I could access the savedInstanceState now in the onCreate and onConfigurationChanged functions of the activity and set the tab selected previously to a screen rotation.

Activity access of savedInstanceState

Activity access of savedInstanceState

And done 🙂

If you have any questions, any improvement suggestions for the implementation (I mentioned that this is my first Android App) or any improvement suggestions for my English, I really would appreciate if you can leave me a comment.

And I would even more appreciate if you like to check out the ‘Fitness Manager’ App in the Google Play Store or the ‘Fitness Manager’ App in the iTunes Store. See you.

Follow us on

Facebook
Twitter
Google Plus