0

I want to apply custom themes to my tab layout but I can't get it to work, a couple stipulations I have are :

  1. It mustn't be programmatic, I want to be able call setTheme at the very beginning of my activity or fragment and not have to do any other work

  2. The theme must be setup in themes and be changeable as I have 6 themes, some of these are light and some are dark

WHAT I'M CURRENTLY TRYING

What I've been trying to do is set up a theme in my styles.xml file and then have smaller themes set up for different widgets that i can register in my main style, like a parent sibling relationship, so here is my blue theme/style (parent)

<style name="MyAppBlueTheme" parent="Theme.AppCompat">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/primary_blue</item>
    <item name="colorPrimaryDark">@color/primary_dark_blue</item>
    <item name="colorAccent">@color/accent_blue</item>
    <item name="android:textColor">@color/primary_text_blue</item>
    <item name="android:colorBackground">@color/background_light</item>
    <item name="android:tabWidgetStyle">@style/Blue.TabLayout</item>
</style>

Here I declare tabWidgetStyle as Blue.TabLayout as a sort of sibling, here is what that tab layout style looks like (sibling)

<style name="Blue.TabLayout" parent="Base.Widget.Design.TabLayout">
    <item name="tabSelectedTextColor">@color/selected_textBlue</item>
    <item name="tabIndicatorColor">@color/accent_blue</item>
    <item name="tabBackground">@color/primary_blue</item>
</style>

And this has its own sibling for the text

<style name="CustomTabTexStyleBlue" parent="TextAppearance.Design.Tab">
    <item name="android:textColor">@color/primary_text_blue</item>
</style>

I set the theme by using setTheme at the beginning of my activity or fragment and everything else looks fine except my tab layout which appears transparent, can anyone give me some insight into whats going on here please

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
martinseal1987
  • 1,862
  • 8
  • 44
  • 77
  • The `android:tabWidgetStyle` attribute is not for `TabLayout`. If you want to style `TabLayout`, have a look at [this answer](https://stackoverflow.com/a/31465146). – Mike M. Oct 20 '18 at 13:31
  • so its saying create a style in styles.xml and then apply it via xml which means if i wanted to do this for many themes i would have to do that programmatically – martinseal1987 Oct 20 '18 at 13:49
  • I don't follow. What would you be doing programmatically, other than the `setTheme()` call? – Mike M. Oct 20 '18 at 13:55
  • I have multiple themes, so the approach in that link has a style defined in styles.xml and sets that style in the tab layout view in their XML layout file, that value can't be changed at runtime – martinseal1987 Oct 20 '18 at 14:33
  • Sorry that didn't explain it clearly I mean I would have to set the things I want changed like background colour programmatically rather than just setting a theme and have that do the work for me – martinseal1987 Oct 20 '18 at 14:37
  • 1
    Nah, we should be able to do it in XML. [Here's a quick test project](https://drive.google.com/file/d/1_LohpvWA66wBGmizzjMKCSA9zEedzhVw/view?usp=drivesdk) I threw together on my mobile IDE. Give it a try. I stuck all of the resource stuff into `styles.xml`, just to keep it simple. It doesn't really matter which file any given resource is in, as long as it's under a `values*/` directory, but lemme know if Android Studio complains. – Mike M. Oct 20 '18 at 15:31
  • yeah that works, what magic is this? lol this is really cool Ive not seen an approach like this before thank you very much if you want to add it as an answer id be happy to accept it many many thanks – martinseal1987 Oct 20 '18 at 15:57
  • Ah, nifty. Good to hear. I do want to thoroughly test that, though, especially on older Android versions, before I post it as a complete answer. I'll let you know if I find any issues, but you might want to make sure it works, as well, for all versions that you support. Thanks! Glad it's working for you (at least for now). Cheers! – Mike M. Oct 20 '18 at 16:14
  • i have it working back to 5.0 so far and ill be testing back to ics – martinseal1987 Oct 20 '18 at 16:32

0 Answers0