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.
- 
                    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 Answers
in your onCreate() use this..
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
 
    
    - 4,245
- 2
- 26
- 39
- 
                    @CautionContinues, SudiptaforAndroid,Amitabha Biswas. Thanks, Happy Coding.! – Mehul Ranpara Dec 17 '15 at 08:12
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"
 
    
    - 153
- 1
- 10
 
    
    - 4,500
- 4
- 31
- 62
In your AndroidManifest.xml add the attribute android:windowSoftInputMode:
<activity android:name="your.package.ActivityName"
      android:windowSoftInputMode="stateHidden"  />
 
    
    - 2,420
- 23
- 24
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
 
    
    - 3,447
- 6
- 42
- 61
You can do this using intputmethodmangare... using the following code..
InputMethodManager imm = (InputMethodManager)getSystemService(
      Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
 
    
    - 6,004
- 2
- 27
- 45
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.
 
    
    - 829
- 1
- 13
- 15
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);
    }
}
 
    
    - 1
