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,136
- 2
- 23
- 38
-
@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"
- 113
- 1
- 9
- 4,486
- 4
- 27
- 60
In your AndroidManifest.xml add the attribute android:windowSoftInputMode:
<activity android:name="your.package.ActivityName"
android:windowSoftInputMode="stateHidden" />
- 2,372
- 20
- 24
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
- 3,268
- 6
- 36
- 60
You can do this using intputmethodmangare... using the following code..
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
- 5,822
- 1
- 22
- 43
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.
- 644
- 1
- 8
- 13
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