Prevent the keyboard from displaying on activity start
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
On Android, the soft keyboard appears automatically when an input view has focus as an activity starts. This can hurt first-screen experience on dashboards, detail pages, and read-only flows. Preventing it reliably usually requires manifest configuration plus explicit focus policy. It is best handled as a screen-design decision, not a one-off workaround.
Configure windowSoftInputMode
The first control point is activity configuration in AndroidManifest.xml.
stateHidden requests that keyboard remains hidden on launch. adjustResize helps layout behave correctly once keyboard appears later.
If theme also sets soft input mode, confirm activity and theme values are not conflicting.
Avoid Initial Focus on EditText
Even with stateHidden, focused input fields can still trigger keyboard on some devices. Set root view as focusable and remove default focus from inputs.
Then request focus on the root in code during startup.
Programmatic Hide as Backup
When navigation or lifecycle timing still causes keyboard display, hide it in onCreate or onResume as a fallback.
Use this sparingly. Overuse can fight normal UX when users expect immediate input.
Fragment Navigation Considerations
If your app uses fragments, keyboard behavior may differ by destination. Keep focus policy at fragment level where input actually lives.
For example:
- Read-only fragment: clear focus in
onViewCreated. - Edit fragment: request focus and show keyboard intentionally.
This avoids one global activity rule that breaks specific screens.
UX Rules for When Not to Hide
Do not force keyboard hidden on screens where typing is the first action, such as:
- Search-first pages.
- Login pages where username field is the main entry point.
- Chat composer screens.
In those contexts, automatic keyboard display improves usability. The objective is intentional behavior, not always hidden behavior.
Testing Across Devices
Keyboard behavior varies by Android version and OEM keyboard implementation. Test:
- Cold app launch.
- Returning via back stack.
- Rotation changes.
- Split-screen mode.
- Hardware keyboard connected.
A minimal debug check:
This helps diagnose unexpected show and hide events.
Jetpack Compose Note
If your screen uses Compose, keyboard visibility is still driven by focus state. Avoid requesting focus automatically in first composition unless text entry is the primary task.
Compose and View-based screens can coexist in one app, so keep keyboard policy consistent across both stacks to avoid confusing user transitions.
Common Pitfalls
- Setting
windowSoftInputModebut leavingEditTextfocused at startup. - Combining theme and activity soft-input flags with conflicting values.
- Hiding keyboard globally even on screens that need immediate typing.
- Applying fix only at activity level while fragment focus logic overrides it.
- Testing on one device and assuming behavior is identical everywhere.
Summary
- Use
windowSoftInputModeas the primary keyboard-start behavior control. - Remove initial focus from input fields when keyboard should stay hidden.
- Apply programmatic hide only as fallback for edge timing cases.
- Keep focus strategy aligned with per-screen UX intent.
- Validate behavior across device types and navigation flows.
Related reading
- Print Entry, CFBundleIdentifier, Does Not Exist
- Print the size megabytes of Data in Swift
- print without newline in swift
- Printing a variable memory address in swift
- Priority Queue in swift
- Processing Symbol Files in Xcode
- Profile doesn't match the entitlements file's value for the application-identifier entitlement
- Programmatically Add CenterX/CenterY Constraints
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.