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
Android on Twitter
-
@StephanieNich10 (Stephanie Nichols)#android I laughed so hard at ochocinco 's avi. Hahahaha http://t.co/MPuhhi3m
25 weeks 1 day ago -
@CarlaAtkins8 (Carla Atkins)#android Omg! This is actually f'n interesting http://t.co/JodMOehr
25 weeks 1 day ago -
@MarianMcleod12 (Marian Mcleod)#android Precisely what song is? http://t.co/YmJXU0rB
25 weeks 1 day ago -
@JoBeach15 (Jo Beach)#android haha this made me laugh, i love ted:-) http://t.co/gtcWQ79C
25 weeks 1 day ago -
@aochart3 (青ちゃ)Start playing Paradise Island on Android http://t.co/DEID0Ao5 #Android #Androidgames #Gameinsight http://t.co/e1bifSeL
25 weeks 1 day ago
Poll
Useful resources
Android Development Projects
- simple android app just convert - repost by thorapps
- Android 4pics1word Game by yhassany
- Synchronize Faceebook and Twitter feeds by mahdidarwiche
- Fake bomb for Tablet (for airsoft / paintball games) by seanchiarot
- Trading application by TravisVB
- Android App - live footy (to complete started project) by dmaco
- Worksmart mobile application by Richabhatia2
- Chess APP for Web and Mobile by johnohiggs
- androidApp - repost by mmalselek
- Nonpublic project #4532466 by folababa



