Last active
April 22, 2021 18:12
-
-
Save nwellis/66ca9ce309840a438d80018805f6421d to your computer and use it in GitHub Desktop.
Extension functions using window insets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import androidx.annotation.ColorRes | |
import androidx.appcompat.app.AppCompatActivity | |
import androidx.core.view.WindowCompat | |
import androidx.core.view.WindowInsetsCompat | |
import androidx.core.view.WindowInsetsControllerCompat | |
import com.sample.app.R | |
private val statusAndNavBarType = | |
WindowInsetsCompat.Type.statusBars() or WindowInsetsCompat.Type.navigationBars() | |
fun AppCompatActivity.setWindowMode(mode: WindowMode) { | |
val containerView = window.decorView | |
// Note: If the activity XML has android:fitsSystemWindows="true" then this will not work! | |
WindowCompat.setDecorFitsSystemWindows(window, mode.showStatusBar && mode.showNavBar) | |
WindowInsetsControllerCompat(window, containerView).apply { | |
WindowInsetsCompat.Type.statusBars().let { if (mode.showStatusBar) show(it) else hide(it) } | |
WindowInsetsCompat.Type.navigationBars().let { if (mode.showNavBar) show(it) else hide(it) } | |
isAppearanceLightStatusBars = mode.isLightStatusBar | |
isAppearanceLightNavigationBars = mode.isLightNavBar | |
} | |
window.statusBarColor = resources.getColor(mode.statusBarColor, theme) | |
window.navigationBarColor = resources.getColor(mode.navBarColor, theme) | |
} | |
/** | |
* Set the window and inset properties of your view. When using a light status/navigation bar | |
* color, be sure to set the appropriate flags to reflect this, [isLightStatusBar] and | |
* [isLightNavBar]. | |
* | |
* The following will ignore the fullscreen configuration and cause unwanted inset padding for | |
* a status and nav bar that doesn't take up space. | |
* ```xml | |
* <androidx.coordinatorlayout.widget.CoordinatorLayout | |
* xmlns:android="http://schemas.android.com/apk/res/android" | |
* android:layout_width="match_parent" | |
* android:layout_height="match_parent" | |
* android:fitsSystemWindows="true" | |
* ``` | |
* | |
* Note that [isLightNavBar] is always false for APIs less than 26. This can be set, but will have | |
* no affect for versions prior to 26. | |
*/ | |
data class WindowMode( | |
val showStatusBar: Boolean, | |
val isLightStatusBar: Boolean, | |
@ColorRes val statusBarColor: Int, | |
val showNavBar: Boolean, | |
val isLightNavBar: Boolean, | |
@ColorRes val navBarColor: Int, | |
) { | |
companion object { | |
val Light = WindowMode( | |
showStatusBar = true, | |
isLightStatusBar = true, | |
statusBarColor = R.color.white, | |
showNavBar = true, | |
isLightNavBar = true, | |
navBarColor = R.color.white, | |
) | |
val Dark = WindowMode( | |
showStatusBar = true, | |
isLightStatusBar = false, | |
statusBarColor = R.color.colorPrimary, | |
showNavBar = true, | |
isLightNavBar = false, | |
navBarColor = R.color.black, | |
) | |
val Fullscreen = Dark.copy( | |
showStatusBar = false, | |
showNavBar = false, | |
) | |
val Default = Light | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment