288

I have an activity with an Edit Text input. When the activity is initialized, the Android keyboard is shown. How can the keyboard remain hidden until the user focuses the input?

5

18 Answers 18

448

I think the following may work

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

I've used it for this sort of thing before.

5
  • Is there a way to set it to be a digits-only keypad? i.e. 12Key keyboard? Feb 27, 2015 at 15:46
  • @MohamedKhamis input.setRawInputType(Configuration.KEYBOARD_12KEY);
    – Karan
    Apr 27, 2015 at 9:46
  • 1
    Yes it still works. @SagarNayak why would you want to hide keyboard for EditText?:) This is to hide keyboard when activity starts which contains EditText
    – Martynas
    Jun 14, 2016 at 20:55
  • @Devealte 7 years later and it worked for me, did you place it in onCreate?
    – Dymas
    Mar 13, 2019 at 16:50
  • @Dymas Yes, and I just fixed it several months ago :)
    – Devealte
    Mar 13, 2019 at 21:38
196

Try this -

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

Alternatively,

  1. you could also declare in your manifest file's activity -
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Main"
          android:label="@string/app_name"
          android:windowSoftInputMode="stateHidden"
          >
  1. If you have already been using android:windowSoftInputMode for a value like adjustResize or adjustPan, you can combine two values like:
<activity
        ...
        android:windowSoftInputMode="stateHidden|adjustPan"
        ...
        >

This will hide the keyboard whenever appropriate but pan the activity view in case the keyboard has to be shown.

0
34

Hide it for all activities using the theme

<style name="MyTheme" parent="Theme">
    <item name="android:windowSoftInputMode">stateHidden</item>
</style>

set the theme

<application android:theme="@style/MyTheme">
2
  • like this global approach.
    – Rm558
    Dec 21, 2015 at 13:42
  • 1
    This worked as i was using different themes at various places
    – nmxprime
    Mar 10, 2016 at 12:21
24

Add these two properties to your parent layout (ex: Linear Layout, Relative Layout)

android:focusable="false"
android:focusableInTouchMode="false" 

It will do the trick :)

4
  • 2
    This doesn't work for me, however setting them to true works, as per Jack T's answer. Was there a behavior change in recent versions?
    – Protean
    Apr 25, 2018 at 5:08
  • In addition to my answer, you need to check the other properties as well which you have in manifest and for edit text. Apr 25, 2018 at 5:13
  • I just have the most basic properties in them. I don't understand why setting these to false should work though, as the idea is to get the focus away from the EditText boxes.
    – Protean
    Apr 25, 2018 at 5:17
  • Maybe it used to get the focus away from the EditText boxes by getting it away from the parent layout? But not anymore, it seems.
    – Protean
    Apr 25, 2018 at 5:21
14

If you are using API level 21, you can use editText.setShowSoftInputOnFocus(false);

14

Try to declare it in manifest file

<activity
    android:name=".HomeActivity"
    android:label="@string/app_name"
    android:windowSoftInputMode="stateAlwaysHidden" >
11

Just Add in AndroidManifest.xml

<activity android:name=".HomeActivity"  android:windowSoftInputMode="stateHidden">
</activity>
10

You can also write these lines of code in the direct parent layout of the .xml layout file in which you have the "problem":

android:focusable="true"
android:focusableInTouchMode="true"

For example:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    ...
    android:focusable="true"
    android:focusableInTouchMode="true" >

    <EditText
        android:id="@+id/myEditText"
        ...
        android:hint="@string/write_here" />

    <Button
        android:id="@+id/button_ok"
        ...
        android:text="@string/ok" />
</LinearLayout>


EDIT :

Example if the EditText is contained in another layout:

<?xml version="1.0" encoding="utf-8"?>
<ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    ... >                                            <!--not here-->

    ...    <!--other elements-->

    <LinearLayout
        android:id="@+id/theDirectParent"
        ...
        android:focusable="true"
        android:focusableInTouchMode="true" >        <!--here-->

        <EditText
            android:id="@+id/myEditText"
            ...
            android:hint="@string/write_here" />

        <Button
            android:id="@+id/button_ok"
            ...
            android:text="@string/ok" />
    </LinearLayout>

</ConstraintLayout>

The key is to make sure that the EditText is not directly focusable.
Bye! ;-)

0
7

Best solution for me, paste your class

@Override
public void onResume() {
    this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    super.onResume();
}

@Override
public void onStart() {
    this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    super.onStart();
}
7

Just add this in your manifest.xml file

<activity android:name=".MainActivity"
            android:windowSoftInputMode="stateHidden">

You are all done.

4

To expand upon the accepted answer by @Lucas:

Call this from your activity in one of the early life cycle events:

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

Kotlin Example:

override fun onResume() {
  super.onResume()

  window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)
}
3

Function to hide the keyboard.

public static void hideKeyboard(Activity activity) {
    View view = activity.getCurrentFocus();

    if (view != null) {
        InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);

        inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }
}

Hide keyboard in AndroidManifext.xml file.

<activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    android:windowSoftInputMode="stateHidden">
2

You can try this set unique attribute for each element

TextView mtextView = findViewById(R.id.myTextView);
mtextView.setShowSoftInputOnFocus(false);

Keyboard will not show while element is focus

1
  • While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer - From Review
    – Nick
    Nov 26, 2018 at 12:25
1
//to hide the soft keyboard
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
0

just add this on your Activity:

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
      if (getCurrentFocus() != null) {
           InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
           imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
      }
      return super.dispatchTouchEvent(ev);
}
0

declare this code( android:windowSoftInputMode="stateAlwaysHidden") in manifest inside your activity tag .

like this :

<activity android:name=".MainActivity"
  android:windowSoftInputMode="stateAlwaysHidden">
0

Only this solution worked for me on API 26 & Kotlin

   override fun onResume() {
    window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)
    super.onResume()
}
-1

or You can use focusable tag in xml.

android:focusable="false"

set it to false.here is the snippet of the code

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.