Skip to content

Commit

Permalink
Add adaptive navigation bar buttons color (#5860)
Browse files Browse the repository at this point in the history
Co-authored-by: Guy Carmeli <[email protected]>
  • Loading branch information
rverbytskyi and guyca committed Jan 21, 2020
1 parent 27ceea8 commit 6521177
Showing 1 changed file with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,27 @@ private void mergeNavigationBarOptions(NavigationBarOptions options) {
private void setNavigationBarBackgroundColor(NavigationBarOptions navigationBar) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && navigationBar.backgroundColor.canApplyValue()) {
int defaultColor = activity.getWindow().getNavigationBarColor();
activity.getWindow().setNavigationBarColor(navigationBar.backgroundColor.get(defaultColor));
int color = navigationBar.backgroundColor.get(defaultColor);
activity.getWindow().setNavigationBarColor(color);
setNavigationBarButtonsColor(color);
}
}

private void setNavigationBarButtonsColor(int color) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
View decorView = activity.getWindow().getDecorView();
int flags = decorView.getSystemUiVisibility();
if (isColorLight(color)) {
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;
}
}

0 comments on commit 6521177

Please sign in to comment.