views:

1294

answers:

3

Hi,

i'm trying to use TabHost and TabWidget in my Android application. This is my layout main.xml:

<TabHost
    android:id="@+id/mainTabHost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"&gt;
    <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="65px"/>
    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <LinearLayout
            android:id="@+id/contentTags"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical">
        </LinearLayout>
        <LinearLayout
            android:id="@+id/contentPreferences"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical">
        </LinearLayout>
    </FrameLayout>
</TabHost>

And my code:

final TabHost mainTabHost = (TabHost) this.findViewById(R.id.mainTabHost);
mainTabHost.setup();
final TabHost.TabSpec tabSpecTags = mainTabHost.newTabSpec("tabTags");
tabSpecTags.setIndicator(this.getString(R.string.tab_name_tags));
tabSpecTags.setContent(R.id.contentTags);
mainTabHost.addTab(tabSpecTags);
final TabHost.TabSpec tabSpecPreferences = mainTabHost.newTabSpec("tabPreferences");
tabSpecPreferences.setIndicator(this.getString(R.string.tab_name_preferences));
tabSpecPreferences.setContent(R.id.contentPreferences);
mainTabHost.addTab(tabSpecPreferences);

The problem is that i do not want my tabs to be so tall (65px). However, if i set the layout_height of the TabWidget to e.g. 30px, i can't see the tab labels (indicator) on the tabs at all.

Seems like there is a "minimum required" height for the TabWidget (65px?) and the indicator is positioned at the bottom of this 65px?

Is there a way to adjust the positioning of the indicator?

Thanks!

+1  A: 

However, if i set the layout_height of the TabWidget to e.g. 30px, i can't see the tab labels (indicator) on the tabs at all.

Note that the techniques for doing this won't necessarily be reliable in future Android releases, if they change up how a TabHost is constructed.

Is there a way to adjust the positioning of the indicator?

Starting with API Level 4 (a.k.a., Android 1.6), TabHost.TabSpec accepts a View as the indicator via setIndicator(). I haven't tried it, but it may give you greater control over the layout of an individual tab's indicator.

CommonsWare
Thanks! i tried it out and yes, i can use a TextView with setIndicator. So all that's left is to configure the TextView. The downside is that it's basically starting from scratch (by default just text with no decoration).
Edwin Lee
Well, you can always create a layout XML file for your indicator and inflate it, using that for the `setIndicator()` call.
CommonsWare
A: 

Hi Edwin,

can you please share your code with me on how you got your tabs length small and how you got the text to appear as you want. i am in same problem as when ever I decrease the height of my tabs I am loosing the text display.

androidProgrammer
A: 

i see... when u addTab, usually we use setIndicator like this: QTabHost.addTab(QTabHost.newTabSpec("tab_test2").setIndicator("TAB 2").bla bla....

u can use TextView to replace "TAB 2", became like this:

tview=new TextView(this); tview.setText("Title here"); QTabHost.addTab(QTabHost.newTabSpec("tab_test2").setIndicator(tview).bla bla....

all u need is just modify the textview. Thanks... ^^

Rubin Happy Ch.