11

I have an activity with lots of edittext. whenever I load that activity, the keyboard appears and eats half of the screen which makes that activity's look bad. So is there any way to hide keyboard when I load that activity.

Caution Continues
  • 733
  • 2
  • 10
  • 25
  • Possible duplicate of [How to hide Soft Keyboard when activity starts](https://stackoverflow.com/questions/18977187/how-to-hide-soft-keyboard-when-activity-starts) – Hasan El-Hefnawy Jul 11 '19 at 06:47

7 Answers7

38

in your onCreate() use this..

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Mehul Ranpara
  • 4,186
  • 2
  • 24
  • 39
9

Add this two line in your activity's XML file in the RootLayout i.e. either relative or linear(whatever you have taken) :

android:focusableInTouchMode="true" 

Add this line in activity manifests file

 android:windowSoftInputMode="stateHidden"
Usman Arshad
  • 133
  • 1
  • 10
AndroidLearner
  • 4,504
  • 4
  • 28
  • 61
8

In your AndroidManifest.xml add the attribute android:windowSoftInputMode:

<activity android:name="your.package.ActivityName"
      android:windowSoftInputMode="stateHidden"  />
Hoshouns
  • 2,422
  • 21
  • 24
5
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
Venkat
  • 3,338
  • 6
  • 40
  • 61
0

You can do this using intputmethodmangare... using the following code..

InputMethodManager imm = (InputMethodManager)getSystemService(
      Context.INPUT_METHOD_SERVICE);

imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
Jigar Pandya
  • 5,902
  • 2
  • 24
  • 43
0

Put this code on the onCrete function:

new Handler().postDelayed(new Runnable() { @Override public void run() { InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(view.getWindowToken(), 0); view.clearFocus(); }}, 50);

where view is your EditText

The runnable is because the code might be executed before the editText is rendered.

Tincho825
  • 739
  • 1
  • 10
  • 14
0

I created a method which I call in all the required Activity classes in the onCreate event. Worked for me in all scenarios.

public class ClassLib {
        public static void hideKeyboard(Activity activity) {
        //Hide keyboard
        activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    }
}
A.D
  • 1