1

I have created a function as below which sets fitSystemWindow attribute :

private void setFitSystemWindows() {
        try {
            if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.LOLLIPOP) {
                // Do something for lollipop and above versions
                View view = findViewById(R.id.relRoot);
                view.setFitsSystemWindows(true);
                view.setPadding(0, 0, 0, 0);

                Window window = getWindow();
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                window.setStatusBarColor(ContextCompat.getColor(CameraActivity.this, R.color.colorBlackTransperent));
            } else {
                // do something for phones running an SDK before lollipop
                View view = findViewById(R.id.relRoot);
                view.setFitsSystemWindows(false);
                view.setPadding(0, 0, 0, 0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Let me explain it :

Here, R.id.relRoot is the parent root RelativeLayout in layout xml file.

I am checking build version first and If build version is greater than Lollipop, am setting fitsSystemWindows to true and if not then setting it to false.

But, still when I run the application in devices > Lollipop, am getting my status bar color white and bottom soft view (which contains back,home and recent) also with the color white.

You can see I am also using method setStatusBarColor but it's not working.

What might be the issue ?

NOTE : Am checking in emulator device : NEXUS 5X API 27 (Android 8.1.0, API 27)

Jaimin Modi
  • 1,530
  • 4
  • 20
  • 72

2 Answers2

4

Here I'm set status bar color using this function which I have implemented. In Lollipop I have set status bar color black. in the above version, I have set my app theme color.

Function:-

fun setStatusBarColor(activity: Activity, color: Int) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            activity.window.statusBarColor = ContextCompat.getColor(activity, color)
            if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP || Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP_MR1) {
                activity.window.statusBarColor = ContextCompat.getColor(activity, android.R.color.black)
            }
        }
    }

Note:- This above function I have implemented in kotlin.

Java function:-

public void setStatus(Activity activity, int color){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            activity.getWindow().setStatusBarColor(ContextCompat.getColor(activity, color));
            if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP || Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP_MR1)
                activity.getWindow().setStatusBarColor(ContextCompat.getColor(activity, android.R.color.black));
        }
    }
Kaushal Panchal
  • 1,785
  • 1
  • 11
  • 27
1

Well, I tried your code and it worked for me. So I just can try to analyze your code snippet.

  1. what does the debugger say? Have you tried to set a mark next to the method? Where does the debugger go? Maybe the debugger jumps into the exception for some reason.

  2. correct if:

if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.LOLLIPOP) {
    // Do something for lollipop and above versions
}

You are not looking for lollipop and above but for Version above except lollipop. If you want to look for lollipop as well, use >=

  1. check the color. Have you tried to set another color (without transparency)?

  2. Context. have your tried getApplicationContext() or similiar?

I would advice you to use the debugger to find the issue. Maybe it's something with the code, maybe with the color. As I said, I tried your code and it worked for me.

Caetto
  • 43
  • 8