43

In an Android app, whenever the activity launches, the textbox gets the focus and the soft keyboard pops up automatically. I have tried to stop this by using following line in onCreate method, but it does not work.

((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(EditText.getWindowToken(), 0);
Adnan
  • 2,946
  • 7
  • 41
  • 63
  • Possible duplicate of [How to remove auto focus/keyboard popup of a field when the screen shows up?](https://stackoverflow.com/questions/2892615/how-to-remove-auto-focus-keyboard-popup-of-a-field-when-the-screen-shows-up) – Shirish Herwade Apr 13 '18 at 09:19

7 Answers7

120

I know this is old but maybe it will help someone in the future...

I haven't seen anyone suggest "stateHidden"

From the Android docs - android:windowSoftInputMode

Your manifest file would look like:

<activity
    ...
    android:windowSoftInputMode="stateHidden|adjustResize"
    ...
>
rf43
  • 4,315
  • 3
  • 21
  • 28
19

You can use the following line of code to make sure the keyboard only pops up when a user clicks into an EditText

Java

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

Kotlin

window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)

And

You need to add

android:windowSoftInputMode="adjustResize"

to your activity tag in the AndroidManifest.xml file.

Đorđe Nilović
  • 2,772
  • 1
  • 19
  • 20
Donal Rafferty
  • 19,239
  • 37
  • 110
  • 186
  • This works, but when I click the text box for typing, the keyboard pops up over the text box an I can't see what I am typing. The text box is at the bottom of the screen. – Adnan Jun 15 '10 at 08:09
  • You need to add android:windowSoftInputMode="adjustResize" to your tag in the AndroidManifest.xml file. – Donal Rafferty Jun 15 '10 at 08:23
  • android:windowSoftInputMode="adjustResize" is set in AndroidManifest.xml but still same problem. There is a button in same screen too. I tried to move focus to the button by using: btn.setFocusable(true); btn.requestFocus(); But still the focus is on TextView. I don't know what I am doing wrong. – Adnan Jun 15 '10 at 08:40
  • Strange, can you post your code? The activity tag from the manifest and the code from your UI Activity? – Donal Rafferty Jun 15 '10 at 09:14
  • 1
    I am going to post the code as an answer of this question, as code formatting is not available in comments. – Adnan Jun 15 '10 at 10:12
2

Does the following work?

// Find editor
EditText editWindowInstance = this.findViewById(R.id.MyEditWindow);

// close soft keyboard 
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(editWindowInstance.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); 
Thorsten Dittmar
  • 52,871
  • 8
  • 78
  • 129
1

You can put this code in your Activity.onCreate: this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

But I found that the most reliable and clean way was to simply set the focus onto to a different view, in your activity XML Layout

<Button
  android:id="@+id/mybutton">
  <requestFocus />
</Button>
Mtl Dev
  • 1,375
  • 14
  • 26
1

None of the solutions worked for me. Finally a very easy solution worked like magic. Add these two lines in your parent layout.

android:focusable="true" 
android:focusableInTouchMode="true"
Anuj Garg
  • 909
  • 7
  • 10
0

This will work perfectly, try this

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

And add the following to your manifest.

android:windowSoftInputMode="stateHidden|adjustResize"

cheers enjoy coding....

Abhi
  • 101
  • 1
  • 4
  • 11
0

The following code works for me

((InputMethodManager) iClockActivity
                    .getSystemService(Context.INPUT_METHOD_SERVICE))
                    .showSoftInput(textView, 0);
amithgc
  • 5,597
  • 6
  • 25
  • 37