Skip to content

Instantly share code, notes, and snippets.

@vodamiro
Last active August 30, 2018 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vodamiro/b26d770bba7c62dd48ea68f8a537356b to your computer and use it in GitHub Desktop.
Save vodamiro/b26d770bba7c62dd48ea68f8a537356b to your computer and use it in GitHub Desktop.
Extension for `AppCompatActivity` to setup fullscreen window mode with specific status and navigation bar
package com.app.ext
import android.graphics.Color
import android.os.Build
import android.support.v7.app.AppCompatActivity
import android.view.View
/***
*
* Extension for `AppCompatActivity` to setup fullscreen window mode with specific status and
* navigation bar.
*
* Example usage:
*
* ```
* override fun onResume() {
* super.onResume()
* setupFullscreenWindow(TransparentStatusBarOnDarkBg, TransparentNavigationBarOnDarkBg)
* }
* ```
*
* Note: Don't forget to add `android:fitsSystemWindows="true"` to root viewgroup in XML layout of the activity.
*
*/
open class StatusBar(val color: Int, val darkBackground: Boolean)
object TransparentStatusBarOnDarkBg : StatusBar(Color.TRANSPARENT, true)
object TransparentStatusBarOnLightBg : StatusBar(Color.TRANSPARENT, false)
open class NavigationBar(val placeAboveWindow: Boolean, val color: Int, val darkBackground: Boolean)
object TransparentNavigationBarOnDarkBg : NavigationBar(true, Color.TRANSPARENT, true)
object TransparentNavigationBarOnLightBg : NavigationBar(true, Color.TRANSPARENT, false)
fun AppCompatActivity.setupFullscreenWindow(statusBar: StatusBar, navigationBar: NavigationBar) {
window?.decorView?.let {
it.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE // Setup fullscreen window
setupStatusBar(statusBar)
setupNavigationBar(navigationBar)
}
}
fun AppCompatActivity.setupStatusBar(statusBar: StatusBar) {
window?.decorView?.let {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// Setup background color (or black below Marshmallow when background color is not dark enough - because light status bar is supported on Marshmallow and above)
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M && !statusBar.darkBackground) { // On Lollipop show black status bar when the background is light
window?.statusBarColor = Color.BLACK
} else {
window?.statusBarColor = statusBar.color
}
// Setup dark icons color on light background (only Marshmallow and above)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !statusBar.darkBackground) {
it.systemUiVisibility = it.systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
}
}
}
}
fun AppCompatActivity.setupNavigationBar(navigationBar: NavigationBar) {
window?.decorView?.let {
// Setup navigation bar - placement
if (navigationBar.placeAboveWindow) {
it.systemUiVisibility = it.systemUiVisibility or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
}
// Setup navigation bar - colors
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// Setup background color (or black below Oreo MR1 when background color is not dark enough - because light navigation bar is supported on Oreo and above)
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O_MR1 && !navigationBar.darkBackground) { // On Lollipop and Marshmallow show black navigation bar when the background is light
window?.navigationBarColor = Color.BLACK
} else {
window?.navigationBarColor = navigationBar.color
}
// Setup dark icons color on light background (only Oreo MR1 and above)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1 && !navigationBar.darkBackground) {
it.systemUiVisibility = it.systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment