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
12 years 4 weeks ago -
@tommy_banane (tom b.)
RT @AndroidFavorite: #Android New Desktop Android Market Is Live, Adds Several New Features http://zorr0.nl/lFwXNz
12 years 4 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
12 years 4 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
12 years 4 weeks ago -
@tayaitapps (Taya IT)
Next Google TV Looks A Lot Like Android http://t.co/dvlTim3 via @alleyinsider #google #apple #android #tv #honeycomb
12 years 4 weeks ago
Poll
Useful resources
Android Development Projects
- App Development
- Telegram bulk messages sender
- Photo Editing Application
- React Native Developer - part time basis
- Locket App Copy
- Courier app $150 only
- Mobile Application Development - IOS and Android (Rewards for sharing brand ads on social media)
- Android app for sms
- Fitness app project for sale online
- io developer