Skip to content

Commit 315c389

Browse files
authored
Android: Set background color for status bar (#431)
1 parent 9bf9a92 commit 315c389

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

modules/statusbar/src/main/native/android/dalvik/DalvikStatusBarService.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, Gluon
2+
* Copyright (c) 2020, 2025, Gluon
33
*
44
* This program is free software: you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License as published by
@@ -29,8 +29,10 @@
2929

3030
import android.app.Activity;
3131
import android.content.Context;
32+
import android.graphics.drawable.ColorDrawable;
3233
import android.os.Build;
3334
import android.util.Log;
35+
import android.view.View;
3436
import android.view.Window;
3537
import android.view.WindowManager;
3638

@@ -45,7 +47,7 @@ public DalvikStatusBarService(Activity activity) {
4547
}
4648

4749
private void setColor(final int color) {
48-
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
50+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { // < 21
4951
Log.e(TAG, "StatusBar service is not supported for the current Android version");
5052
return;
5153
}
@@ -62,9 +64,27 @@ private void setColor(final int color) {
6264
@Override
6365
public void run() {
6466
Window window = activity.getWindow();
65-
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
66-
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
67-
window.setStatusBarColor(color);
67+
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { // <= 34
68+
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
69+
// make status bar transparent
70+
window.setStatusBarColor(0x00000000);
71+
// paint window
72+
window.setBackgroundDrawable(new ColorDrawable(color));
73+
} else { // >= 35
74+
View decorView = window.getDecorView();
75+
76+
// Get insets, before applying the color, as it will reset them
77+
int top = decorView.getPaddingTop();
78+
int right = decorView.getPaddingRight();
79+
int bottom = decorView.getPaddingBottom();
80+
int left = decorView.getPaddingLeft();
81+
82+
// Apply color
83+
decorView.setBackground(new ColorDrawable(color));
84+
85+
// Restore insets
86+
decorView.setPadding(left, top, right, bottom);
87+
}
6888
}
6989
});
7090
}

0 commit comments

Comments
 (0)