Reputation: 396
I have a problem with SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
somehow not working, but SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
is working. I'm working with Android API 28 at the moment.
So whats happening? On API 23 and below I get translucent Status Bar and Navigation Bar as expected. Between API 23 and API API 26 I get transculent Navigation Bar and Light Mode Status bar as expected. But on API 27 and above I get the Light Mode Status Bar but not the Light Mode Navigation Bar. It just is the normal black one and nothing changes.
Here is my MainActivity with my code to enable Translucent or Light Mode Status Bar and Navigation Bar based on the Android API Level (Note my comments describe what works and what doesn't):
View decorView = getWindow().getDecorView();
Window win = getWindow();
//Setup Status Bar and Nav Bar white if supported
if(Build.VERSION.SDK_INT >= 27) {
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);// <- works not
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); // <- works
}
else if (Build.VERSION.SDK_INT >= 23 && Build.VERSION.SDK_INT < 27) {
//this here works
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
win.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
} else {
//this here works
win.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
win.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
And this here is my style.xml:
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorWhite</item>
<item name="colorPrimaryDark">@color/colorWhite</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:textColorPrimary">@color/colorBlack</item>
<item name="android:fitsSystemWindows">true</item>
</style>
</resources>
Can you guys tell me what's missing? I use the exact same code for Navigation bar as I do for the Status Bar but only the Status Bar gets Light Mode. Thanks for every help
Upvotes: 2
Views: 6327
Reputation: 10299
In your code NAVIGATION_BAR
flag is getting override by STATUS_BAR
Flag.
To add both the flag replace.
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
With
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
Update: You can also specify the Color of the NavigationBar
getWindow().setNavigationBarColor(ContextCompat.getColor(this, R.color.primary));
Upvotes: 3
Reputation: 396
After hours of testing I figured it out! First you have to set your Navigation Bar to white in xml with target API above 26 in your style.xml:
<item name="android:navigationBarColor" tools:targetApi="27">@android:color/white</item>
After that you have to import this flag in your Main Activity:
import static android.view.WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS;
And in your Code you have to set the flags all toghether like this:
if(Build.VERSION.SDK_INT >= 27) {
decorView.setSystemUiVisibility(FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS |
View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR |
View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
And now if Im above API 26 I get Light Nav and Status Bar!
Upvotes: 7