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.

LucasSeveryn
  • 5,984
  • 8
  • 38
  • 65

2 Answers2

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!

Trinimon
  • 13,839
  • 9
  • 44
  • 60
  • @Supuhstar: can you explain why or what error you got? – Trinimon Jul 07 '14 at 20:14
  • 6
    no error, just... absolutely none of this worked. The `TextView`s 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 eachother – Ky - Jul 07 '14 at 22:21
  • 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 ... – Trinimon Jul 08 '14 at 13:43
  • Thanks for the tips! I ended up changing them programmaticly – Ky - Jul 08 '14 at 14:32
  • 4
    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? – kyle Sep 25 '14 at 16:07
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.

jorgensen
  • 11
  • 1
  • 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. – Lorenz Meyer Dec 28 '14 at 07:07
  • 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 files – STremblay Apr 09 '17 at 21:18