Navigation bar buttons color

wownis picture wownis · Jul 4, 2017 · Viewed 9.2k times · Source

I want to change color of navigation buttons in my application, enter image description here

I tried with window.setNavigationBarColor(@ColorInt int color) but this method changing only background of bar. Any ideas?

Answer

guy.gc picture guy.gc · Apr 18, 2020

You can change the navigation color dynamically using the following function. Basically it checks if the given NavigationBar background color is light or dark and sets the appropriate theme to the buttons. Setting a specific color to the buttons is not possible.

private void setNavigationBarButtonsColor(Activity activity, int navigationBarColor) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        View decorView = activity.getWindow().getDecorView();
        int flags = decorView.getSystemUiVisibility();
        if (isColorLight(navigationBarColor)) {
            flags |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
        } else {
            flags &= ~View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
        }
        decorView.setSystemUiVisibility(flags);
    }
}

private boolean isColorLight(int color) {
    double darkness = 1 - (0.299 * Color.red(color) + 0.587 * Color.green(color) + 0.114 * Color.blue(color)) / 255;
    return darkness < 0.5;
}