listview - How to stop EditText from gaining focus at Activity startup in Android

ID : 181

viewed : 287

Tags : androidlistviewandroid-edittextfocusandroid

Top 5 Answer for listview - How to stop EditText from gaining focus at Activity startup in Android

vote vote


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"/> 
vote vote


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

vote vote


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

<RelativeLayout xmlns:android=""     android:id="@+id/mainLayout"     android:descendantFocusability="beforeDescendants"     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.

vote vote


The only solution I've found is:

  • Create a LinearLayout (I don't know 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

vote vote


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 :

<EditText    android:id="@+id/emailField"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:inputType="textEmailAddress">     //<requestFocus /> /* <-- without this line */ </EditText> 

Top 3 video Explaining listview - How to stop EditText from gaining focus at Activity startup in Android