mr.klood4
mr.klood4

Reputation: 93

how change navigation bar color?

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

Upvotes: 6

Views: 10718

Answers (4)

Mia
Mia

Reputation: 1277

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

Upvotes: 1

Lokesh Desai
Lokesh Desai

Reputation: 2677

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));
}

Upvotes: 10

GiuseppeC
GiuseppeC

Reputation: 11

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.

Upvotes: 1

D_Alpha
D_Alpha

Reputation: 4069

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));
}

Upvotes: 2

Related Questions