SetStatusBarColor doesn't work [Lollipop]

How do I change the color of the status bar? I'm building with 5.0 and my target sdk is 21 and this code doesn't work ...

setContentView(R.layout.main_activity);
if (Build.VERSION.SDK_INT >= 21){
    try {
        getWindow().setStatusBarColor(Color.RED);

    } catch (Exception e) {
        e.printStackTrace();
    }
}   

      

+3


source to share


1 answer


The documentation says about the setStatusBarColor method, which

"For this to take effect, the window must paint system bar backgrounds with FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS and FLAG_TRANSLUCENT_STATUS must not be set."



So, it worked for me (notice the "addFlags" line in the code):

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

      

+8


source







All Articles