How can I set the height of tabs in Android a TabLayout?
Asked Answered
F

3

12

I Have this TabLayout in Android and wanted to make the tabs a little heigher than the default (48dp)

    <android.support.design.widget.TabLayout
            android:id="@+id/contentTabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="@style/Theme.Zhaw.TabLayout"
            app:tabMode="fixed"
            app:tabGravity="fill"/>

Here is the Style Theme.Zhaw.TabLayout:

<style name="Theme.Zhaw.TabLayout" parent="Widget.Design.TabLayout">
    <item name="tabIndicatorColor">@color/text_white</item>
    <item name="tabIndicatorHeight">4dp</item>
    <item name="tabPaddingStart">6dp</item>
    <item name="tabPaddingEnd">6dp</item>
    <item name="tabBackground">@color/colorPrimary</item>
    <item name="tabTextAppearance">@style/Theme.Zhaw.TabLayoutText</item>
    <item name="tabSelectedTextColor">@color/text_white</item>
</style>

tabIndicatorHeight can set the height of the small indicator (active tab) in the tab. but how can we set the height of the tab itself?

Fist answered 25/10, 2016 at 11:31 Comment(2)
just replace height with android:layout_height="your_height"Adamsun
@HRaval java.lang.RuntimeException: Unable to start activity ComponentInfo{MainActivity}: android.view.InflateException: Binary XML file line #25: Binary XML file line #25: You must supply a layout_height attribute.Fist
D
6

set a layout_height in dps instead of wrap_content this could differ in different display sizes but if you wanna set a height dynamically

getApplication.getResources().getDisplayMetrics()

get your current height and calculate the height according to that

Darladarlan answered 25/10, 2016 at 11:58 Comment(0)
C
10

just change layout_height from wrap_content to what ever u want it to be

  <android.support.design.widget.TabLayout
            android:id="@+id/contentTabs"
            android:layout_width="match_parent"
            android:layout_height="Your Value"
            style="@style/Theme.Zhaw.TabLayout"
            app:tabMode="fixed"
            app:tabGravity="fill"/>
Cherilyncherilynn answered 25/10, 2016 at 13:37 Comment(0)
D
6

set a layout_height in dps instead of wrap_content this could differ in different display sizes but if you wanna set a height dynamically

getApplication.getResources().getDisplayMetrics()

get your current height and calculate the height according to that

Darladarlan answered 25/10, 2016 at 11:58 Comment(0)
E
2

I use the following code for setting the height of TabLayout. Hope it helps you:

//Get tablayout
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
//Get the layout params that will allow you to resize the tablayout
ViewGroup.LayoutParams params = tabLayout.getLayoutParams();
//Change the height in 'Pixels'
params.height = 100;
tabLayout.setLayoutParams(params);
Excruciation answered 23/6, 2018 at 19:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.