How to detect when keyboard is shown and hidden
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Detecting keyboard visibility is really about responding to layout changes when text input starts or ends. The exact API depends on platform, but the goal is the same everywhere: know when the keyboard is visible, measure how much space it consumes, and move or pad your UI so the active field stays usable.
Detect Keyboard Changes on iOS
On iOS, UIKit posts notifications when the keyboard is about to appear, change frame, or hide. The common pattern is to observe keyboardWillShowNotification and keyboardWillHideNotification, read the keyboard frame from the notification payload, and update constraints or content insets.
If the keyboard can resize while visible, keyboardWillChangeFrameNotification is often a better signal than only listening for show and hide.
Detect Keyboard Changes on Android
On Android, the modern approach is to read input method editor insets instead of guessing from view height changes. Using WindowInsetsCompat gives a direct answer to whether the software keyboard is visible and how much bottom space it occupies.
This is more reliable than older global-layout heuristics because it asks the system directly for the input-method insets instead of trying to infer them indirectly.
React to Space, Not Just Visibility
In real applications, the useful value is usually the keyboard height or bottom inset, not just a true-or-false flag. Devices, orientations, floating keyboards, and split-screen modes can all change how much space the keyboard actually covers.
That is why robust keyboard handling adjusts:
- bottom constraints on fixed forms
- content insets on scroll views
- the focused control position during field navigation
Treat keyboard detection as a layout input, not just an event to log.
Animation and User Experience
A layout jump is often worse than a partially covered field. On iOS, you can read animation timing from the notification and match it. On Android, inset-aware layouts already make the transition more natural because the system drives much of the movement.
The implementation detail differs across platforms, but the design rule is consistent: move only what needs to move, and keep the focused field visible.
Common Pitfalls
- Listening only for show and hide while ignoring keyboard frame changes.
- Guessing keyboard height from screen size instead of using platform APIs.
- Updating layout without considering scroll views, safe areas, or insets.
- Forgetting to remove observers in longer-lived iOS objects.
- Relying on old Android global-layout hacks when insets are available.
Summary
- On iOS, observe keyboard notifications and read the ending frame from the payload.
- On Android, prefer input-method insets over manual height calculations.
- Use the keyboard height or bottom inset to drive layout changes.
- Keep the active input visible instead of shifting the whole screen unnecessarily.
- Good keyboard handling is mostly about layout correctness and smooth transitions.

