11

How to set status bar color dynamically for an application, am using view pager while swiping (horizontally) status bar color and title bar and button should change the color . as per my code title and button color changing perfectly ,but the issue is status bar color taking next color from array list. how to fix that issue can anyone help me. here is my code

 private int[] colors = new int[]{0xffffd200, 0xff37beb7, 0xff00ccff, 0xff8585c1, 0xfff2a03c, 0xff2a80b9, 0xfff15972,
        0xffe9776c, 0xff9dcc96,0xff76c069};

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Window window = ((Activity) context).getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

        int coloring = position % colors.length;
        int new_color = colors[coloring];
        window.setStatusBarColor(new_color);
        title_bar.setBackgroundColor(new_color);
        set_share.setBackgroundColor(new_color);

    }
    else{

        int color = position % colors.length;
        itemView.setBackgroundColor(colors[color]);
        title_bar.setBackgroundColor(colors[color]);
        set_share.setBackgroundColor(colors[color]);
    }
developer
  • 319
  • 1
  • 4
  • 15
  • Try this: [How to set status bar color dynamically in Android](http://stackoverflow.com/a/34072723/5475941) – Mohammad Dec 03 '15 at 19:46

3 Answers3

22

To change status bar color use setStatusBarColor(int color). According the javadoc, we also need set some flags on the window.

Working snippet of code:

Window window = activity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(activity.getResources().getColor(R.color.example_color));

This is taken from following reference: How to change status bar color to match app in Lollipop? [Android]

Community
  • 1
  • 1
androgo
  • 564
  • 2
  • 8
  • setting status bar color is not a problem for me, title bar and button color is different from status bar color but am passing same color, how to resolve that issue. i want all color should be same – developer Dec 02 '15 at 12:44
  • For button useButton11.setBackgroundColor(getResources().getColor(R.color.example_color); and for title bar you can use two methods: 1. requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.main); getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle); 2 is if you are using "ToolBar" you can direclt set mToolbar.setBackground() – androgo Dec 02 '15 at 12:57
  • dude i dont know whether you understood my problem , above mention my code is working fine but issue is example {red,green,blue,yellow} while swiping page titlebar and button is taking red color , but the status bar is taking (next color) green color i dont know what is the issue – developer Dec 02 '15 at 13:08
  • Okay, So when you use SwipeViews or ViewPager, It Initialise its previous fragment and its next fragement , so when you are going from one view pager to other, status bar is given the Last Initialized Fragment value to Status Bar, Just use a variable to set Color only when user is there. – androgo Dec 02 '15 at 13:28
  • So it will solve your problem, just take current ViewPager and Change only color according to that only. – androgo Dec 02 '15 at 13:30
  • Working perfectly . @androgo thanks for ur previous message. I have fixed my issues according to your previous comment. – developer Dec 11 '15 at 10:31
8

coming to Status bar color, you can only add it to devices with API level more than 21. To those devices which satisfy this condition you can change the StatusBar color dynamically as shown below.

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        window.setStatusBarColor(getResources().getColor(R.color.Statusbar));
    }
aravindkanna
  • 663
  • 1
  • 7
  • 25
2

When I wanted to set the status bar color, I used https://github.com/jgilfelt/SystemBarTint

I used it like that :

public static void colorStatusBar(Window window, Activity activity) {
    Log.v(Constants.LOG_TAG, "Start defining color bar status");
    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {      
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        SystemBarTintManager tintManager = new SystemBarTintManager(activity);
        tintManager.setStatusBarTintEnabled(true);

        tintManager.setTintColor(activity.getResources().getColor(R.color.colorPrimaryDark));
    }
}

But beware that setting status bar color is only possible if your app runs on a phone with API >= 19

ImpSy
  • 106
  • 4