I would like to be able to remove the focus from the EditText. For example if the Keyboard appears, and the user hides it with the back button, I would like the focus and the cursor to disappear. How can it be done?
-
Is there any other view that can be focused after EditText? – Heidar Feb 21 '11 at 02:06
-
Is it possible to focus a WebView? – Alex1987 Feb 21 '11 at 02:06
-
See my answer here: http://stackoverflow.com/questions/3890033/how-to-remove-focus-from-single-edittext/12530226#12530226 – Sveinung Kval Bakken Sep 21 '12 at 12:02
-
5check dis out.. http://stackoverflow.com/questions/1555109/stop-edittext-from-gaining-focus-at-activity-startup – Amritpal Singh Feb 26 '13 at 12:22
-
one line code https://stackoverflow.com/a/46580242/6611895 – pruthwiraj.kadam Oct 05 '17 at 07:33
-
for removing the cursor from edit text editText.setCursorVisible(false); – Jayakrishnan Oct 14 '17 at 12:28
18 Answers
You can make cursor and focus disappear by
edittext.clearFocus();
But detect when the key board hide is a hard work.

- 5,870
- 1
- 21
- 23
-
66
-
209It works, but it sets the focus back to the first focusable view in the activity, so if you have only one view in the activity you may think it doesn't, but it does work, you have to understand its behavior :) – Ashraf Bashir Jan 06 '13 at 14:20
-
How do I go about returning the focus to the EditText? I've tried using .requestFocus(); but this doesn't seem to work. Thanks in advance. – edwoollard Nov 06 '13 at 18:35
-
I have a few apps, in those where I'm using a fragment, clearFocus() works, in those where I just have 1 activity it doesn't work, the focus just jumps to the first editText. – Leon Sep 17 '15 at 15:48
-
125If anyone else is struggling to get this to work, if you add `android:focusable="true"` and `android:focusableInTouchMode="true"` to the root `Layout` of your activity or fragment, this changes the focus to the `Layout` instead of the `EditText`. – Loyalar Apr 20 '16 at 07:06
-
You can add this to onCreate
and it will hide the keyboard every time the Activity
starts.
You can also programmatically change the focus to another item.
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
-
-
-
@Mr.Noob If you have a text field inside your DialogFragment, exactly the same. – Max Aug 21 '15 at 12:19
-
3To guys like me who first skipped the comments under the question itself : @AmritpalSingh has a more elegant way (using only XML attributes) to achieve this. Check it out ! – pr-shadoko Apr 12 '16 at 09:45
-
1to re-gain the focus, use `this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);` and `editText.requestFocus();` – Prabs Aug 24 '16 at 06:56
-
-
beware, this requires an a window container, doesn't work inside a custom view – desgraci Feb 23 '18 at 20:48
Add LinearLayout
before EditText
in your XML.
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true"
android:layout_width="0px"
android:layout_height="0px" />
Or you can do this same thing by adding these lines to view before your 'EditText'.
<Button
android:id="@+id/btnSearch"
android:layout_width="50dp"
android:layout_height="50dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center"
android:text="Quick Search"
android:textColor="#fff"
android:textSize="13sp"
android:textStyle="bold" />
<EditText
android:id="@+id/edtSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:gravity="left"
android:hint="Name"
android:maxLines="1"
android:singleLine="true"
android:textColorHint="@color/blue"
android:textSize="13sp"
android:textStyle="bold" />

- 11
- 4

- 2,454
- 1
- 16
- 13
-
16I liked this solution used in combination with a .clearFocus() on my EditText (which was the first focusable item in the layout, so clearFocus() didn't work on its own). Spoofing the LinearLayout to be focusable is a simple and easy solution. Nice work Singh Arjun :) – BasicPleasureModel May 22 '13 at 09:42
-
1You'll only need to use `android:focusableInTouchMode="true"` because setting this to true will also ensure that this view is focusable. – GuilhE Aug 03 '16 at 10:30
-
-
I don't know why but for this work I should use android:layout_width="1dp" android:layout_height="1dp" – Javad Jan 30 '21 at 06:04
Remove focus but remain focusable:
editText.setFocusableInTouchMode(false);
editText.setFocusable(false);
editText.setFocusableInTouchMode(true);
editText.setFocusable(true);
EditText will lose focus, but can gain it again on a new touch event.
-
11This seems to shift the focus to the next focusable view, rather than removing the focus from the edit text. It seems like Android doesn't ever want to be "unfocused"; I opted to make the root layout focusable / focusableInTouchMode in my project for this reason. It makes semantic sense as well: when you don't want to be focusing on the particular edit text, setting the focus to the root layout is akin to taking a step back and focusing on the full activity / fragment instead of the particular view. – Shane Creighton-Young Oct 23 '14 at 18:36
-
1
-
1
-
1
-
2
-
If you do this inside the EditText's onFocusChangeListener, on Android 6.0 (Marshmallow) it will yield a stack overflow (crash). – straya Feb 20 '20 at 23:54
Add these two properties to your parent layout (ex: Linear Layout, Relative Layout)
android:focusable="true"
android:focusableInTouchMode="true"
It will do the trick :)

