Home » Android » android – How to Hide keyboard when activity starts

android – How to Hide keyboard when activity starts

Posted by: admin May 14, 2020 Leave a comment

Questions:

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.

How to&Answers:

in your onCreate() use this..

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

Answer:

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"

Answer:

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

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

Answer:

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

Answer:

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

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

imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);

Answer:

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.