I'm having a bad struggle in trying to change the theme of my Android Activity in runtime.
This is what I'm trying to archieve: I have an activity (MainActivity) which contains three fragments. Two of those fragments should have a toolbar and a normal status bar, but the third one shouldn't have a toolbar and should have a transparent status bar with content behind it. So, I created two styles: the normal AppTheme with toolbar etc and another one called Fullscreen without those. Now, the theme should change when the fragments are being switched.
I have tried to just change the theme after inflating a new fragment, but that didn't work (obviously).
I also saw this question and a few others and they almost all contain the same answer which doesn't work for me.
I really hope one of you could help!
Thanks
Well you can modify Activity
theme and then recreate
it to see its effect
class YourActivity{
boolean isThirdFragmentVisible;
...
public void onNavigateToThirdFragment(){
setTheme(<full screen theme>);
isThirdFragmentVisible = true;
recreate();
}
...
public void onCreate(){
if(isThirdFragmentVisible){
showThirdFragment();
}
}
...
}
Any fragment has access to the attached activity. So, fragments can set any activity property, like toolbar and status bar.
Example:
Status bar:
In onViewCreated callback you can set this mode to not show the status bar:
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
To show the status bar in the other fragments, you can set it in the same way:
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
Toolbar:
In the same way, the toolbar can be set by any fragment using setSupportActionBar
activity method. More information > https://developer.android.com/training/appbar/setting-up.html#add-toolbar
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.