Skip to content

Commit 05f3672

Browse files
beero's agentBeerosagos
authored andcommitted
android: align system bars with app theme
The colors of both the status and the navigation bars and their icons were not always matching the app background. Also, forcing the app theme in the settings and then switching the OS theme was sometime causing the bar icons to change and blend with the bars. This commit does the following: - Drives the colors of both bars and icons in the setDarkTheme method to avoid misalignemnts with the app theme - Removes the onConfigurationChanged overriding, which was conficting with the implemented theme handling, which is delegated to the frontend DarkmodeProvider. Listening of OS theme changes in the activity must be avoided as it could fire unwanted changes in the app theme. - Improve the app static styles, to reduce the color flickering before setDarkTheme() is called for the first time. - Apply matching backgrounds to the root layout and WebView so the bars blend with the app in both light and dark modes.
1 parent ac2f42f commit 05f3672

File tree

4 files changed

+48
-28
lines changed

4 files changed

+48
-28
lines changed

frontends/android/BitBoxApp/app/src/main/java/ch/shiftcrypto/bitboxapp/MainActivity.java

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
import android.content.Context;
55
import android.content.Intent;
66
import android.content.ServiceConnection;
7-
import android.content.res.Configuration;
87
import android.net.Uri;
8+
import android.os.Build;
99
import android.os.Bundle;
1010
import android.os.IBinder;
1111
import android.view.View;
12+
import android.view.Window;
1213
import android.view.WindowManager;
1314
import android.webkit.WebView;
1415

@@ -50,37 +51,46 @@ public void onServiceDisconnected(ComponentName arg0) {
5051
}
5152
};
5253

53-
@Override
54-
public void onConfigurationChanged(Configuration newConfig) {
55-
int currentNightMode = newConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK;
56-
switch (currentNightMode) {
57-
case Configuration.UI_MODE_NIGHT_NO:
58-
// Night mode is not active, we're using the light theme
59-
setDarkTheme(false);
60-
break;
61-
case Configuration.UI_MODE_NIGHT_YES:
62-
// Night mode is active, we're using dark theme
63-
setDarkTheme(true);
64-
break;
65-
}
66-
super.onConfigurationChanged(newConfig);
67-
}
68-
6954
public void setDarkTheme(boolean isDark) {
70-
int flags = getWindow().getDecorView().getSystemUiVisibility(); // get current flag
55+
int statusBarColor = ContextCompat.getColor(
56+
getApplicationContext(),
57+
isDark ? R.color.colorPrimaryDark : R.color.colorPrimary
58+
);
59+
// Pre-Oreo devices cannot render light nav bar icons, so fall back to dark bar to keep icons visible.
60+
boolean supportsLightNavIcons = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O;
61+
int navBarColor = supportsLightNavIcons
62+
? statusBarColor
63+
: ContextCompat.getColor(getApplicationContext(), R.color.colorPrimaryDark);
64+
65+
Window window = getWindow();
66+
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
67+
window.setStatusBarColor(statusBarColor);
68+
window.setNavigationBarColor(navBarColor);
69+
70+
View decorView = window.getDecorView();
71+
int flags = decorView.getSystemUiVisibility(); // get current flag
7172
if (isDark) {
7273
Util.log("Dark theme");
73-
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
74-
getWindow().setStatusBarColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimaryDark));
75-
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
76-
flags &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR; // remove LIGHT_STATUS_BAR to flag
77-
getWindow().getDecorView().setSystemUiVisibility(flags);
74+
flags &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR; // remove LIGHT_STATUS_BAR flag
75+
if (supportsLightNavIcons) {
76+
flags &= ~View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
77+
}
7878
} else {
7979
Util.log("Light theme");
80-
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
81-
getWindow().setStatusBarColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));
82-
flags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR; // add LIGHT_STATUS_BAR to flag
83-
getWindow().getDecorView().setSystemUiVisibility(flags);
80+
flags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR; // add LIGHT_STATUS_BAR flag
81+
if (supportsLightNavIcons) {
82+
flags |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
83+
}
84+
}
85+
decorView.setSystemUiVisibility(flags);
86+
87+
View root = findViewById(R.id.root_layout);
88+
WebView vw = findViewById(R.id.vw);
89+
if (root != null) {
90+
root.setBackgroundColor(statusBarColor);
91+
}
92+
if (vw != null) {
93+
vw.setBackgroundColor(statusBarColor);
8494
}
8595
}
8696

@@ -93,7 +103,6 @@ protected void onCreate(Bundle savedInstanceState) {
93103
if (actionBar != null) {
94104
actionBar.hide(); // hide title bar with app name.
95105
}
96-
onConfigurationChanged(getResources().getConfiguration());
97106
setContentView(R.layout.activity_main);
98107
final WebView vw = findViewById(R.id.vw);
99108

frontends/android/BitBoxApp/app/src/main/res/layout/activity_main.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
xmlns:tools="http://schemas.android.com/tools"
55
android:layout_width="match_parent"
66
android:layout_height="match_parent"
7+
android:id="@+id/root_layout"
78
android:keepScreenOn="true"
89
android:fitsSystemWindows="true"
910
tools:context="ch.shiftcrypto.bitboxapp.MainActivity">
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<resources>
2+
<style name="AppTheme" parent="BaseAppTheme">
3+
<item name="android:statusBarColor">@color/colorPrimaryDark</item>
4+
<item name="android:navigationBarColor">@color/colorPrimaryDark</item>
5+
<item name="android:windowLightStatusBar">false</item>
6+
<item name="android:windowLightNavigationBar">false</item>
7+
</style>
8+
</resources>

frontends/android/BitBoxApp/app/src/main/res/values-v23/styles.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
<!-- extend the base theme to add styles available only with API level 23+ -->
33
<style name="AppTheme" parent="BaseAppTheme">
44
<item name="android:statusBarColor">@color/colorPrimary</item>
5+
<item name="android:navigationBarColor">@color/colorPrimary</item>
56
<item name="android:windowLightStatusBar">true</item>
7+
<item name="android:windowLightNavigationBar">true</item>
68
</style>
79
</resources>

0 commit comments

Comments
 (0)