Introduction
Sometimes, if we want to display multiple views in a window, we need to use the Tab container. In Android, it is called TabHost.
There are two ways to use TabHost:
Use TabHost to navigate multiple views in the same activity
Use TabHost to navigate multiple activities (using intents)
Structure of the Tab Application
The Activity structure of TabHost is as follows:
First look at an example:
Layout file:
<? Xml version = "1.0" encoding = "UTF-8"?>
<TabHost android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: id = "@ + id/tabHost"
Xmlns: android = "http://schemas.android.com/apk/res/android"
>
<TabWidget
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: id = "@ android: id/tabs"
/>
<FrameLayout
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: id = "@ android: id/tabcontent"
>
<LinearLayout
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: id = "@ + id/tab1"
Android: orientation = "vertical"
Android: paddingTop = "60px"
>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "100px"
Android: text = "This is tab1"
Android: id = "@ + id/txt1"
/>
</LinearLayout>
<LinearLayout
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: id = "@ + id/tab2"
Android: orientation = "vertical"
Android: paddingTop = "60px"
>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "100px"
Android: text = "This is tab 2"
Android: id = "@ + id/txt2"
/>
</LinearLayout>
<LinearLayout
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: id = "@ + id/tab3"
Android: orientation = "vertical"
Android: paddingTop = "60px"
>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "100px"
Android: text = "This is tab 3"
Android: id = "@ + id/txt3"
/>
</LinearLayout>
</FrameLayout>
</TabHost>
<? Xml version = "1.0" encoding = "UTF-8"?>
<TabHost android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: id = "@ + id/tabHost"
Xmlns: android = "http://schemas.android.com/apk/res/android"
>
<TabWidget
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: id = "@ android: id/tabs"
/>
<FrameLayout
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: id = "@ android: id/tabcontent"
>
<LinearLayout
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: id = "@ + id/tab1"
Android: orientation = "vertical"
Android: paddingTop = "60px"
>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "100px"
Android: text = "This is tab1"
Android: id = "@ + id/txt1"
/>
</LinearLayout>
<LinearLayout
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: id = "@ + id/tab2"
Android: orientation = "vertical"
Android: paddingTop = "60px"
>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "100px"
Android: text = "This is tab 2"
Android: id = "@ + id/txt2"
/>
</LinearLayout>
<LinearLayout
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: id = "@ + id/tab3"
Android: orientation = "vertical"
Android: paddingTop = "60px"
>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "100px"
Android: text = "This is tab 3"
Android: id = "@ + id/txt3"
/>
</LinearLayout>
</FrameLayout>
</TabHost>
Activity Code:
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
TabHost tabHost = (TabHost) findViewById (R. id. tabHost );
TabHost. setup ();
TabSpec spec1 = tabHost. newTabSpec ("Tab 1 ");
Spec1.setContent (R. id. tab1 );
Spec1.setIndicator ("Tab 1 ");
TabSpec spec2 = tabHost. newTabSpec ("Tab 2 ");
Spec2.setIndicator ("Tab 2 ");
Spec2.setContent (R. id. tab2 );
TabSpec spec3 = tabHost. newTabSpec ("Tab 3 ");
Spec3.setIndicator ("Tab 3 ");
Spec3.setContent (R. id. tab3 );
TabHost. addTab (spec1 );
TabHost. addTab (spec2 );
TabHost. addTab (spec3 );
}
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
TabHost tabHost = (TabHost) findViewById (R. id. tabHost );
TabHost. setup ();
TabSpec spec1 = tabHost. newTabSpec ("Tab 1 ");
Spec1.setContent (R. id. tab1 );
Spec1.setIndicator ("Tab 1 ");
TabSpec spec2 = tabHost. newTabSpec ("Tab 2 ");
Spec2.setIndicator ("Tab 2 ");
Spec2.setContent (R. id. tab2 );
TabSpec spec3 = tabHost. newTabSpec ("Tab 3 ");
Spec3.setIndicator ("Tab 3 ");
Spec3.setContent (R. id. tab3 );
TabHost. addTab (spec1 );
TabHost. addTab (spec2 );
TabHost. addTab (spec3 );
}
Create a Tab using the TabSpecs class
Use the setIndicator method to set the text of a tab
Use setContent to set the tab content
If you use TabActivity as the base class of your Activity, you do not need to call the TabHost. Setup () method.
It looks like this after running:
You can also specify indicator as a view:
TabSpec spec1 = tabHost. newTabSpec ("Tab 1 ");
Spec1.setContent (R. id. tab1 );
TextView txt = new TextView (this );
Txt. setText ("Tab 1 ");
Txt. setBackgroundColor (Color. RED );
Spec1.setIndicator (txt );
TabSpec spec1 = tabHost. newTabSpec ("Tab 1 ");
Spec1.setContent (R. id. tab1 );
TextView txt = new TextView (this );
Txt. setText ("Tab 1 ");
Txt. setBackgroundColor (Color. RED );
Spec1.setIndicator (txt );
Set the tab content
The preceding example shows how to use a tab to display different layout resources. What should we do if we need to navigate to different activities through tab?
In this case, we need an activity as the root activity of the application. This Activity contains TabHost, which is used to navigate different activities through intents.
Note: The root Activity must inherit TabActivity. The Code is as follows:
Layout:
<? Xml version = "1.0" encoding = "UTF-8"?>
<TabHost android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: id = "@ android: id/tabhost"
Xmlns: android = "http://schemas.android.com/apk/res/android"
>
<TabWidget
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: id = "@ android: id/tabs"
/>
<FrameLayout
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: id = "@ android: id/tabcontent"
>
</FrameLayout>
</TabHost>
<? Xml version = "1.0" encoding = "UTF-8"?>
<TabHost android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: id = "@ android: id/tabhost"
Xmlns: android = "http://schemas.android.com/apk/res/android"
>
<TabWidget
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: id = "@ android: id/tabs"
/>
<FrameLayout
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: id = "@ android: id/tabcontent"
>
</FrameLayout>
</TabHost>
Activity:
Public class TabDemo extends TabActivity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
TabHost tabHost = getTabHost ();
// No need to call TabHost. Setup ()
// First Tab
TabSpec spec1 = tabHost. newTabSpec ("Tab 1 ");
Spec1.setIndicator ("Tab 1", getResources (). getDrawable (R. drawable. sun ));
Intent in1 = new Intent (this, Act1.class );
Spec1.setContent (in1 );
TabSpec spec2 = tabHost. newTabSpec ("Tab 2 ");
Spec2.setIndicator ("Tab 2", getResources (). getDrawable (R. drawable. chart ));
Intent in2 = new Intent (this, Act2.class );
Spec2.setContent (in2 );
TabHost. addTab (spec2 );
TabHost. addTab (spec3 );
}
}
Public class TabDemo extends TabActivity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
TabHost tabHost = getTabHost ();
// No need to call TabHost. Setup ()
// First Tab
TabSpec spec1 = tabHost. newTabSpec ("Tab 1 ");
Spec1.setIndicator ("Tab 1", getResources (). getDrawable (R. drawable. sun ));
Intent in1 = new Intent (this, Act1.class );
Spec1.setContent (in1 );
TabSpec spec2 = tabHost. newTabSpec ("Tab 2 ");
Spec2.setIndicator ("Tab 2", getResources (). getDrawable (R. drawable. chart ));
Intent in2 = new Intent (this, Act2.class );
Spec2.setContent (in2 );
TabHost. addTab (spec2 );
TabHost. addTab (spec3 );
}
} Running Effect
Add a Tab at runtime
At runtime, we can add a Tab by calling TabSepc. setContent (TabContentFactory.
<Span style = "white-space: pre"> </span> TabSpec spec1 = tabHost. newTabSpec ("Tab 1 ");
Spec1.setIndicator ("Tab 1", getResources (). getDrawable (R. drawable. sun ));
Spec1.setContent (new TabContentFactory (){
<Span style = "white-space: pre"> </span> @ Override
<Span style = "white-space: pre"> </span> public View createTabContent (String tag ){
<Span style = "white-space: pre"> </span> // TODO Auto-generated method stub
<Span style = "white-space: pre"> </span> return (new AnalogClock (TabDemo. this ));
<Span style = "white-space: pre"> </span>}
<Span style = "white-space: pre"> </span> });
<Span style = "white-space: pre"> </span> TabSpec spec1 = tabHost. newTabSpec ("Tab 1 ");
Spec1.setIndicator ("Tab 1", getResources (). getDrawable (R. drawable. sun ));
Spec1.setContent (new TabContentFactory (){
<Span style = "white-space: pre"> </span> @ Override
<Span style = "white-space: pre"> </span> public View createTabContent (String tag ){
<Span style = "white-space: pre"> </span> // TODO Auto-generated method stub
<Span style = "white-space: pre"> </span> return (new AnalogClock (TabDemo. this ));
<Span style = "white-space: pre"> </span>}
<Span style = "white-space: pre"> </span> });
From xinem's column