43
votes

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);
7

7 Answers

121
votes

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"
    ...
>
19
votes

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.

2
votes

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); 
1
votes

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>
1
votes

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"
0
votes

The following code works for me

((InputMethodManager) iClockActivity
                    .getSystemService(Context.INPUT_METHOD_SERVICE))
                    .showSoftInput(textView, 0);
0
votes

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....