- 18,405
- 4
- 60
- 77
-
11
-
2
-
4Dont wonder. Android sometimes does wonders that it's own creators dont understand. – JaydeepW Nov 10 '16 at 14:13
-
2
-
3@UtsavGupta the underlying (and unpredictable) logic/behavior is that when the focus is lost from a view, android will set it to the “first” view that supports it, in the view hierarchy. – Martin Marconcini May 18 '18 at 18:50
-
1
-
1
-
1So simple, so good. Keyboard is hidden and all the views work as expected when clicking into them. Thank you. – Luigi_Papardelle Jan 03 '22 at 12:56
remove autofocus edittext android
It's working for me
Edit In the link they suggest to use LinearLayout, but simple View will work
<View
android:id="@+id/focus_thief"
android:layout_width="1dp"
android:layout_height="1dp"
android:focusable="true"
android:focusableInTouchMode="true" />
Then if this "thief" is placed at the top of the layout (to be first focusable item) calls to clearFocus()
will work.

- 6,078
- 7
- 43
- 79

- 1,204
- 9
- 11
-
11While this may theoretically answer the question, [it would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Bill the Lizard Mar 30 '12 at 13:24
-
1It is not required to add a separate "focus_thief" view if you have any other view in your layout that will take the android:focusable="true" & android:focusableInTouchMode="true"parameters. Just add those to the view above (if you have one) instead. – Bamerza Oct 02 '18 at 00:58
To hide the keyboard when activity starts.. write the following code in onCreate()..
InputMethodManager imm = (InputMethodManager)
getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
To clear focus and remove cursor from edittext.....
editText.clearFocus();
editText.setCursorVisible(false);

- 1,402
- 6
- 25
- 31

- 1,819
- 17
- 17
-
2Note: for ```editText.clearFocus();``` to work you need to add ```android:focusableInTouchMode="true" android:focusable="true"``` to the root layout – DIRTY DAVE Nov 18 '21 at 20:24
You can also include android:windowSoftInputMode="stateAlwaysHidden" in your manifest action section.
This is equivalent to :
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
but in XML way.
FYI, you can also hide the keyboard with codes:
// hide virtual keyboard
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mYourEditText.getWindowToken(), 0);

- 586
- 8
- 24

- 5,697
- 2
- 24
- 16
-
-
1And when you'll ineveitablly want to hide it, you'll have to do this http://stackoverflow.com/a/14327901/1166266 – Allison Nov 28 '16 at 22:34
try to use this one on your view it worked for me:
<View
android:id="@+id/fucused"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"/>

- 10,769
- 13
- 68
- 113

- 1,135
- 1
- 12
- 19
-
5I can't believe how terrible is the focus management in Android, after trying different possible solutions until the point I was frustrated, this comment was the only option that works flawlessly... just adding a simple View on the xml does the trick (obviously you set the focus to that view when you want to remove from other) Thank you.. – Pedro Varela Jan 22 '16 at 21:59
-
4
-
6
-
None of the solutions worked for me except this one. My SearchView was within a fragment and first view at the top of the view group hence it was taken focus no wonder what I try. – Ishaan Jun 20 '20 at 14:39
Add to your parent layout where did you put your EditText
this android:focusableInTouchMode="true"

- 1,873
- 1
- 20
- 24
-
3This trick works for me. In case the view is the first `EditText` it seems `clearFocus()` clears its focus but then starts searching for a focus candidate through the view tree and then finds out the same view. And the result is the same. But if you put `android:focusableInTouchMode` the focus traversal ends up on its parent. – WindRider Jun 15 '16 at 11:11
-
-
you have to remove <requestFocus/>
if you don't use it and still the same problem
user LinearLayout
as a parent and set
android:focusable="true"
android:focusableInTouchMode="true"
Hope it's help you.

