How to change the font size of tabhost in android
Asked Answered
C

4

26

How can the font size of the tabs be changed? I extend TabActivity for the tabs.

Cymograph answered 26/4, 2011 at 10:24 Comment(0)
K
69

You can define themes, use styles to achieve this:

First you create the theme (name:CustomTheme) for your Activity in your res/values/styles.xml:

<style name="CustomTheme" parent="@android:style/Theme">
    <item name="android:tabWidgetStyle">@style/CustomTabWidget</item>
</style>
<style name="CustomTabWidget" parent="@android:style/Widget.TabWidget">
    <item name="android:textAppearance">@style/CustomTabWidgetText</item>
</style>
<style name="CustomTabWidgetText" 
    parent="@android:style/TextAppearance.Widget.TabWidget">
    <item name="android:textSize">20sp</item>
    <item name="android:textStyle">bold</item>
</style>

Then in your androidManifest.xml you specify the theme above for your TabActivity or Activity containing your TabWidget:

<activity android:name="MyTabActivity" android:theme="@style/CustomTheme">

This will serve you with the output you want (of course you should change the size and style for your preference).

Kreiker answered 26/4, 2011 at 11:46 Comment(3)
In Android 4.1.2 emulator this seems to set the tab style to some weird looking one to me: the buttons are orange, when pressed. (perhaps its applying some Android 1.0 style instead of the 4.1 default one?)Tadeo
I got errors when I put it under res but fixed it by putting it under res/values.Allsun
@Tadeo You should derive from Widget.Holo.TabWidgetSuspiration
A
17

Its not pretty but try this Dirty Fix :

TabWidget tw = (TabWidget)tabHost.findViewById(android.R.id.tabs);
View tabView = tw.getChildTabViewAt(0);
TextView tv = (TextView)tabView.findViewById(android.R.id.title);
tv.setTextSize(20);

or

 //Do this to hack font size of title text
 LinearLayout ll = (LinearLayout) tabHost.getChildAt(0);
 TabWidget tw = (TabWidget) ll.getChildAt(0);

 // for changing the text size of first tab
 RelativeLayout rllf = (RelativeLayout) tw.getChildAt(0);
 TextView lf = (TextView) rllf.getChildAt(1);
 lf.setTextSize(21);
 lf.setPadding(0, 0, 0, 6);
Adumbrate answered 26/4, 2011 at 10:36 Comment(1)
The above code snippet is working for android below 2.3. Not working for android 4.0 and 4.1.Bess
G
6

Slightly generalized:

final TabWidget tw = (TabWidget)mTabHost.findViewById(android.R.id.tabs);
    for (int i = 0; i < tw.getChildCount(); ++i)
    {
        final View tabView = tw.getChildTabViewAt(i);
        final TextView tv = (TextView)tabView.findViewById(android.R.id.title);
        tv.setTextSize(20);
    }
Goosestep answered 30/9, 2014 at 22:27 Comment(0)
D
2

i use this piece of code in my Code but it effect only on first Tab the other 3 Tabs are still unchanged.

    TabWidget tw = (TabWidget)tabHost.findViewById(android.R.id.tabs);
    View tabView = tw.getChildTabViewAt(0);
    TextView tv = (TextView)tabView.findViewById(android.R.id.title);
    tv.setTextSize(10);
Discrepancy answered 23/5, 2015 at 11:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.