Android: Apply different themes to fragments of one activity

14,121

Solution 1

Many fragments (such as PreferenceFragment) read the styled attributes directly from the Context returned by Fragment.getContext() method, so you might need to override that too:

private var themedContext: Context? = null

override fun onAttach(context: Context) {
    super.onAttach(context).also {
        themedContext = ContextThemeWrapper(context, R.style.ThemeForThisFragment)
        // if you want to apply a theme overlay:
        // themedContext.theme.applyStyle(R.style.MyThemeOverlay, true)
    }
}

override fun onDetach() {
    super.onDetach()
    themedContext = null
}

override fun getContext(): Context? {
    return themedContext ?: super.getContext()
}

Solution 2

Try LayoutInflater localInflater = inflater.from(contextThemeWrapper); instead.

Share:
14,121

Related videos on Youtube

Jonas
Author by

Jonas

💻→📱→🎉

Updated on September 16, 2022

Comments

  • Jonas
    Jonas over 1 year

    What I want to do:

    I want each Fragment of my MainActivity to use a different theme, so that the ActionBar has different background-colors, depending on the visible Fragment.

    The Situation:

    I created a MainActivity which uses the Tabs + Swipe Navigation. I Added 7 Tabs (=7 Fragments). I created one Theme which should be applied only to the first Fragment (fragment_main_1).

    Here the Theme:

    <resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="Blue" parent="android:Theme.Holo.Light">
        <item name="android:actionBarStyle">@style/Blue.ActionBarStyle</item>
    </style>
    
    <style name="Blue.ActionBarStyle" parent="android:Widget.Holo.Light.ActionBar">
        <item name="android:titleTextStyle">@style/Blue.ActionBar.TitleTextStyle</item>
        <item name="android:background">#33B5E5</item>
    </style>
    
    <style name="Blue.ActionBar.TitleTextStyle" parent="android:TextAppearance.Holo.Widget.ActionBar.Title">
        <item name="android:textColor">#FFFFFF</item>
    </style>
    </resources>
    

    After creating 6 more Themes it should be possible to swipe through the Tabs while the ActionBar changes its background-color automatically.

    What didn't work:

    Adding those lines (which I found here on stackoverflow) to the Fragment1.java:

    // create ContextThemeWrapper from the original Activity Context with the custom theme
    final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.Blue);
    
    // clone the inflater using the ContextThemeWrapper
    LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);
    
    // inflate the layout using the cloned inflater, not default inflater
    return localInflater.inflate(R.layout.fragment_main_1,container, false);
    

    I hope you can help me:) Thank you.

    • jcaruso
      jcaruso over 10 years
      Did you ever find a solution?
  • Jonas
    Jonas about 11 years
    Thanks for your fast reply, but that did't help :(
  • Reza Mohammadi
    Reza Mohammadi almost 3 years
    This is awesome because you create new windows like dialogs using context attached to the fragment, it's better to override getContext in the first place. And to change style of the fragment view itself, you just use inflater.cloneInContext(context) to inflate the view inside onCreateView.