- 1,084
- 13
- 14
This is my very first answer on SO, so don't be too harsh on me if there are mistakes. :D
There are few answers floating around the SO, but I feel the urge to post my complete solution cause this drove me nuts. I've grabbed bits and pieces from all around so forgive me if I don't give respective credits to everyone... :)
(I'll simplify my result cause my view has too many elements and I don't wanna spam with that and will try to make it as generic as possible...)
For your layout you need a parent your EditText and parent view defined something like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lytContainer"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true">
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/etEditor"
android:inputType="number"
android:layout_gravity="center"
android:hint="@string/enter_your_text"
android:textColor="@android:color/darker_gray"
android:textSize="12dp"
android:textAlignment="center"
android:gravity="center"
android:clickable="true"/>
</LinearLayout>
So, I needed a few things here. I needed to have a Placeholder for my EditText - which is that -
android:hint="hint"
Also,
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
made it happen for EditText not to be focused on entering the Activity and later on in the Activity itself when setting it this setting helps so you can set onTouchListener on it to steal the focus away from EditText.
Now, in the Activity:
package com.at.keyboardhide;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
public class MainActivity extends Activity implements OnTouchListener{
private EditText getEditText;
private LinearLayout getLinearLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
setContentView(R.layout.keyboardmain);
getEditText = (EditText)findViewById(R.id.etEditor);
getLinearLayout = (LinearLayout)findViewById(R.id.lytContainer);
getLinearLayout.setOnTouchListener(this);
getEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
Log.d("EDTA", "text was entered.");
getEditText.clearFocus();
imm.hideSoftInputFromWindow(barcodeNo.getWindowToken(), 0);
return true;
}
return false;
}
});
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if(v==getLinearLayout){
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getEditText.getWindowToken(), 0);
getEditText.clearFocus();
return true;
}
return false;
}
}
Few of the answers for bits I found on this question page, and the part with the Activity solution I found on this blog. The rest I missed which I had to figure out myself was clearing focus on the EditText which I added to both inside the setOnEditorActionListener and onTouchLister for the parent view.
Hope this helps someone and saves their time. :)
Cheers, Z.

- 183
- 5
- 12
-
android:descendantFocusability="beforeDescendants" android:focusableInTouchMode="true" Worked for me Thank you very much – sharma_kunal Nov 04 '16 at 20:42
-
In the comments you asked if another view can be focused instead of the EditText. Yes it can. Use .requestFocus()
method for the view you want to be focused at the beginning (in onCreate() method)
Also focusing other view will cut out some amount of code. (code for hiding the keyboard for example)

- 49,966
- 15
- 119
- 148
I had the same problem. It made me more than crazy.
I had an extended Dialog with a ScrollView that had a TableLayout with extended LinearLayout that contained a SeekBar and a EditText.
The first EditText had always autofocus after showing the Dialog and after finishing editing the text over the keyboard the EditText still had the focus and the keyboard was still visible.
I tried nearly all solutions of this thread and none worked for me.
So here my simple solution: (text = EditText)
text.setOnEditorActionListener( new OnEditorActionListener( ){
public boolean onEditorAction( TextView v, int actionId, KeyEvent event ){
if( (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) ||
(actionId == EditorInfo.IME_ACTION_DONE) ){
text.clearFocus( );
InputMethodManager iMgr = null;
iMgr = (InputMethodManager)mContext.getSystemService( Context.INPUT_METHOD_SERVICE );
iMgr.hideSoftInputFromWindow( text.getWindowToken(), 0 );
}
return true;
}
});
By the way I didn't used any of the following snippets to solve it:
//setFocusableInTouchMode( true )
//setFocusable( true )
//setDescendantFocusability( ViewGroup.FOCUS_BEFORE_DESCENDANTS )
AND I didn't used a spacer item like a View with width and height of 1dp.
Hopefully it helps someone :D

- 301
- 6
- 9
-
1what is this? ^^ :) I will try all other solutions on StackOverFlow before trying this code. – Mike76 Sep 14 '15 at 20:06
-
1This is a working solutions (for me) and I wanted to share it, because as I've written: "It made me more than crazy." Have fun by trying ALL other solutions, which are sometimes more complicated than mine by using wrappers or not needed functions and at the end they didn't worked for me. – Pixtar Sep 24 '15 at 04:33
-
1It's fine if it worked for you, I solved it with `android:focusable="true" android:focusableInTouchMode="true"` in the parent RelativeLayout. – Mike76 Sep 24 '15 at 07:06
-
1This answer totally worked for me, removing focus of editText AND closing keyboard. I do have `android:focusable="true" `and `android:focusableInTouchMode="true"` on parent layout though Haven't tried without them, just happy that it works after two days shouting and trying everything under the sun. Stupid Android editText. – Rodia Jul 30 '17 at 02:06
You can also include android:windowSoftInputMode="stateAlwaysHidden"
in your manifest action section.
This is equivalent to:
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
editText.setFocusableInTouchMode(true)
The EditText
will be able to get the focus when the user touch it.
When the main layout (activity, dialog, etc.) becomes visible the EditText
doesn't automatically get the focus even though it is the first view in the layout.

- 2,493
- 2
- 27
- 39

- 31
- 1
You can avoid any focus on your elements by setting the attribute android:descendantFocusability of the parent element.
Here is an example:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/search__scroller"
android:descendantFocusability="blocksDescendants"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ScrollView>
Here, the attribute android:descendantFocusability set to "blocksDescendants" is blocking the focus on the child elements.
You can find more info here.

- 1
- 1
-
1You should use `android:descendantFocusability="beforeDescendants" android:focusable="true" android:focusableInTouchMode="true"` on the ScrollView's child – Ionut Negru Jan 27 '14 at 12:04
-
check your xml file
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp" >
**<requestFocus />**
</EditText>
//Remove **<requestFocus />** from xml

- 17
- 1