Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigation bar buttons color

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?

like image 688
wownis Avatar asked Jul 04 '17 12:07

wownis


People also ask

How do I change the navigation color on my Android?

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.


1 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;
}
like image 130
guy.gc Avatar answered Oct 04 '22 21:10

guy.gc