Understanding Window, Insets, and WindowInsets in Android: Concepts, APIs, and Practical Usage
This article explains the fundamental concepts of Window, Insets, and WindowInsets in Android, describes the different inset types, demonstrates how to retrieve and apply them with modern AndroidX APIs, and provides practical Kotlin code examples for handling system UI elements such as status bars, navigation bars, and the soft keyboard.
1. What is a Window – In Android, a Window (implemented by PhoneWindow ) is the core UI component that hosts all Views; Activities, Dialogs, and Toasts rely on a Window to display their content, and developers interact with Views rather than the Window directly.
2. What are Insets – Insets represent the areas occupied by system UI (status bar, navigation bar, IME, etc.). They are not the same as a Rect ; they describe offsets rather than absolute coordinates. In Android they are represented by WindowInsets (framework) or WindowInsetsCompat (AndroidX).
3. What is WindowInsets – WindowInsets aggregates several inset groups such as stableInsets , systemWindowInsets , and windowDecorInsets . systemWindowInsets is the most commonly used, indicating the area covered by system bars. Example code to apply the bottom inset:
ViewCompat.setOnApplyWindowInsetsListener(view) { v, insets ->
val sysWindow = insets.systemWindowInsets
//val stable = insets.stableInsets
//val systemGestures = insets.systemGestureInsets
//val tappableElement = insets.tappableElementInsets
v.updatePadding(bottom = sysWindow.bottom)
}Newer APIs replace deprecated members with type‑based queries:
ViewCompat.setOnApplyWindowInsetsListener(view) { view, insets ->
val sysWindow = insets.getInsets(Type.systemBars())
val stable = insets.getInsetsIgnoringVisibility(Type.systemBars())
val systemGestures = insets.getInsets(Type.systemGestures())
val tappableElement = insets.getInsets(Type.tappableElement())
}The Type class provides constants such as ime() , statusBars() , navigationBars() , systemBars() , systemGestures() , mandatorySystemGestures() , tappableElement() , and displayCutout() . Notably, systemGestures() refers to the gesture navigation area introduced in Android 10, while mandatorySystemGestures() denotes the non‑overridable region (e.g., the bottom 60 dp swipe area).
4. Handling Inset Conflicts – To avoid UI overlap with system UI, developers add an OnApplyWindowInsetsListener to the target view and adjust padding or margin based on the retrieved insets. The listener may be invoked multiple times, so updates should be idempotent.
Keyboard (IME) handling example:
val insets = ViewCompat.getRootWindowInsets(view)
val imeVisible = insets.isVisible(WindowInsets.Type.ime())
val imeHeight = insets.getInsets(WindowInsets.Type.ime()).bottomShow/hide keyboard:
ViewCompat.getWindowInsetsController(view)!!.show(WindowInsetsCompat.Type.ime())
ViewCompat.getWindowInsetsController(view)!!.hide(WindowInsetsCompat.Type.ime())Controlling status and navigation bars:
val controller = ViewCompat.getWindowInsetsController(view)
controller?.hide(WindowInsetsCompat.Type.statusBars())
controller?.show(WindowInsetsCompat.Type.navigationBars())
controller?.isAppearanceLightStatusBars = false
controller?.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_BARS_BY_SWIPEWindowInsetsAnimation (Android 11) can animate UI together with the keyboard:
object : WindowInsetsAnimation.Callback(DISPATCH_MODE_STOP) {
override fun onPrepare(animation: WindowInsetsAnimation) { super.onPrepare(animation) }
override fun onProgress(insets: WindowInsets, runningAnimations: MutableList
): WindowInsets {
return insets
}
override fun onEnd(animation: WindowInsetsAnimation) { super.onEnd(animation) }
}5. Summary – WindowInsetsCompat and related APIs simplify access to system UI elements such as the status bar, navigation bar, and keyboard, making UI adaptation more straightforward and robust for modern Android applications.
Sohu Tech Products
A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.