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?
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.
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.
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.
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.
@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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With