EditText
focus
programmatically
Android development
keyboard display

How can I set the focus and display the keyboard on my EditText programmatically

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Showing the soft keyboard for an EditText programmatically is a two-part problem: the view must gain focus, and the input method must be asked to show itself at a moment when the view is actually attached to the window. Most failures happen because one of those two conditions is missing.

Focus Comes First

The keyboard is tied to the currently focused input view. If the EditText does not own focus, showSoftInput often does nothing.

A minimal Kotlin example looks like this.

kotlin
1val editText = findViewById<EditText>(R.id.myEditText)
2editText.requestFocus()
3
4val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
5imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT)

The same idea in Java is almost identical.

java
1EditText editText = findViewById(R.id.myEditText);
2editText.requestFocus();
3
4InputMethodManager imm =
5    (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
6imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);

Timing Is Usually the Real Bug

Calling the code too early is the most common reason it fails. In onCreate, after a fragment transaction, or immediately after inflating a layout, the view may not yet have a window token.

A safe pattern is to defer the keyboard request until the view is laid out.

kotlin
1editText.post {
2    editText.requestFocus()
3    val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
4    imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT)
5}

Using post works well because it schedules the action to run after the current layout work completes.

Manifest and Window Flags Matter Too

If the activity or fragment is intended to open directly into text entry, windowSoftInputMode can help the system cooperate.

xml
<activity
    android:name=".SearchActivity"
    android:windowSoftInputMode="stateVisible|adjustResize" />

This does not replace requestFocus, but it can make the startup behavior more consistent, especially for search or login screens.

A Modern Alternative on Newer APIs

Newer Android UI code can also work through insets APIs.

kotlin
editText.requestFocus()
WindowCompat.getInsetsController(window, editText)
    .show(WindowInsetsCompat.Type.ime())

This is especially useful when your app already uses window-insets handling and wants keyboard visibility to participate in that same model.

Hiding the Keyboard Cleanly

The inverse operation is straightforward once you understand that the keyboard is attached to a window token.

kotlin
1fun hideKeyboard(activity: Activity) {
2    val view = activity.currentFocus ?: View(activity)
3    val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
4    imm.hideSoftInputFromWindow(view.windowToken, 0)
5}

When hiding the keyboard, it is often also useful to clear focus so it does not immediately reappear.

kotlin
editText.clearFocus()
hideKeyboard(this)

Fragments Need the Same Rule

In fragments, developers often run keyboard code in onViewCreated and assume the view is ready. Sometimes it is, sometimes it is not.

The safest fragment pattern is still to call post on the target view.

kotlin
1override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
2    val editText = view.findViewById<EditText>(R.id.myEditText)
3    editText.post {
4        editText.requestFocus()
5        val imm = requireContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
6        imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT)
7    }
8}

That avoids racing the fragment lifecycle.

Common Pitfalls

The biggest mistake is calling showSoftInput before the EditText is attached and focusable in the current window. Use post when in doubt.

Another mistake is forgetting requestFocus. Without focus, the keyboard manager often ignores the request.

A third issue is using SHOW_FORCED instead of SHOW_IMPLICIT. Forced display can produce awkward behavior and keep the keyboard visible longer than intended.

Finally, manifest flags and runtime code can work against each other. For example, stateHidden in the manifest and explicit keyboard-show logic in code create a conflict that looks random.

Summary

  • Request focus on the EditText before trying to show the keyboard.
  • Delay the keyboard request until the view is attached, usually with post.
  • Use InputMethodManager.showSoftInput for the classic approach.
  • Use insets APIs when your app already follows modern window-insets patterns.
  • Clear focus and hide via the window token when dismissing the keyboard.
  • Most failures are timing problems, not API problems.

Course illustration
Course illustration

All Rights Reserved.