Home » Android » android – Navigation bar buttons color

android – Navigation bar buttons color

Posted by: admin May 14, 2020 Leave a comment

Questions:

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?

How to&Answers:

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