Phone: +66-94-257-9379

Beatrice Krell
2016-04-29 06:04:09
Android setOnTabChangeListener, Fitness Manager, Android onSaveInstanceState, Android TabWidget, Android TabHost, Android TabContentView

The Fitness Manager App provides several statistics for the user in order to check the workout progress. The different statistics should be displayed in an Android 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 Android 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 Android 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.


The RelativeLayout of the TabHost contains the definition of an Android TabWidget. The Android 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.


The XML Layout definition for an Android 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 Android TabContentView of the TabHost.


The XML Layout definition for the FrameLayout of the the Android TabContentView 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.


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


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.


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 Android onSaveInstanceState of the activity and saved the current tab value.


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.


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

Google Plus

Contact Details

Telephone: +66-94-257-9379

Hot and quiet place with WiFi

Koh Phangan,