Skip to content

Commit 8c24903

Browse files
authored
feat(statusbar)!: make DEFAULT change style based on the Android theme (#2166)
1 parent 023f53b commit 8c24903

File tree

4 files changed

+28
-11
lines changed

4 files changed

+28
-11
lines changed

status-bar/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,11 +270,11 @@ of the space underneath it.
270270

271271
#### Style
272272

273-
| Members | Value | Description | Since |
274-
| ------------- | ---------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----- |
275-
| **`Dark`** | <code>'DARK'</code> | Light text for dark backgrounds. | 1.0.0 |
276-
| **`Light`** | <code>'LIGHT'</code> | Dark text for light backgrounds. | 1.0.0 |
277-
| **`Default`** | <code>'DEFAULT'</code> | The style is based on the device appearance. If the device is using Dark mode, the statusbar text will be light. If the device is using Light mode, the statusbar text will be dark. On Android the default will be the one the app was launched with. | 1.0.0 |
273+
| Members | Value | Description | Since |
274+
| ------------- | ---------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----- |
275+
| **`Dark`** | <code>'DARK'</code> | Light text for dark backgrounds. | 1.0.0 |
276+
| **`Light`** | <code>'LIGHT'</code> | Dark text for light backgrounds. | 1.0.0 |
277+
| **`Default`** | <code>'DEFAULT'</code> | The style is based on the device appearance. If the device is using Dark mode, the statusbar text will be light. If the device is using Light mode, the statusbar text will be dark. | 1.0.0 |
278278

279279

280280
#### Animation

status-bar/android/src/main/java/com/capacitorjs/plugins/statusbar/StatusBar.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.capacitorjs.plugins.statusbar;
22

3+
import android.content.res.Configuration;
34
import android.graphics.Color;
45
import android.os.Build;
56
import android.util.DisplayMetrics;
@@ -21,15 +22,13 @@ public class StatusBar {
2122
private int currentStatusBarColor;
2223
private final ChangeListener listener;
2324
private final AppCompatActivity activity;
24-
private final String defaultStyle;
25+
private String currentStyle = "DEFAULT";
2526

2627
public StatusBar(AppCompatActivity activity, StatusBarConfig config, ChangeListener listener) {
2728
// save initial color of the status bar
2829
this.activity = activity;
2930
this.currentStatusBarColor = activity.getWindow().getStatusBarColor();
3031
this.listener = listener;
31-
this.defaultStyle = getStyle();
32-
3332
setBackgroundColor(config.getBackgroundColor());
3433
setStyle(config.getStyle());
3534
setOverlaysWebView(config.isOverlaysWebView());
@@ -41,15 +40,27 @@ public StatusBar(AppCompatActivity activity, StatusBarConfig config, ChangeListe
4140
public void setStyle(String style) {
4241
Window window = activity.getWindow();
4342
View decorView = window.getDecorView();
44-
43+
this.currentStyle = style;
4544
if (style.equals("DEFAULT")) {
46-
style = this.defaultStyle;
45+
style = getStyleForTheme();
4746
}
4847

4948
WindowInsetsControllerCompat windowInsetsControllerCompat = WindowCompat.getInsetsController(window, decorView);
5049
windowInsetsControllerCompat.setAppearanceLightStatusBars(!style.equals("DARK"));
5150
}
5251

52+
private String getStyleForTheme() {
53+
int currentNightMode = activity.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
54+
if (currentNightMode != Configuration.UI_MODE_NIGHT_YES) {
55+
return "LIGHT";
56+
}
57+
return "DARK";
58+
}
59+
60+
public void updateStyle() {
61+
setStyle(this.currentStyle);
62+
}
63+
5364
@SuppressWarnings("deprecation")
5465
public void setBackgroundColor(int color) {
5566
Window window = activity.getWindow();

status-bar/android/src/main/java/com/capacitorjs/plugins/statusbar/StatusBarPlugin.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.capacitorjs.plugins.statusbar;
22

3+
import android.content.res.Configuration;
34
import com.getcapacitor.JSObject;
45
import com.getcapacitor.Logger;
56
import com.getcapacitor.Plugin;
@@ -49,6 +50,12 @@ private String styleFromConfig(String style) {
4950
}
5051
}
5152

53+
@Override
54+
protected void handleOnConfigurationChanged(Configuration newConfig) {
55+
super.handleOnConfigurationChanged(newConfig);
56+
implementation.updateStyle();
57+
}
58+
5259
@PluginMethod
5360
public void setStyle(final PluginCall call) {
5461
final String style = call.getString("style");

status-bar/src/definitions.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ export enum Style {
6969
* The style is based on the device appearance.
7070
* If the device is using Dark mode, the statusbar text will be light.
7171
* If the device is using Light mode, the statusbar text will be dark.
72-
* On Android the default will be the one the app was launched with.
7372
*
7473
* @since 1.0.0
7574
*/

0 commit comments

Comments
 (0)