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.
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()
}
Try LayoutInflater localInflater = inflater.from(contextThemeWrapper);
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With