AndroidでNavigation BarとStatus Barの色を変える

2021/03/04に公開

systemUiVisibilityがdeprecatedになったので、コードを見直した
少しはまったので、残しておく

MainActivity.kt
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        setStatusBarColor(android.R.color.black)
        setNavigationBarColor(android.R.color.black)
        window.setLightBackgroundSystemBar()
    }

    private fun AppCompatActivity.setStatusBarColor(colorRes: Int) {
        var colorResTemp = colorRes
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            colorResTemp = android.R.color.black
        }
        window.statusBarColor = ContextCompat.getColor(this, colorResTemp)
    }

    private fun AppCompatActivity.setNavigationBarColor(colorRes: Int) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
            return
        }
        window.navigationBarColor = ContextCompat.getColor(this, colorRes)
    }

    private fun Window.setLightBackgroundSystemBar() {
        when {
            Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1 -> {
                return
            }
            Build.VERSION.SDK_INT >= Build.VERSION_CODES.R -> {
                insetsController?.run {
                    WindowInsetsControllerCompat.toWindowInsetsControllerCompat(this).isAppearanceLightNavigationBars =
                        true
                    WindowInsetsControllerCompat.toWindowInsetsControllerCompat(this).isAppearanceLightStatusBars =
                        true
                }
            }
            else -> {
                @Suppress("DEPRECATION")
                @RequiresApi(Build.VERSION_CODES.M)
                decorView.systemUiVisibility = when {
                    Build.VERSION.SDK_INT >= Build.VERSION_CODES.O -> decorView.systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
                    else -> decorView.systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
                }
            }
        }
    }

    private fun Window.setLightBackgroundNavigationBar() {
        when {
            Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1 -> {
                return
            }
            Build.VERSION.SDK_INT >= Build.VERSION_CODES.R -> {
                insetsController?.run {
                    WindowInsetsControllerCompat.toWindowInsetsControllerCompat(this).isAppearanceLightNavigationBars =
                        true
                }
            }
            else -> {
                @Suppress("DEPRECATION")
                decorView.systemUiVisibility = when {
                    Build.VERSION.SDK_INT >= Build.VERSION_CODES.O -> decorView.systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
                    else -> decorView.systemUiVisibility
                }
            }
        }
    }

    private fun Window.setLightBackgroundStatusBar() {
        when {
            Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1 -> {
                return
            }
            Build.VERSION.SDK_INT >= Build.VERSION_CODES.R -> {
                insetsController?.run {
                    WindowInsetsControllerCompat.toWindowInsetsControllerCompat(this).isAppearanceLightStatusBars =
                        true
                }
            }
            else -> {
                @Suppress("DEPRECATION")
                decorView.systemUiVisibility = when {
                    Build.VERSION.SDK_INT >= Build.VERSION_CODES.O -> decorView.systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
                    else -> decorView.systemUiVisibility
                }
            }
        }
    }

    private fun Window.setDarkBackgroundSystemBar() {
        when {
            Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1 -> {
                return
            }
            Build.VERSION.SDK_INT >= Build.VERSION_CODES.R -> {
                insetsController?.run {
                    WindowInsetsControllerCompat.toWindowInsetsControllerCompat(this).isAppearanceLightNavigationBars =
                        false
                    WindowInsetsControllerCompat.toWindowInsetsControllerCompat(this).isAppearanceLightStatusBars =
                        false
                }
            }
            else -> {
                @Suppress("DEPRECATION")
                @RequiresApi(Build.VERSION_CODES.M)
                decorView.systemUiVisibility = when {
                    Build.VERSION.SDK_INT >= Build.VERSION_CODES.O -> decorView.systemUiVisibility and View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR.inv() and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv()
                    else -> decorView.systemUiVisibility and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv()
                }
            }
        }
    }

    private fun Window.setDarkBackgroundStatusBar() {
        when {
            Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1 -> {
                return
            }
            Build.VERSION.SDK_INT >= Build.VERSION_CODES.R -> {
                insetsController?.run {
                    WindowInsetsControllerCompat.toWindowInsetsControllerCompat(this).isAppearanceLightStatusBars =
                        false
                }
            }
            else -> {
                @Suppress("DEPRECATION")
                @RequiresApi(Build.VERSION_CODES.M)
                decorView.systemUiVisibility = when {
                    Build.VERSION.SDK_INT >= Build.VERSION_CODES.O -> decorView.systemUiVisibility and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv()
                    else -> decorView.systemUiVisibility
                }
            }
        }
    }

    private fun Window.setDarkBackgroundNavigationBar() {
        when {
            Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1 -> {
                return
            }
            Build.VERSION.SDK_INT >= Build.VERSION_CODES.R -> {
                insetsController?.run {
                    WindowInsetsControllerCompat.toWindowInsetsControllerCompat(this).isAppearanceLightNavigationBars =
                        false
                }
            }
            else -> {
                @Suppress("DEPRECATION")
                @RequiresApi(Build.VERSION_CODES.M)
                decorView.systemUiVisibility = when {
                    Build.VERSION.SDK_INT >= Build.VERSION_CODES.O -> decorView.systemUiVisibility and View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR.inv()
                    else -> decorView.systemUiVisibility
                }
            }
        }
    }

Discussion