0

enter image description hereI have an Activity and a Fragment in my project.In the Activity I set the layout take the entire screen and in fragment I try to change the status bar color programmatically. The status bar is shown over the layout but the color is not changed.

Activity:

if (Build.VERSION.SDK_INT > 16) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);

        }

Fragment:

 if (Build.VERSION.SDK_INT > 16) {
            getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
            if (Build.VERSION.SDK_INT >= 21) {
                Log.e("statusbarcolor","changing..");
                getActivity().getWindow().setStatusBarColor(ContextCompat.getColor(getContext(),R.color.statusBarColor));
            }
        }
jobin
  • 1,311
  • 2
  • 21
  • 42
  • 1
    try this link http://stackoverflow.com/questions/22192291/how-to-change-the-status-bar-color-in-android and http://stackoverflow.com/a/26623245/2784663 – YLS Oct 24 '16 at 07:35
  • 2
    http://stackoverflow.com/questions/26496411/android-material-status-bar-color-wont-change – sasikumar Oct 24 '16 at 07:36
  • Make sure testing on =>21 – Bills Oct 24 '16 at 07:37
  • Why don't you just use the xml way to do it? – Johan De waal Oct 24 '16 at 07:38
  • @YLS I changed it but it did not work – jobin Oct 24 '16 at 07:46
  • I tried using getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); But it did not change – jobin Oct 24 '16 at 07:52

4 Answers4

3

I guess , Problem is your Build.VERSION.SDK_INT section .

In my case (Activity)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
    {
        Window window = activity.getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(Color.parseColor("#54d66a");
    }
Rushi Ayyappa
  • 2,620
  • 2
  • 13
  • 26
IntelliJ Amiya
  • 70,230
  • 14
  • 154
  • 181
0

Just try doing:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    Window window = getWindow();
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    window.setStatusBarColor(Color.BLUE);
}

Just tested this with ActionBarActivity and it works alright.

Note: Setting the FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag programmatically is not necessary if your values-v21 styles file has it set already, via:

<item name="android:windowDrawsSystemBarBackgrounds">true</item>

Create a method in your activity

public void updateStatusBarColor(String color){// Color must be in hexadecimal fromat
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(Color.parseColor(color));
    }
}

Call this method from your fragment

((ActivityName)getActivity()).updateStatusBarColor("#000000")

In given below using my application :

..................
 switch (position) {
        case 0 :
            fragment = new SampleOneFragment();
            color = R.color.ColorPrimaryDark;
            break;
        case 1:
            fragment = new SampleOneFragment();
            color = R.color.AlertRed_dark;
            break;
        case 2:
            fragment = new SampleOneFragment();
            color = R.color.ProgressBarPurple_dark;
            break;
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        getWindow().setStatusBarColor(ContextCompat.getColor(this, color));
    }  
    replaceFragment(fragment);
}
Manikandan K
  • 839
  • 9
  • 21
  • Why use **Deprecated** `ActionBarActivity` – IntelliJ Amiya Oct 24 '16 at 07:52
  • android version 22.1.0, the class ActionBarActivity is deprecated. You should use to AppCompatActivity. – Manikandan K Oct 24 '16 at 07:53
  • @Manikandan K.I tried your code,it still has not changed – jobin Oct 24 '16 at 08:02
  • I now used getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); getActivity().getWindow().setStatusBarColor(Color.parseColor("#8f1745")); – jobin Oct 24 '16 at 08:03
  • WhenI used the above code the status bar is shown,but the color has not changed – jobin Oct 24 '16 at 08:04
  • Please replace getActivity().getWindow().setStatusBarColor(Color.parseColor‌​("#8f1745")); to getActivity(). getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.LogsPBYellow_dark)); – Manikandan K Oct 24 '16 at 08:06
  • I have included the screenshot in my question – jobin Oct 24 '16 at 08:24
  • Are you using ToolBar? – Manikandan K Oct 24 '16 at 09:06
0

I did it finally.I simply used the below code in my fragment:

getActivity().getWindow().clearFlags(WindowManager.FLAG_FULL_SCREEN);
jobin
  • 1,311
  • 2
  • 21
  • 42
0

User this code

if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {

            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(getResources().getColor(R.color.my_statusbar_color));
        }
IntelliJ Amiya
  • 70,230
  • 14
  • 154
  • 181
Aniruddh Ambarkar
  • 1,010
  • 1
  • 11
  • 20