Martin Seal
Martin Seal

Reputation: 636

Android how can i change the tab color from style or theme

Hi just as the title suggests I'm sure this should be simple but alas it isn't working for me, I'm not looking for anything fancy like onTabSelected etc I just want to set the color of the entire widget based on the theme, right now im trying to do the following, which I'm not sure if is correct but its not working,

It looks like this is what I need:

<item name="tabBackground">@color/colorPinkPrimary</item>

But I've tried adding this to my theme in 2 ways and neither work, the first way was to just add this directly to my theme as it is but no joy, the second way I tried was to add another theme.

<style name="CustomTabLayoutStylePink" parent="Base.Widget.Design.TabLayout">
    <item name="tabSelectedTextColor">@color/selected_textPink</item>
    <item name="tabIndicatorColor">@color/colorPinkAccent</item>
    <item name="tabTextAppearance">@style/CustomTabTexStylePink</item>
    <item name="tabBackground">@color/colorPinkPrimary</item>
</style>

And refer it from my theme like this (just to reiterate I don't know if this is best practice or even feasible, it just looked right)

<item name="android:tabWidgetStyle">@style/CustomTabLayoutStylePink</item>

The one that works is the following:

<item name="android:background">@color/colorPinkPrimary</item>

This works for every view in my entire app even down to the preference activity textviews.

Upvotes: 1

Views: 193

Answers (2)

Martin Seal
Martin Seal

Reputation: 636

ok so what i wanted wasn't possible instead ive set up a preference to record what theme should be applied, and depending on what theme is selected, color the tabs accordingly like this

 private void setupTabBackground(){

    SharedPreferences sharedPreferences = PreferenceManager
            .getDefaultSharedPreferences(getApplicationContext());
    //  Create a new boolean and preference and set it to true
    prefThemeString = sharedPreferences.getString("THEME SELECTED","BLUE");
    switch (prefThemeString){
        case "BLUE":
            tabLayout.setBackgroundColor(getResources().getColor(R.color.colorPrimaryBlue));
            break;
        case "PINK":
            tabLayout.setBackgroundColor(getResources().getColor(R.color.colorPinkPrimary));
            break;
    }

}

i think there should also be a shared preference listener in this activity but i can address that later

Upvotes: 1

Zhi Kai
Zhi Kai

Reputation: 1587

tabBackground should be in your XML file and not inside the style.

<android.support.design.widget.TabLayout
    ...
    app:tabBackground="@color/colorPinkPrimary"
    ...
/>

Upvotes: 1

Related Questions