Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set initial focus in an Android application

Tags:

android

focus

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?

like image 264
stealthcopter Avatar asked Apr 30 '10 09:04

stealthcopter


People also ask

What is focusable in android?

Focusable means that it can gain the focus from an input device like a keyboard. Input devices like keyboards cannot decide which view to send its input events to based on the inputs itself, so they send them to the view that has focus.

Is focused by default android?

Although every View can be made focusable, not all are focusable by default. You can use the android:focusable property in XML or View#setFocusable(boolean) API in Java to override the default value. EditTexts, Buttons and ScrollViews are examples of Views that are focusable by default.

What is android view view?

View is a basic building block of UI (User Interface) in android. A view is a small rectangular box that responds to user inputs. Eg: EditText, Button, CheckBox, etc. ViewGroup is an invisible container of other views (child views) and other ViewGroup.


2 Answers

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.

like image 106
Matthias Avatar answered Sep 25 '22 04:09

Matthias


@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> 
like image 31
Vince Lasmarias Avatar answered Sep 26 '22 04:09

Vince Lasmarias