Set initial focus in an Android application
Asked Answered
P

9

91

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?

Priest answered 30/4, 2010 at 9:31 Comment(0)
A
168

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.

Alkalinize answered 30/4, 2010 at 10:33 Comment(8)
Yeah, it has happened with two of my applications now. I guess I could requestFocus onto a element that doesn't change like a TextView or would this not be allowed?Priest
@stealthcopter: can you tell me which Android version you're using. I'm using 2.1 & 2.2, but requestFocus doesn't work.Condensed
Well I've been using it across all versions, but the problem I was having seemed to disappear on it's own so I haven't investigated much.Priest
I've tried the XML approach, I've tried the programmatic approach. Yet the EditText retains focus. VERY ANNOYING !Jacklight
for an EditText, use programmatic method and be sure to use setFocusableInTouchMode(true);Jacklight
<requestFocus /> is here: developer.android.com/guide/topics/resources/…Blaise
I have found it when I added a new TextView however this is not seen in xml while typing ctrl +space.Aracelis
@SomeoneSomewhere, see Stop EditText from gaining focus at Activity startupEp
G
68

@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>
Gagarin answered 16/11, 2011 at 5:54 Comment(3)
I was able to gain focus on an AlertDialog and enabling focusableintouchmode helped me get it right. Essentially, here's how: alert.show(); alert.getButton(AlertDialog.BUTTON_POSITIVE).setFocusableInTouchMode(true);alert.getButton(AlertDialog.BUTTON_NEGATIVE).requestFocus();Sergent
Thank you! This was the only solution that worked in my case. Although I used it slightly different; I put the focus stuff in a RelativeLayout containing the EditText.Mettlesome
With Android P following changes are coming to the platform: "Views with 0 area (either a width or a height is 0) are no longer focusable. Additionally, activities no longer implicitly assign initial focus in touch-mode. Instead, it is up to you to explicitly request initial focus, if desired."Bendicty
T
29

Set both :focusable and :focusableInTouchMode to true and call requestFocus. It does the trick.

Trow answered 26/4, 2011 at 21:18 Comment(1)
Yes this is the true solution to fix it. Thanks AndersYtterbium
K
25

I found this worked best for me.

In AndroidManifest.xml <activity> element add android:windowSoftInputMode="stateHidden"

This always hides the keyboard when entering the activity.

Karmakarmadharaya answered 31/7, 2013 at 14:59 Comment(2)
Easy and clean!Spoofery
great... this is very impressive!Yokoyokohama
B
11

I just add this line of code into onCreate():

this.getWindow().setSoftInputMode(
    WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

Problem solved.

Bybee answered 2/6, 2012 at 14:35 Comment(0)
P
8

Use the code below,

TableRow _tableRow =(TableRow)findViewById(R.id.tableRowMainBody);
tableRow.requestFocus();

that should work.

Pinchcock answered 30/1, 2012 at 22:6 Comment(0)
S
4

@Someone Somewhere I used this to clear focus:

editText.clearFocus();

and it helps

Scriptorium answered 27/9, 2011 at 6:43 Comment(0)
R
3
android:focusedByDefault="true"
Righteous answered 29/11, 2019 at 11:27 Comment(1)
Attribute focusedByDefault is only used in API level 26 and higher (current min is 21).Favus
B
0

in my case, I had to put isFocusable = true in the init block of my custom view:

init {
    isFocusable = true
}
Barroom answered 20/5 at 17:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.