Fragment theme not appliying - android

I would like to change dynamically the theme of my Fragment.
I have tried these solutions:
1st try, set the getTheme method on the activity. It only changes the colors of the activity:
#Override
public Resources.Theme getTheme() {
Resources.Theme theme = super.getTheme();
theme.applyStyle(R.style.AppTheme_RED, true);
return theme;
}
2nd try not changing anything:
final Context contextThemeWrapper = new
ContextThemeWrapper(getActivity(), R.style.AppTheme_RED);
LayoutInflater localInflater = inflater.from(contextThemeWrapper);
final View view = localInflater.inflate(R.layout.fragment_workout_list, container, false);
3rd try work only for activity, not fragment:
setTheme(R.style.AppTheme_RED);
Any help is welcomed.
Thanks in advance.
Edit: I have found that floatingbutton have the good color when i apply theme to fragment.

as google says we must inflate our theme before super.oncreate()
inside your activity or fragment and in oncreate() or on oncreateview() simply add the code below:
setTheme(R.style.AppTheme_RED);

Related

How can I set theme for a fragment programmatically?

Description:
I had been changing my activities themes using manifests or, in some cases, when I have to use a personalized theme, programmatically. This is what I was doing:
setTheme(R.style.MyTheme);
I have an activity with switching fragments (Fragment A and Fragment B). I need to set programmatically Theme A to Fragment A and Theme B to Fragment B.
Problem:
I've realized that my setTheme I use for my activities, is not working for fragments.
I've tried changing the theme in my activity when fragment changes, but this didn't work.
What can I do to solve this?
Edit:
My "father" fragment JAVA code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_contenedor_perfil_alumno, container, false);
viewPagerr = view.findViewById(R.id.view_pagerr);
return view;}
You could create a new inflater with the theme that you want and use it to inflate the root of fragment.
In your onCreateView() method,
val inflater = LayoutInflater.from(ContextThemeWrapper(context, R.style.some_theme))
return inflater.inflate(R.layout.your_fragment_layout, parent, false)
For Java,
LayoutInflater themedInflater = LayoutInflater.from(new
ContextThemeWrapper(context, R.style.some_theme));
View view = themedIinflater.inflate(R.layout.fragment_contenedor_perfil_alumno, container, false);
// ... your other code whatever that is
return view;

Can not set theme/style in FragmentDialog

class BottomBarFragment : BottomSheetDialogFragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val contextThemeWrapper = ContextThemeWrapper(getActivity(), R.style.Theme_BaseDarkTheme)
val localInflater = inflater.cloneInContext(contextThemeWrapper)
binding = FragmentBottomBarBinding.inflate(localInflater, container, false)
// tried setStyle also
setStyle(0, R.style.Theme_BaseDarkTheme)
}
}
style
<style name="Theme.BaseDarkTheme" parent="Theme.AppCompat">
<item name="dividerColor">#color/divider_dark</item>
</style>
in fragment dialog layout
<LinearLayout
style="#style/llParent"
android:background="?attr/dividerColor"
>
According to this, my layout should be dark, but it is light always. What am I doing wrong?
The documentation for setStyle() says this:
Call to customize the basic appearance and behavior of the fragment's
dialog. This can be used for some common dialog behaviors, taking care
of selecting flags, theme, and other options for you. The same effect
can be achieve by manually setting Dialog and Window attributes
yourself. Calling this after the fragment's Dialog is created will
have no effect.
Fragment's Dialog will be created after onCreate() and before onCreateView().
Try calling setStyle() from the onCreate() method
Basicly, if you are trying to use something, what have "dialog" in name, you should build dialog. To do this, there is one method like onCreateDialog().
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),your_style);
...
return builder.create();
}
Otherwise, you should extend just a Fragment, and there you can call in onCreateView() something like this:
Context contextThemeWrapper = new ContextThemeWrapper(getActivity(),your_style);
LayoutInflater newInflater = inflater.cloneInContext(contextThemeWrapper);
View view = newInflater.inflate(your_layout,container,false);
Sorry for java code, but there should be similar code.

Showing Fragment title in custom Actionbar

i want to use custom actionbar with fragment title.but this method only display application name for all fragments.i need to use appropriate fragment name in actionbar.
getActivity().getActionBar().setDisplayShowCustomEnabled(true);
getActivity().getActionBar().setDisplayShowTitleEnabled(false);
LayoutInflater inflator = LayoutInflater.from(getActivity());
View v = inflator.inflate(R.layout.title, null);
//if you need to customize anything else about the text, do it here.
//I'm using a custom TextView with a custom font in my layout xml so all I need to do is set title
((TextView)v.findViewById(R.id.title)).setText(getActivity().getTitle());
//assign the view to the actionbar
getActivity().getActionBar().setCustomView(v);
You just need to define a method which will update the title of action bar and call that method from ever activity / Fragment's onResume()
In activity :
#Override
public void onResume() {
// For activity
setActionbarTitle("title");
// For Fragment
((ParentActivtyNAme)getActivty).setActionbarTitle("title");
}
setActionbarTitle Method :
public void setActionbarTitle(String title) {
textTitle.setText(title);
}
And initialize textTitle when you set Custom View in Actionbar :
textTitle= ((TextView)v.findViewById(R.id.title));
Hope it helps you ツ

Android: Apply different themes to fragments of one activity

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.

Android - how to re-inflate View hierarchy

I'm having problems making my Activity respond to a theme change (i.e. Theme_Dark change to Theme_Light).
In the Activity this code below works fine, and the theme is changed when the Activity is created (method getPreferenceTheme() just gets the theme preference value that was set via a PreferenceActivity).
protected void onCreate(Bundle savedInstanceState) {
setTheme(getPreferenceTheme());
super.onCreate(savedInstanceState);
setContentView(R.layout.controls);
}
But how can I dynamically change the theme ? So after I change the theme in a PreferenceActivity and return to the main Activity how can I get it to change?
I know that I can re-start the Activity to do this (calling onCreate() again), but I didn't want to do this and have heard that it is possible to "re-inflate the view hierarchy" in onResume() - how do I do this ?
I tried the following (a stab in the dark) but with no joy.
protected void onResume() {
super.onResume();
LayoutInflater inflater = LayoutInflater.from (this);
View v = inflater.inflate (R.layout.controls, null);
setTheme(getPreferenceTheme());
setContentView(v);
}
Any help much appreciated,
M.
Try the "recreate" Activity method.
http://developer.android.com/reference/android/app/Activity.html#recreate()
Call it after you have set the new theme.

Categories

Resources