Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set status bar color dynamically in android

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]);
    }
like image 870
developer Avatar asked Dec 02 '15 12:12

developer


1 Answers

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]

like image 122
androgo Avatar answered Nov 15 '22 19:11

androgo