본문 바로가기

안드로이드

[Android] 상태바 색상 변경 (api 21 이상)

상태바 배경색상 변경시, 배경위에 표시되는 아이콘 및 텍스트 색도 배경에 맞게 조정이 필요함

 

상태바 색상 변경 클래스

public class StatusBarUtil {
    public enum StatusBarColorType {
        // 색 지정
        WHITE_STATUS_BAR(R.color.color_white),
        DEFAULT_STATUS_BAR(R.color.color_f9957f);

        private int backgroundColorId;

        StatusBarColorType(int backgroundColorId) {
            this.backgroundColorId = backgroundColorId;
        }

        public int getBackgroundColorId() {
            return backgroundColorId;
        }
    }

    public static void setStatusBarColor(Activity activity, StatusBarColorType colorType) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            // 상태바 텍스트 색 지정
            if(colorType.getBackgroundColorId() == R.color.color_f9957f) {
                // 흰색
                activity.getWindow().getDecorView().setSystemUiVisibility(0);
            } else {
                // 검은색
                activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            }

            // 상태바 배경 색 지정
            activity.getWindow().setStatusBarColor(ContextCompat.getColor(activity, colorType.getBackgroundColorId()));
        }
    }
}

 

onCreate내 호출

StatusBarUtil.setStatusBarColor(this, StatusBarUtil.StatusBarColorType.WHITE_STATUS_BAR);