Search code examples
androidandroid-fragmentsandroid-toolbarandroid-statusbarandroid-color

How to display different statusbar color in different fragment


I have 20 fragments in my android application.

There are two fragments which have CollaspingToolbarLayout - i want this fragment's statusbar color as like their CollaspingToolbar color which is primarycolor

Other eighteen fragments have Toolbar - i want this fragment's statusbar color as like their toolbar color which is white

I can do it by adding this in each fragments but I have to add this in all the fragments.

GetActivity().getWindow().setStatusBarColor(//color here//);

Is there any other way to do so? Thank you in advance.


Solution

  • Your case is very specific.

    You can set StatusBar color back to white when closing your fragments which have CollapsingToolbarLayout. In this way, you do not need to touch other 18 fragments. You only touch only those 2 fragments.

    Note: I really recommend to have a parent activity or fragment which have dedicated methods for such actions. It makes your code clean and more readable.