Remove autofocus from an EditText in Android
When we create a layout with an EditText or an AutoCompleteTextView, for some reason, it always gains the focus on starting.

Here is the main.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.c
om/apk/res/android" - android:orientation="vertical" android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <Button android:text="@string/button_text"
; android:id="@+id/Button01" - android:layout_width="wrap_content" android:layout_height="wrap_content">
- </Button>
- <EditText android:text="" android:id="@+id/EditText01"
- android:layout_width="wrap_content" android:layout_height="wrap_content"
- android:hint="@string/hint">
- </EditText>
- <Button android:text="@string/button_text"
; android:id="@+id/Button02" - android:layout_width="wrap_content" android:layout_height="wrap_content">
- </Button>
- </LinearLayout>
How can we remove the focus form the EditText, without giving it to an other visible layout element?
The simplest (and I think, the only 100% working) solution is, to create an invisible LinearLayout. This LinearLayout will grab the focus from the EditText.
Modify the main.xml like this, add a LinearLayout before the EditText:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.c
om/apk/res/android" - android:orientation="vertical" android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <Button android:text="@string/button_text"
; android:id="@+id/Button01" - android:layout_width="wrap_content" android:layout_height="wrap_content">
- </Button>
- <LinearLayout android:focusable="true"
- android:focusableInTouchMode="true" android:layout_width="0px"
- android:layout_height="0px" />
- <EditText android:text="" android:id="@+id/EditText01"
- android:layout_width="wrap_content" android:layout_height="wrap_content"
- android:hint="@string/hint">
- </EditText>
- <Button android:text="@string/button_text"
; android:id="@+id/Button02" - android:layout_width="wrap_content" android:layout_height="wrap_content">
- </Button>
- </LinearLayout>

No other coding needed, it's a simple and clear solution.
New tutorials from Helloandroid
Recent Apps
Android on Twitter
-
@Idevicegazette (iDevice Gazette)
GSM-to-Skype bridge lets you lose those roaming fees http://bit.ly/lbRJeh #android
11 years 6 weeks ago -
@tommy_banane (tom b.)
RT @AndroidFavorite: #Android New Desktop Android Market Is Live, Adds Several New Features http://zorr0.nl/lFwXNz
11 years 6 weeks ago -
@dwilliams5 (Dennis Williams)
just completed a runtastic run of 3.02 km in 40 min 11 s with #runtastic #Android App: http://tinyurl.com/5tvrpe3
11 years 6 weeks ago -
@S_Pinz (Spinz!)
RT @Androidheadline: Out of box #LG Optimus 3D got Quadrant 2420 score. Thanks @blink_c #io2011 #android http://twitpic.com/4whkdz
11 years 6 weeks ago -
@tayaitapps (Taya IT)
Next Google TV Looks A Lot Like Android http://t.co/dvlTim3 via @alleyinsider #google #apple #android #tv #honeycomb
11 years 6 weeks ago
Poll
Useful resources
Android Development Projects
- iOS/Android Developer to take older Games and bring them Current
- Android apps developer - need to finish urgent.
- Buliding MobileApp For onlie order
- looking for android APP developers
- Create an ecommerce app
- text-to voice for smartphones IOS - GOOGLE - HARMONY - AND ALEXA
- Optimize Images on App
- Create small feature with drag-drop text for Android
- Scouting for advanced website and Mobile apps developers. Potential Long-term contract.
- BLACK SCREEN