Categories
android android-activity android-edittext focus listview

How to stop EditText from gaining focus when an activity starts in Android?

3099

I have an Activity in Android, with two elements:

  1. EditText
  2. ListView

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

EditText.setSelected(false);
EditText.setFocusable(false);

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

0

    2806

    Adding the tags android:focusableInTouchMode="true" and android:focusable="true" to the parent layout (e.g. LinearLayout or ConstraintLayout) like in the following example, will fix the problem.

    <!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
    <LinearLayout
        android:focusable="true" 
        android:focusableInTouchMode="true"
        android:layout_width="0px" 
        android:layout_height="0px"/>
    
    <!-- :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"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:nextFocusUp="@id/autotext" 
        android:nextFocusLeft="@id/autotext"/>
    

    4

    • 752

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

      Mar 21, 2014 at 5:58

    • @MuhammadBabar android:focusableInTouchMode=”true” create a zoom view in emulator

      May 16, 2021 at 23:22

    • In my case adding to even a text view also works

      Jun 1, 2021 at 11:39

    • if SO allow me to upvote questions multiple times then i will upvote this answer twice. Thank you brother

      – D_K

      Mar 8 at 12:39

    1734

    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 on the EditText? I don’t really see an issue with the EditText having a focus on the 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).

    2

    • 26

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

      May 20, 2014 at 8:17

    • 45

      @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.

      – Joe

      Jul 8, 2014 at 23:38

    1196

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

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/mainLayout"
        android:descendantFocusability="beforeDescendants"
        android:focusableInTouchMode="true" >
    

    And now, when the activity starts this main layout will get focused 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:

    findViewById(R.id.mainLayout).requestFocus();
    

    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 is 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.

    0