-2

I am creating an app which contains multiple activities. A number of my activities have an 'EditText' field. As soon as I enter these activities, the keyboard instantly pops up assuming I want to type something straight away.

Does anyone have a simple code I can add into my java file that will prevent the keyboard to pop up by default because there is an 'EditText' field.

If you can also specify where to place the line of code such as whether it goes in the onCreate method etc will be appreciated.

I'm assuming the following will work, but where do I need to place it?

getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN
);
Boken
  • 3,207
  • 9
  • 25
  • 31
N MC
  • 207
  • 1
  • 9
  • Check this answer: [http://stackoverflow.com/questions/18977187/how-to-hide-soft-keyboard-when-activity-starts](http://stackoverflow.com/questions/18977187/how-to-hide-soft-keyboard-when-activity-starts) – cameronlund4 May 18 '16 at 02:22

2 Answers2

0

The above code can be placed in the onCreate method.

p.s I figured this out after some trial and error, hope it helps others

N MC
  • 207
  • 1
  • 9
0

There are multiple answers for this.

You can add this to your menifest file.

<activity android:name="com.your.package.ActivityName"
      android:windowSoftInputMode="stateHidden"  />

OR

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)‌​;

OR

You can call this method in your onCreate

/**
 * Hides the soft keyboard
 */
public void hideSoftKeyboard() {
    if(getCurrentFocus()!=null) {
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
    }
}
Anuj Sharma
  • 4,016
  • 2
  • 32
  • 50