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
  • 723
  • 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,136
  • 2
  • 23
  • 38
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
  • 113
  • 1
  • 9
AndroidLearner
  • 4,486
  • 4
  • 27
  • 60
8

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

<activity android:name="your.package.ActivityName"
      android:windowSoftInputMode="stateHidden"  />
Hoshouns
  • 2,372
  • 20
  • 24
5
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
Venkat
  • 3,268
  • 6
  • 36
  • 60
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,822
  • 1
  • 22
  • 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
  • 644
  • 1
  • 8
  • 13
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