Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how change navigation bar color?

Tags:

java

android

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

like image 621
mr.klood4 Avatar asked Dec 24 '17 11:12

mr.klood4


People also ask

How do I change the color of my navigation bar android?

Steps to Change the Navigation Bar on Android Smartphone Once you give permissions to the navbar apps, you will be able to use the widgets. On the top, you will get the color widget which adds color to the navigation bar according to the app which is currently opened.

How do I change my navigation bar background?

In order to change the hub navigation bar color, we can go to site settings of hub site>Change the look>under the header section>Background> select a theme color to change the background color of your site header.

How do I change the color of my status bar?

Step 1: After opening the android studio and creating a new project with an empty activity. Step 2: Navigate to res/values/colors. xml, and add a color that you want to change for the status bar.


4 Answers

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));
}
like image 186
Lokesh Desai Avatar answered Oct 24 '22 00:10

Lokesh Desai


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));
}
like image 21
D_Alpha Avatar answered Oct 24 '22 01:10

D_Alpha


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.

like image 27
GiuseppeC Avatar answered Oct 24 '22 01:10

GiuseppeC


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
like image 39
Mia Avatar answered Oct 24 '22 00:10

Mia