Last active
July 18, 2025 05:26
-
-
Save vishalkale/3b0ce82708337631573e9aba6b7d6a68 to your computer and use it in GitHub Desktop.
Edge to edge helper
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 android.app.Activity | |
import android.graphics.Color | |
import android.view.View | |
import android.view.Window | |
import androidx.annotation.ColorInt | |
import androidx.core.view.WindowCompat | |
import androidx.core.view.WindowInsetsCompat | |
import androidx.core.view.updatePadding | |
import androidx.core.view.ViewCompat | |
/** | |
* Utility to configure a [Window] for edge-to-edge rendering while preserving | |
* proper handling of system insets (status & navigation bars). | |
* | |
* Call [enableEdgeToEdge] from every Activity (ideally early in `onCreate`). | |
* For Compose screens use the provided [consumeInsets] extension on the root view | |
* or wrap content with `WindowInsets` modifiers. | |
*/ | |
object WindowInsetsHelper { | |
/** | |
* Sets the window to draw edge-to-edge and applies transparent system bar | |
* colours. Replace colours if you need a custom overlay shade. | |
*/ | |
@JvmStatic | |
@JvmOverloads | |
fun enableEdgeToEdge( | |
window: Window, | |
@ColorInt statusBarColor: Int = Color.TRANSPARENT, | |
@ColorInt navigationBarColor: Int = Color.TRANSPARENT, | |
lightBars: Boolean = false, | |
) { | |
// Let app content draw under system bars. | |
WindowCompat.setDecorFitsSystemWindows(window, false) | |
window.statusBarColor = statusBarColor | |
window.navigationBarColor = navigationBarColor | |
// Adjust icon colours if requested. | |
WindowCompat.getInsetsController(window, window.decorView)?.let { controller -> | |
controller.isAppearanceLightStatusBars = lightBars | |
controller.isAppearanceLightNavigationBars = lightBars | |
} | |
} | |
/** | |
* Convenience extension for Activities. | |
*/ | |
@JvmStatic | |
@JvmOverloads | |
fun Activity.enableEdgeToEdge( | |
@ColorInt statusBarColor: Int = Color.TRANSPARENT, | |
@ColorInt navigationBarColor: Int = Color.TRANSPARENT, | |
lightBars: Boolean = false, | |
) { | |
enableEdgeToEdge(window, statusBarColor, navigationBarColor, lightBars) | |
} | |
/** | |
* Applies the current WindowInsets as padding on the [View] so that legacy | |
* XML layouts can avoid clipping under system bars after edge-to-edge is | |
* enabled. Use this on the root view of activities not yet migrated to | |
* Compose. For Compose use PaddingValues from `WindowInsets` instead. | |
*/ | |
@JvmStatic | |
fun View.consumeInsets() { | |
ViewCompat.setOnApplyWindowInsetsListener(this) { v, insets -> | |
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()) | |
v.updatePadding(top = systemBars.top, bottom = systemBars.bottom) | |
WindowInsetsCompat.CONSUMED | |
} | |
} | |
/** | |
* Java-friendly wrapper around [consumeInsets] to apply padding for | |
* system bars on arbitrary [View] roots (e.g., XML dialogs). | |
*/ | |
@JvmStatic | |
fun applyInsetsPadding(view: View) { | |
view.consumeInsets() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment