By Mark

2009-10-12 15:03:22 8 Comments

I have an Activity in Android, with two elements:

  1. EditText
  2. ListView

When my Activity starts, the EditText immediately has input focus (flashing cursor). I don't want any control to have input focus at startup. I tried:


No luck. How can I convince the EditText to not select itself when the Activity starts?


@Morgan Christiansson 2009-11-02 15:49:57

Excellent answers from Luc and Mark however a good code sample is missing. Adding the tag android:focusableInTouchMode="true" and android:focusable="true" to parent layout (e.g. LinearLayout or ConstraintLayout) like the following example will fix the problem.

<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->

<!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
to prevent the dummy from receiving focus again -->
<AutoCompleteTextView android:id="@+id/autotext"

@Muhammad Babar 2014-03-21 05:58:50

What about setting the parent layout to android:focusableInTouchMode="true"!

@Sripathi 2014-05-06 11:12:50

@Muhammed Babar : I used to set the foucusable = "true" and android:focusableInTouchMode="true" of the parent layout..

@maddob 2014-07-10 09:12:01

Perfect! Thanks... One thing to note is that the dummy item has to be PLACED RIGHT BEFORE the focusable element!!! I had a TextView between my dummy and the EditText and the method did not work!

@Erik Soderstrom 2014-08-22 09:16:23

I just want to add, if you're applying this to many layouts, don't forget to check for <requestFocus/> and delete it, otherwise this fix won't work.

@sujith s 2015-03-24 10:15:19

setting android:focusableInTouchMode="true" to parent layout was the good approach thanks @Muhammad Babar

@Jacob R 2015-09-28 12:48:48

Great code, helped me a lot :) To try to improve upon it, android:focusableInTouchMode="true" covers android:focusable="true" + some more, so in this case android:focusable="true" is unnecessary, and can be removed. Additionally the dummy view can be a View instead of a LinearLayout. This saves processing power + saves you from some Warnings. Thus, my recommendation is replacing the dummy view with <View android:focusableInTouchMode="true" android:layout_width="0px" android:layout_height="0px"/>

@Weston Wedding 2016-03-08 16:47:59

@PietroRea Android has annoying auto-focus behaviors that are built into the core behavior of its app structure. If you tell it that an edit text is releasing or losing focus, it automatically makes decision on where the focus goes. Without a new object for the focus to transfer to, it picks the first focusable element in the layout even when that element is the one that just cleared its focus! It's kinda nuts and frustrating, but maybe there's a valid reason for this type of behavior.

@Amar 2017-07-25 09:38:28

This will not work in case you are creating EditText programmtically,for that add android:descendantFocusability="blocksDescendants" in the layout where you will be adding EditText and when done generating EditText settDescendantFocusability(BEFORE OR AFTER) in code.

@slezadav 2018-01-17 07:37:41

Isn't there an accessibility issue with this ? A random view getting focused ? I don't think this is the right solution.

@Minas Mina 2018-01-25 19:21:04

This is the best solution, which also works when the parent is ScrollView (android:focusableInTouchMode="true" does not work in that case).

@Ready Android 2018-05-04 12:54:45

Nice, can be done with window soft input mode like…

@Jeffrey 2018-06-22 21:01:28

A very clear answer by @Sniper…

@Christopher Smit 2018-06-27 07:55:53

@MuhammadBabar - Your comment should be an answer and the accepted one. THANKS!!

@Muhammad Babar 2018-06-27 08:00:13

@ChristopherSmit Welcome and thanks for tagging, Glad to see that BTW none of my answers had that much up vote than the comment above :)

@AustrianDude 2018-07-04 15:31:52

With Android P following changes should be considered: "Views with 0 area (either a width or a height is 0) are no longer focusable. Additionally, activities no longer implicitly assign initial focus in touch-mode. Instead, it is up to you to explicitly request initial focus, if desired."

@Daniel T 2019-02-20 03:49:02

This solution also works when the parent layout is ConstraintLayout.

@Silver 2011-12-26 23:35:55

A simpler solution exists. Set these attributes in your parent layout:

<RelativeLayout xmlns:android=""
    android:focusableInTouchMode="true" >

And now, when the activity starts this main layout will get focus by default.

Also, we can remove focus from child views at runtime (e.g., after finishing child editing) by giving the focus to the main layout again, like this:


Good comment from Guillaume Perrot:

android:descendantFocusability="beforeDescendants" seems to be the default (integer value is 0). It works just by adding android:focusableInTouchMode="true".

Really, we can see that the beforeDescendants set as default in the ViewGroup.initViewGroup() method (Android 2.2.2). But not equal to 0. ViewGroup.FOCUS_BEFORE_DESCENDANTS = 0x20000;

Thanks to Guillaume.

@tomrozb 2015-02-09 14:10:25

It's important to set these attributes to the direct parent of the view which gains focus on activity creation, otherwise it won't work.

@Meet Vora 2016-03-14 10:38:46

Just a perfect solution. Works for both: 1.Disabling autofocus, and 2. Disable auto-opening of soft keyboard

@Kurotsuki 2017-01-21 17:58:39

Nice. It does not need to be a direct parent. I set it at root level of layout and it seems work fine.

@Mark Buikema 2018-04-24 10:37:05

@DanielWilson when would you want a scrollview as direct parent of an edittext?

@Ready Android 2018-05-04 12:55:09

You have to work with window soft input like…

@Amir Raza 2019-11-27 14:29:37

Simply add android:focusableInTouchMode="true" in parent layout of EditText and you will get rid of this awkward behavior.

@Bukunmi 2019-07-27 20:06:12

I clear all focus with submit button

XML file:

android:focusableInTouchMode="true"> // 1. make this focusableInTouchMode...

JAVA file:

private LinearLayout mLinearLayout; // 2. parent layout element
private Button mButton;

