Set initial focus in an android application


Question

In my android application it automatically focuses the first Button I have in my layout, giving it an orange outline. How can I set the initial focus preferably in XML, and can this be set to nothing?

1
83
4/30/2010 9:31:37 AM

Accepted Answer

You could use the requestFocus tag:

<Button ...>
  <requestFocus />
</Button>

I find it odd though that it auto-focuses one of your buttons, I haven't observed that behavior in any of my views.

156
4/30/2010 10:33:58 AM

@Someone Somewhere, I tried all of the above to no avail. The fix I found is from http://www.helloandroid.com/tutorials/remove-autofocus-edittext-android . Basically, you need to create an invisible layout just above the problematic Button:

<LinearLayout android:focusable="true"
              android:focusableInTouchMode="true" 
              android:layout_width="0px"
              android:layout_height="0px" >
    <requestFocus />
</LinearLayout>

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon