tags:

views:

1197

answers:

5

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?

+1  A: 

Try clearFocus() instead of setSelected(false). Every view in Android has both focusability and selectability, and I think you want to just clear the focus.

Klondike
That sounds promising, but at what point in the Activity lifecycle should it be called? If I call it in onCreate(), the EditText still has focus. Should it be called in onResume() or some other location? Thanks!
Mark
I combined the accepted answer with this answer. I called `myEditText.clearFocus(); myDummyLinearLayout.requestFocus();` in the `onResume` of the Activity. This ensured the EditText didn't keep the focus when the phone was rotated.
teedyay
+5  A: 

I have the same problem. Tested the same - clearFocus() - without any result. Some solution? Thanks :)

Edit!! The only solution I've found after the whole day is:

  • Create a LinearLayout (I dunno 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 main focus after starting activity :)

Luc
A: 

Yeah I did the same thing - create a 'dummy' linear layout which gets initial focus. Furthermore, I set the 'next' focus IDs so the user can't focus it any more after scrolling once:

dummy.setNextFocusDownId(et.getId()); dummy.setNextFocusUpId(et.getId()); et.setNextFocusUpId(et.getId());

a lot of work just to get rid of focus on a view..

Thanks

mark
+12  A: 

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/text"
 android:layout_width="fill_parent" android:layout_height="wrap_content"
 android:nextFocusUp="@+id/text" android:nextFocusLeft="@+id/text"/>
morganchristiansson
This only works so far. If you are building a widget, you can't use the nextFocusUp hack, as you'll kill your ability to focus things above the widget. There's got to be some sort of initial focus resolution that takes place, as buttons won't get the initial focus, but an EditText will.
Steve Pomeroy
Setting the focusable and focusableIntouch to true worked for me. Thanks a lot to everyone for sharing, It'd of take me ages to discover this ¿bug? ¿weird feature?
Maragues
+8  A: 

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)

Joe
I realize this doesn't answer the specific question, but it's a very similar case and it answered my own question. Personally I sprinkled this across most of my Activities. Popping open the keyboard and squishing the view immediately is bad UI design. The user needs context for what they're looking at before editing.
DougW