1

Hi I have some EditTexts and a button at the bottom of the view. Upon starting the activity, the first EditText will gain focus and a keyboard will pop up. I want to upon starting the activity, the focus will be on the button instead of the EditText and the keyboard should not pop up. I tried running:

button.requestFocus(); 

The EditText still have focus and pops up the keyboard. How can I resolve this? Thanks.

Nikhil
  • 16,194
  • 20
  • 64
  • 81
AndroidBase
  • 41
  • 2
  • 5

3 Answers3

5

Its trickier than it seems, but certainly possible. See here:

Stop EditText from gaining focus at Activity startup

Community
  • 1
  • 1
Yony
  • 1,264
  • 1
  • 10
  • 19
2

Put android:windowSoftInputMode="stateHidden" to your Activity which contains EditText

    <activity android:name=".YourActivity" 
        android:screenOrientation="portrait" 
        android:windowSoftInputMode="stateHidden">
    </activity>
Anh Tuan
  • 1,728
  • 1
  • 13
  • 25
1

You can force hide the keyboard by calling this on the EditText view

    InputMethodManager mgr = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    mgr.hideSoftInputFromWindow(myView.getWindowToken(), 0);

Hope this helps,

-serkan

serkanozel
  • 2,947
  • 1
  • 23
  • 27