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?
 
    
    - 11,553
- 8
- 64
- 88
 
    
    - 13,964
- 13
- 65
- 83
8 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.
 
    
    - 43,056
- 28
- 105
- 132
- 
                    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? – stealthcopter Apr 30 '10 at 10:58
- 
                    1@stealthcopter: can you tell me which Android version you're using. I'm using 2.1 & 2.2, but requestFocus doesn't work. – anticafe Mar 05 '11 at 15:00
- 
                    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. – stealthcopter Mar 06 '11 at 14:58
- 
                    19I've tried the XML approach, I've tried the programmatic approach. Yet the EditText retains focus. VERY ANNOYING ! – Someone Somewhere Jul 25 '11 at 21:34
- 
                    for an EditText, use programmatic method and be sure to use `setFocusableInTouchMode(true);` – Someone Somewhere Jul 25 '11 at 21:58
- 
                    4is here: http://developer.android.com/guide/topics/resources/layout-resource.html#requestfocus-element – Erik B Apr 27 '12 at 20:43
- 
                    I have found it when I added a new TextView however this is not seen in xml while typing ctrl +space. – Günay Gültekin Jan 13 '13 at 12:30
- 
                    @SomeoneSomewhere, see [Stop EditText from gaining focus at Activity startup](http://stackoverflow.com/questions/1555109/stop-edittext-from-gaining-focus-at-activity-startup) – Eido95 Jan 26 '17 at 18:49
@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>
 
    
    - 166
- 1
- 2
- 12
 
    
    - 738
- 6
- 8
- 
                    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(); – Sagar Hatekar Jan 19 '12 at 15:30
- 
                    2Thank 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`. – kaka Sep 27 '12 at 12:49
- 
                    1With 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." – AustrianDude Jul 04 '18 at 15:29
Set both :focusable and :focusableInTouchMode to true and call requestFocus. It does the trick.
 
    
    - 15,729
- 10
- 59
- 55
 
    
    - 835
- 8
- 8
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.
 
    
    - 371
- 4
- 3
I just add this line of code into onCreate():
this.getWindow().setSoftInputMode(
    WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Problem solved.
 
    
    - 79,279
- 19
- 185
- 195
 
    
    - 111
- 1
- 3
Use the code below,
TableRow _tableRow =(TableRow)findViewById(R.id.tableRowMainBody);
tableRow.requestFocus();
that should work.
 
    
    - 5,132
- 7
- 45
- 54
@Someone Somewhere I used this to clear focus:
editText.clearFocus();
and it helps
 
    
    - 358
- 3
- 9
android:focusedByDefault="true"
 
    
    - 2,684
- 3
- 40
- 66
- 
                    2Attribute `focusedByDefault` is only used in API level 26 and higher (current min is 21). – AlexS Nov 20 '20 at 10:25