TabHost - how to change tab text in XML
Asked Answered
G

2

9

I know the solution on how to change it programically however I would like to set the text in XML. How do you do that? I have looked here: http://developer.android.com/reference/android/widget/TabHost.html but found no solution.

Gypsophila answered 13/6, 2013 at 13:11 Comment(1)
Did it really work? I'm getting the same problem as the others. Did you have to do extra java code for it?Gourley
L
8

How about ....

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TabHost 
        android:id="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:tag="tab0"
                    android:text="Tab 1"
                    android:background="@android:drawable/btn_star_big_on"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    />
                <TextView
                    android:tag="tab1"
                    android:text="Tab 2"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    />
                <TextView
                    android:tag="tab2"
                    android:text="Tab 3"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    />

            </TabWidget>

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">

                <TextView
                    android:text="Hallo1"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" />
                <TextView
                    android:text="Hallo2"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" />
                <TextView
                    android:text="Hallo3"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" />

            </FrameLayout>
        </LinearLayout>
    </TabHost>

</RelativeLayout>

This way it'll look as follows:

TabHost

Check out the complete tab sample here.

Hope this helps .... Cheers!

Lahdidah answered 13/6, 2013 at 13:30 Comment(5)
@Supuhstar: can you explain why or what error you got?Lahdidah
no error, just... absolutely none of this worked. The TextViews appear to the left of the tabs in the preview, and no tabs appear when run on an actual device. No functionality is gained and the fragments appear atop eachotherSumac
Depends on how you use the XML above; try the following: add ids to all tabs (i.e. TextViews, e.g. android:id="@+id/tab1", ..., android:id="@+id/content1", ... ) and remove android:text tags from the TabWidget as well as undefined symbols. This results in a clean UI preview ...Lahdidah
Thanks for the tips! I ended up changing them programmaticlySumac
I'm attempting to the same thing and getting the same result as Supuhstar. Adding textviews within tabwidget just results in text to the left of the existing tabs, well seemingly created 6 tabs as opposed to the default 3. So I see your most recent comment, but if you remove android:text tags from the text views within tabwidget, youre back to tabs with no labels yes?Tail
M
1

You can always go into the @+id/tab1 and change it to whatever you want the tab to be called. So if you wanted it to be called "About" just change it to @+id/About in the linear layout for the specific tab.

Muro answered 28/12, 2014 at 6:55 Comment(2)
The question is about XML. Is the formula you give an xpath query ? If so, please give an example, everybody might not read fluently xpath.North
Quite late on this one, but jorgensen was talking about tab IDs, by default, TabHost use ID's as tab labels. But as @gattsbr pointed out, that solution is only usable if you don't use string translation filesRoundtheclock

© 2022 - 2024 — McMap. All rights reserved.