Search code examples
androidandroid-fragmentsandroid-statusbar

How can i change the colour of the status bar in the particular fragment?


My question is i want to change the colour of the status bar in the particular fragment.and in all other fragment the colour should be same.Example like in all the fragments my status bar colour is red.but in the particular fragment i want to change it to black. can anyone tell me how can i do this ?

This is the onCreateView Method of my fragment :-

 @Override
    public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
        final View view = (View) inflater.inflate(R.layout.slide_user_image, container, false);

        Window window = getActivity().getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(getActivity().getResources().getColor(R.color.black));


        return view;
    }

Solution

  •  Window window = getActivity().getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(getActivity().getResources().getColor(R.color.black));
    

    Write this code onResume() of fragment, And revert color on onPause() method, So that application will have old statusbarcolor in other fragments.