Skip to content

Commit

Permalink
Fix navigation bar color on older android versions
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecorry31 committed Feb 11, 2024
1 parent c60a8c4 commit 8e2dfd7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import com.kylecorry.trail_sense.tools.clinometer.volumeactions.ClinometerLockVo
import com.kylecorry.trail_sense.tools.flashlight.volumeactions.FlashlightToggleVolumeAction
import com.kylecorry.trail_sense.shared.VolumeAction
import com.kylecorry.trail_sense.shared.colors.ColorUtils
import com.kylecorry.trail_sense.shared.setNavigationBarColorCompat


class MainActivity : AndromedaActivity() {
Expand Down Expand Up @@ -140,15 +141,13 @@ class MainActivity : AndromedaActivity() {
window.apply {
statusBarColor = Color.BLACK
decorView.rootView.setBackgroundColor(Color.BLACK)
navigationBarColor = Color.BLACK
setNavigationBarColorCompat(Color.BLACK)
}
bottomNavigation.setBackgroundColor(Color.BLACK)
} else {
window.apply {
statusBarColor = ColorUtils.backgroundColor(this@MainActivity)

// Match the color of the bottom navigation
navigationBarColor = Resources.androidBackgroundColorSecondary(this@MainActivity)
setNavigationBarColorCompat(Resources.androidBackgroundColorSecondary(this@MainActivity))
}
if (!isDarkThemeOn()) {
WindowInsetsControllerCompat(window, window.decorView).apply {
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/com/kylecorry/trail_sense/shared/Extensions.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.kylecorry.trail_sense.shared

import android.os.Bundle
import android.view.Window
import androidx.annotation.ColorInt
import androidx.annotation.IdRes
import androidx.fragment.app.Fragment
import androidx.navigation.NavController
Expand Down Expand Up @@ -137,4 +139,13 @@ inline fun List<Float>.forEachLine(action: (x1: Float, y1: Float, x2: Float, y2:
for (i in indices step 4) {
action(this[i], this[i + 1], this[i + 2], this[i + 3])
}
}

/**
* Sets the navigation bar color. On SDK < 26, this does nothing because the foreground color is not customizable.
*/
fun Window.setNavigationBarColorCompat(@ColorInt color: Int) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
navigationBarColor = color
}
}

0 comments on commit 8e2dfd7

Please sign in to comment.