CN

Android Tips: How to Prevent EditText from Automatically Getting Focus

Word count: 363Reading time: 2 min
2014/06/03
loading

Reviewed 2026-10-13: This technique applies to traditional Android View layouts. The focus attributes have been available since API 1 and remain supported. Input focus and keyboard visibility are separate states, so adding the attributes does not guarantee that the keyboard stays hidden in every situation.

In Android development, using EditText is very common. However, sometimes EditText automatically grabs focus when entering a page, causing the soft keyboard to pop up immediately. While this is convenient in some cases, most of the time we prefer the keyboard to appear only when the user explicitly clicks on the EditText.

Set these attributes on the parent layout to make it eligible for input focus in touch mode and give it priority during focus search:

1
2
3
android:focusable="true"
android:focusableInTouchMode="true"
android:descendantFocusability="beforeDescendants"

These are attribute fragments for the parent layout’s opening tag. If the container takes initial focus, EditText is no longer the default input target; the user can still tap EditText to edit. See ViewGroup’s focus rules. An explicit <requestFocus />, a code call to requestFocus(), or restored focus can change the result, so inspect those sources as well.

If the requirement is only to hide the keyboard when entering an Activity while allowing EditText to retain focus, set this attribute on the existing Activity declaration:

1
android:windowSoftInputMode="stateHidden"

stateHidden controls the initial keyboard state when the user navigates forward to the Activity. It does not prevent a later explicit request to show the keyboard, and returning to an existing screen can behave differently. See windowSoftInputMode. Choose focus handling and keyboard visibility according to the actual requirement.

About Me && Blog

Below is my personal intro and related links. I look forward to exchanging ideas with fellow professionals. “When three walk together, one can always be my teacher!”

  1. Blogger Intro
  2. Blog Content Navigation: A guide for my blog content.
  3. Curated Excellent Blog Articles - Android Performance Optimization Must-Knows
  4. Android Performance Optimization Knowledge Planet: The paid community stopped operating in 2026 and no longer accepts new members; this link preserves its history.

One walks faster alone, but a group walks further together.

Scan WeChat QR Code

CATALOG
  1. 1. About Me && Blog