Set status bar color dynamically in android

爷,独闯天下 提交于 2020-12-02 06:58:49

问题


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]);
    }

回答1:


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]




回答2:


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));
    }



回答3:


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



来源:https://stackoverflow.com/questions/34042232/set-status-bar-color-dynamically-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!