Android TabLayout selected tab background
Asked Answered
F

1

3

Is there really really no easy way to use a TabLayout and be able to set the colors of the tabs (selected, not selected)? Like the selected tab background uses colorPrimary, non selected tabs uses colorPrimaryDark or something? I've searched the web including this and this and much more. I can change the background color with solution 1 but now the indicator is missing and I want it back.

This can't be so hard to do..

missing indicator

Solution of first link:

<style name="Base.Widget.Design.TabLayout" parent="android:Widget">
        <item name="tabBackground">@drawable/tab_background</item>
</style>

// tab_background
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/tab_background_selected" android:state_selected="true" />
    <item android:drawable="@drawable/tab_background_unselected" android:state_selected="false" android:state_focused="false" android:state_pressed="false" />
</selector>

ANSWER:

<style name="Base.Widget.Design.TabLayout" parent="android:Widget">
        <item name="tabBackground">@drawable/tab_background</item>
        <item name="tabIndicatorColor">@color/colorAccent</item>
        <item name="tabIndicatorHeight">3dp</item>
</style>
Foret answered 5/7, 2016 at 11:43 Comment(0)
C
3

style change

  <style name="Base.Widget.Design.TabLayout" parent="android:Widget">
      <item name="tabBackground">@drawable/tab_background</item>
      <item name="tabIndicatorColor">#000000</item> 
      <item name="tabIndicatorHeight">5dp</item>      
  </style> 
Crumple answered 5/7, 2016 at 12:8 Comment(8)
how does this answer NOT hide the indicator?Foret
app:tabIndicatorColor="@android:color/transparent" app:tabIndicatorHeight="0dp"Crumple
i think you got me wrong, i want the indicator to be available and displayed not hidden!Foret
<style name="Base.Widget.Design.TabLayout" parent="android:Widget"> <item name="tabBackground">@drawable/tab_background</item><item name="tabIndicatorColor">#000000</item> <item name="tabIndicatorHeight">5dp</item> </style>Crumple
my god finally thanks, simple, easy.. I feel stupid now for even askingForet
Is it possible to have a custom background like the above and keep the ripple effect as well? (on tap ripple)Amalamalbena
this code is not working for custom tab view while for default tablayout its working fineImaret
This approach have the downside that applies to all the TabLayouts in your application. In some cases you really don't want that, so the approach to use a <selector> and set is as the tabLayout background could be preferableApocopate

© 2022 - 2024 — McMap. All rights reserved.