6

I want a status bar translucent and navigation bar other color not translucent like blue or white

My code

 <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
        <item name="android:navigationBarColor">@color/colorPrimary</item>
    </style>

Activity

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Window w = getWindow();
            w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        } 

the status bar translucent good but the navigation bar no change color . why ?

navigation bar

mr.klood4
  • 93
  • 1
  • 1
  • 5

4 Answers4

10

Ways to change navigation color:

values-v21/style.xml

<item name="android:navigationBarColor">@color/blue_color</item>

Programatically:

if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    getWindow().setNavigationBarColor(ContextCompat.getColor(this, R.color.primary));
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Lokesh Desai
  • 2,607
  • 16
  • 28
  • 1
    this worked but if add this code to change status bar translucent w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); not changed color navigation bar – mr.klood4 Dec 24 '17 at 11:42
  • means if you set status bar translucent than your navigation bar color not changing? – Lokesh Desai Dec 24 '17 at 11:44
  • 1
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Window w = getWindow(); w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); } put this in your activity's onCreate method before setContentView() hope this will work for you – Lokesh Desai Dec 24 '17 at 11:50
2

check my previous Answer it will definitely help you get the result.

You can achieve this in two ways- either using style or Activity.

values-v21/style.xml

<item name="android:navigationBarColor">@color/navigationbar_color</item>

Using Compat Library in Activity-

if (Build.VERSION.SDK_INT >= 21) {
    getWindow().setNavigationBarColor(ContextCompat.getColor(this, R.color.primary));
}
D_Alpha
  • 4,039
  • 22
  • 36
  • This solution works for me while splsh Screen is showing. However, I'd like to get back to the default device navigation bar color, since many devices different colors (mostly between white and black) use. Is there any solution? Many thanks in advance! – Mark Delphi Nov 22 '20 at 02:23
1

Based on my understanding of "FLAG_LAYOUT_NO_LIMITS" in https://developer.android.com/reference/android/view/WindowManager.LayoutParams.html [Window flag: allow window to extend outside of the screen] the usage of such flag pushes out of the window both the status bar and the navigation bar. In fact using your style.xml file and adding the row

<color name="myWindowBackground">#D7DEB5</color> 

to change the white color window background, it is possible to note both the status bar symbols and the navigation bar symbols but not their bars . Moreover using the following code:

int statusBarColor = w.getStatusBarColor();
String hexStatusBarColor = String.format("#%08X", (0xFFFFFFFF & statusBarColor));
int navigationBarColor = w.getNavigationBarColor();
String hexNavigationBarColor = String.format("#%08X", (0xFFFFFFFF & navigationBarColor));
Log.d("FLAGS", "statusBarColor: " + hexStatusBarColor + " -- navigationBarColor: " + hexNavigationBarColor);

you see that the statusBarColor ("#00000000" fully transparent) and the navigationBarColor are correctly set but not shown since out of the window.

Now without using the flag "FLAG_LAYOUT_NO_LIMITS" you get a transparent statusBar (same color as the windowBackground) and as well the wished navigation bar color, but not sure at this point what else the code is trying to get by using this flag.

GiuseppeC
  • 11
  • 2
1

With FLAG_LAYOUT_NO_LIMITS been set, the view will be extended to status bar and navigation bar, which means you can't set the color by Window.setStatusBarColor. Our App has taken the control of drawing the view behind the navigation bar. We need to set the color on the exactly spot, right behind the navigation bar.

We need to Handle overlaps using insets

by setting padding bottom, you can make the navigation backgroud color by below:

window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
                WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)

    val resourceId: Int = resources.getIdentifier("config_navBarInteractionMode",
                                                  "integer",
                                                  "android")

    ViewCompat.setOnApplyWindowInsetsListener(window.decorView) { view, insets ->

        val systemBarInsets = insets.getInsets(WindowInsetsCompat.Type.systemBars())

        if (systemBarInsets.bottom > 0) {

            view.setPadding(0,
                            systemBarInsets.top,
                            0,
                            systemBarInsets.bottom)

        }

        if (resourceId == 0) {
            view.setBackgroundColor(Color.LTGRAY)
        }

        return@setOnApplyWindowInsetsListener insets

    }

    val windowInsetsControllerCompat = WindowInsetsControllerCompat(window,
                                                                    window.decorView)
    windowInsetsControllerCompat.isAppearanceLightNavigationBars = true
    windowInsetsControllerCompat.isAppearanceLightStatusBars = true
Mia
  • 1,226
  • 1
  • 19
  • 29