android - Stop EditText from gaining focus at Activity startup


I have an Android Activity, 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:

EditText.setSelected(false);

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



Answers


Excellent answers from Luc and Mark however a good code sample is missing:

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



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




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 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:

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




Android: Force EditText to remove focus?

You can add this to onCreate and it will hide the keyboard everytime the activty starts.

You could also programatically change the focus to another item.

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);



You can make cursor and focus disappear by

edittext.clearFocus();

But detect when the key board hide is a hard work.




Add LinearLayout before EditText in your XML.

<LinearLayout 
    android:focusable="true"
    android:focusableInTouchMode="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" />



Dont want soft keyboard to automatically show on launch - Android

You can try:

android:windowSoftInputMode="stateHidden"

for your activity in AndroidManifest.xml




Add this two lines to you main layout.

android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"



Add in your layout definition (in xml file) this following options :

android:focusable="true" android:focusableInTouchMode="true"



android: get rid of focus when enter activity

I had the similar issue and i am sure this is the problem, for that you just need to do a small trick.

Add this LinearLayout before the EditText which is getting focus at activity startup. You don't need to set any value inside the EditText.

<LinearLayout
    android:focusable="true" 
    android:focusableInTouchMode="true"
    android:layout_width="0dip" 
    android:layout_height="0dip"
    android:id="@+id/dummyView"/>



Add this in <activity> tag in your manifest for activity in which edittext is present.

android:windowSoftInputMode="stateHidden"



put android:focusable="false" may it will work for edittext