mLinearLayout = findViewById(;
mButton = findViewById(;

mButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                mLinearLayout.requestFocus(); // 3. request focus


I hope this helps you :)

@Jyotiprakash Das 2019-09-16 12:44:05

Simple and reliable solution , just override this method :

public boolean dispatchTouchEvent(MotionEvent ev) {
    View v = getCurrentFocus();

    if (v != null &&
            (ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_MOVE) &&
            v instanceof EditText &&
            !v.getClass().getName().startsWith("android.webkit.")) {
        int scrcoords[] = new int[2];
        float x = ev.getRawX() + v.getLeft() - scrcoords[0];
        float y = ev.getRawY() + v.getTop() - scrcoords[1];

        if (x < v.getLeft() || x > v.getRight() || y < v.getTop() || y > v.getBottom())
    return super.dispatchTouchEvent(ev);

public static void hideKeyboard(Activity activity) {
    if (activity != null && activity.getWindow() != null && activity.getWindow().getDecorView() != null) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(activity.getWindow().getDecorView().getWindowToken(), 0);

@Rishabh Saxena 2018-04-18 06:14:56

Late but simplest answer, just add this in parent layout of the XML.


Upvote if it helped you ! Happy Coding :)

@Karthic Srinivasan 2018-12-29 14:24:43

worked perfectly for me. one more thing to note is, dont add these lines to scroll view. It wont work in scroll view. But worked perfectly with linear layout.

@Rohit Patil 2019-05-10 19:07:14

If you want to hide the keyboard at the start of the activity. Then mention


To that activity in the manifest file. Problem gets solved.


@Akash Suresh Dandwate 2019-02-22 06:33:25

add below line in Manifest file where you have mentioned your activity


@Dan 2019-03-29 15:02:20

This will prevent the keyboard showing up, which isn't what was asked.

@mmBs 2019-01-02 10:11:10

It can be achieved by inheriting EditText and overriding onTouchEvent.

class NonFocusableEditText: EditText {

    constructor(context: Context): super(context)
    constructor(context: Context, attrs: AttributeSet?): super(context, attrs)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int): super(context, attrs, defStyleAttr)

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        return if (isFocusable) super.onTouchEvent(event) else false

Then you can use it in the layouts like normal EditText:

        android:inputType="textNoSuggestions" />

@Lee Yi Hong 2012-11-28 10:32:35

I had tried several answers individually but the focus is still at the EditText. I only managed to solve it by using two of the below solution together.

<RelativeLayout xmlns:android=""
  android:focusableInTouchMode="true" >

( Reference from Silver )

and remove

 <requestFocus />

at EditText

( Reference from floydaddict )

@Nav 2014-09-09 13:12:22

I had to add edittext.clearFocus() in addition to the above to get it working :)

@hassan mirza 2018-09-09 09:28:32

You have edit text and list. In OnStart/On Create, you should set focus on list view: Listview.requestfocus()

@rgv 2019-03-05 19:33:29

I like this one because it's simple enough and clean

@Eric Mill 2009-10-12 19:02:37

Try clearFocus() instead of setSelected(false). Every view in Android has both focusability and selectability, and I think that you want to just clear the focus.

@Mark 2009-10-12 23:36:09

That sounds promising, but at what point in the Activity lifecycle should it be called? If I call it in onCreate(), the EditText still has focus. Should it be called in onResume() or some other location? Thanks!

@teedyay 2010-10-14 21:02:50

I combined the accepted answer with this answer. I called myEditText.clearFocus(); myDummyLinearLayout.requestFocus(); in the onResume of the Activity. This ensured the EditText didn't keep the focus when the phone was rotated.

@Luc 2009-10-23 08:12:09

The only solution I've found is:

  • Create a LinearLayout (I dunno if other kinds of Layout's will work)
  • Set the attributes android:focusable="true" and android:focusableInTouchMode="true"

And the EditText won't get the focus after starting the activity

@Rino 2017-11-15 05:31:31

Simple!! I initially added these properties to the parent view, which failed, then I set these to another layout and worked fine. Thanks!!

@floydaddict 2012-03-13 09:57:07

The problem seems to come from a property that I can only see in the XML form of the layout.

Make sure to remove this line at the end of the declaration within the EditText XML tags:

<requestFocus />

That should give something like that :


   //<requestFocus /> /* <-- without this line */

@Sharath kumar 2017-09-11 10:21:48

Easiest way to hide keyboard is using setSoftInputMode


or you can use InputMethodManager and hide keyboard like this.

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);

@Ricardo A. 2019-06-04 13:04:31

question is not about the keyboard

@Joe 2010-04-09 21:22:55

Is the actual problem that you just don't want it to have focus at all? Or you don't want it to show the virtual keyboard as a result of focusing the EditText? I don't really see an issue with the EditText having focus on start, but it's definitely a problem to have the softInput window open when the user did not explicitly request to focus on the EditText (and open the keyboard as a result).

If it's the problem of the virtual keyboard, see the AndroidManifest.xml <activity> element documentation.

android:windowSoftInputMode="stateHidden" - always hide it when entering the activity.

or android:windowSoftInputMode="stateUnchanged" - don't change it (e.g. don't show it if it isn't already shown, but if it was open when entering the activity, leave it open).

@martyglaubitz 2014-05-20 08:17:32

yet still the cursor is in the first EditText in the layout - even though the keyboard is not shown

@Joe 2014-07-08 23:38:05

@Anderson: Nothing about the answer implied that it would prevent the EditText from obtaining focus. It actually clearly states that this is how you prevent the software keyboard IME from opening automatically on focus; because it is more likely that the bigger concern is the soft keyboard popping up unexpectedly, not the focus itself. If your issue is with the EditText actually having focus at all, then use someone else's answer.

@Nick Franceschina 2014-10-08 18:26:38

set it programmatically:…

@Daniel Wilson 2015-06-16 11:54:25

Don't do this on an activity with a camera -- it breaks the timing of the flash (don't ask me why)

@n_b 2016-04-27 21:57:26

One reason to not have focus at startup is that if the imeOptions are changed after the view has focus and the keyboard shows, the imeOptions will be those set before the change.

@user924 2019-02-26 13:42:14

this answer doesn't make anysense cause will still see cursor in EditText view at startup

@user924 2019-02-26 13:42:57

@Joe this question isn't about hiding keyboard at startup. Such question was asked here…

@Joe 2019-02-27 15:21:17

@user924 asked and answered, 5 years ago. The upvotes clearly show that it isn't nonsense. It's not the accepted answer (and I'm okay with that because I'm offering an alternative solution to an inferred root cause). Thank you for linking to another question though; I don't care, but someone else might find it useful.

@MattSchmatt 2019-07-03 13:32:18

@Joe Thanks Joe, this helped a lot. You might want to add stateAlwaysHidden which also hides the soft-keyboard going back from another activity. See link

@Vishal Vaishnav 2017-07-26 09:52:46

Write this line in your Parent Layout...


@Ricardo A. 2019-06-04 13:01:58

duplicated answer

@atul 2012-02-14 04:56:12


@DkPathak 2016-09-28 08:19:58

View current = getCurrentFocus();

if (current != null) 

@Tarit Ray 2016-09-22 14:04:10

Write this code inside Manifest file in the Activity where you do not want to open the keyboard.


Manifest file:

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
    android:versionName="1.0" >

        android:targetSdkVersion="24" />

        android:theme="@style/AppTheme" >
            android:label="@string/app_name" >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            android:label="@string/app_name" >



@A. Rager 2016-11-01 18:06:08

This doesn't un-focus the text field: it merely hides the keyboard. You'll still get a hint that's pushed out of the field, and any color state selectors will display the "focused=true" state.

@Mansuu.... 2017-01-16 04:44:21

When your activity is opened, keyboard gets visible automatically which causes focusing of EditText. You can disable keyboard by writing the following line in your activity tag in manifest.xml file.


@Tulsi 2017-01-02 11:57:11

Do this and get your job done!


@Ricardo A. 2019-06-04 13:07:51

duplicated answer

@Muhammad Aamir Ali 2013-05-31 10:47:57

Make sure to remove the line "<requestFocus />" from the EditText tag in xml.


   <requestFocus /> <!-- remove this line --> 

@Sergey Sheleg 2014-03-17 15:42:53

Simple solution: In AndroidManifest in Activity tag use


@katzenhut 2014-09-04 10:04:05

Strictly speaking, this does not solve the issue. the OP said: "I don't want any control to have input focus at startup." Your solution only hides the keyboard, theres a sublte difference.

@Zach 2017-01-15 06:48:54

@katzenhut yep, thats my issue with this answer exactly. Focusing on my edittext opens up a PlaceAutoComplete activity, so this answer is incomplete

@Martin Marconcini 2018-03-21 00:13:53

This answer would be complete if the question was: How do I always ensure my activity never shows a keyboard. Which is not.

@Babar Sanah 2014-04-08 11:48:16

The following worked for me in Manifest. Write ,


@A. Rager 2016-11-01 18:05:08

This doesn't un-focus the text field: it merely hides the keyboard. You'll still get a hint that's pushed out of the field, and any color state selectors will display the "focused=true" state.

@Ish 2015-06-13 21:50:15

You can achieve this by creating a dummy EditText with layout width and height set to 0dp, and request focus to that view. Add the following code snippet in your xml layout:

     <requestFocus />

@prgmrDev 2015-06-24 09:19:11

Add android:windowSoftInputMode="stateAlwaysHidden" in the activity tag of the Manifest.xml file.


@A. Rager 2016-11-01 18:05:36

This doesn't un-focus the text field: it merely hides the keyboard. You'll still get a hint that's pushed out of the field, and any color state selectors will display the "focused=true" state.

@Vishal Raj 2015-12-21 15:33:36

Add following in onCreate method:


@A. Rager 2016-11-01 18:05:49

This doesn't un-focus the text field: it merely hides the keyboard. You'll still get a hint that's pushed out of the field, and any color state selectors will display the "focused=true" state.

@Karthik 2016-08-24 04:07:15

The easiest way is to add android:windowSoftInputMode="stateAlwaysHidden" in the activity tag of the Manifest.xml file

@A. Rager 2016-11-01 18:06:20

This doesn't un-focus the text field: it merely hides the keyboard. You'll still get a hint that's pushed out of the field, and any color state selectors will display the "focused=true" state.

@ceph3us 2015-07-09 21:08:25

 * set focus to top level window
 * disposes descendant focus 
 * disposes softInput 
 * */
public static void topLevelFocus(Context context){
        ViewGroup tlView = (ViewGroup) ((Activity) context).getWindow().getDecorView();

Related Questions

Sponsored Content

105 Answered Questions

[SOLVED] Close/hide android soft keyboard

33 Answered Questions

[SOLVED] How to save an activity state using save instance state?

18 Answered Questions

[SOLVED] Fling gesture detection on grid layout

50 Answered Questions

[SOLVED] You need to use a Theme.AppCompat theme (or descendant) with this activity

21 Answered Questions

[SOLVED] What's the best way to limit text length of EditText in Android

33 Answered Questions

[SOLVED] Activity restart on rotation Android

43 Answered Questions

Sponsored Content