Android: Apply different themes to fragments of one activity
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.
Related videos on Youtube
Comments
-
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 over 10 yearsDid you ever find a solution?
-
-
Jonas about 11 yearsThanks for your fast reply, but that did't help :(
-
Reza Mohammadi almost 3 yearsThis 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.