Android Force EditText to remove focus?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
To make an EditText lose focus in Android, calling clearFocus() is usually necessary but often not sufficient. Focus rarely disappears into nothing. It usually moves to another focusable view, and if you also want the keyboard to go away, you need to hide it separately. That is why the practical fix is usually: clear focus, move focus elsewhere, and dismiss the keyboard.
The Basic Approach
The simplest code looks like this:
This removes focus from the EditText itself, but in many layouts Android will immediately move focus back to it or to another view depending on the focus rules of the screen.
So in real apps you usually want a dedicated target for the new focus.
Move Focus to Another View
A common pattern is to make the root layout focusable and request focus on it:
Then in code:
This gives Android somewhere valid to move focus, which makes the result more stable.
Hide the Keyboard Too
If your real goal is “stop editing and close the keyboard,” add input-method handling as well:
Without the keyboard call, the EditText may lose focus while the soft keyboard still remains visible for a moment or until the next interaction.
A Complete Helper Function
A small helper keeps this behavior reusable:
Usage:
This is the sort of helper that belongs in form-heavy activities or fragments.
When the EditText Regains Focus Immediately
If the field keeps taking focus back, there is usually another cause in the layout or lifecycle:
- the
EditTextis the only focusable view - code later calls
requestFocus()on it again - the layout requests initial focus during screen startup
- a dialog or fragment transaction changes focus behavior
A temporary workaround some developers try is:
But that is only correct if the field really should stop being editable for a while. It is not a general-purpose focus-clearing solution.
A Common XML Trick for Initial Focus
If the problem is that the EditText grabs focus automatically when the screen opens, add a dummy or parent focus target before it:
This often prevents automatic initial focus without disabling the EditText completely.
Java Version
The same idea in Java:
The logic is unchanged: hide keyboard, clear focus, move focus elsewhere.
Common Pitfalls
The biggest pitfall is calling clearFocus() and expecting focus to vanish without another view taking it. Android focus usually transfers rather than disappearing.
Another common mistake is forgetting the keyboard. Removing focus and hiding the keyboard are related actions, but they are not the same thing.
People also often set focusable="false" on the EditText when they really only wanted to dismiss it temporarily. That can break later editing behavior.
Finally, if focus returns immediately, inspect the rest of the layout and lifecycle code. Something else is probably requesting focus again.
Summary
- '
editText.clearFocus()is the starting point, not always the full solution.' - Move focus to another valid view, often the root layout.
- Hide the soft keyboard separately if that is part of the desired UX.
- Use XML root focus settings to prevent unwanted initial focus on screen load.
- If the field keeps regaining focus, look for another explicit or implicit focus request in your code or layout.

