Prevent the keyboard from displaying on activity start

JavaAndroidFocusAndroid Keypad

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

I think the following may work

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

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

Solution 2 - Java

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"
          >

2. 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.

Solution 3 - Java

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">

Solution 4 - Java

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

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

It will do the trick :)

Solution 5 - Java

Try to declare it in manifest file

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

Solution 6 - Java

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

Solution 7 - Java

Just Add in AndroidManifest.xml

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

Solution 8 - Java

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! ;-)

Solution 9 - Java

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();
}

Solution 10 - Java

Just add this in your manifest.xml file

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

You are all done.

Solution 11 - Java

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)
}

Solution 12 - Java

> 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">

Solution 13 - Java

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

Solution 14 - Java

//to hide the soft keyboard
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);

Solution 15 - Java

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);
}

Solution 16 - Java

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

like this :

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

Solution 17 - Java

Only this solution worked for me on API 26 & Kotlin

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

Solution 18 - Java

or You can use focusable tag in xml.

android:focusable="false"

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFcoderView Question on Stackoverflow
Solution 1 - JavaLucasView Answer on Stackoverflow
Solution 2 - JavaPraveenkumarView Answer on Stackoverflow
Solution 3 - JavadiraView Answer on Stackoverflow
Solution 4 - JavaKing of MassesView Answer on Stackoverflow
Solution 5 - JavaandroidifyView Answer on Stackoverflow
Solution 6 - JavaSaraVFView Answer on Stackoverflow
Solution 7 - JavaDayanand WaghmareView Answer on Stackoverflow
Solution 8 - JavaJack TView Answer on Stackoverflow
Solution 9 - JavaEfe ÖZYERView Answer on Stackoverflow
Solution 10 - JavaAndroidoView Answer on Stackoverflow
Solution 11 - JavaTrevor.ScrewsView Answer on Stackoverflow
Solution 12 - JavaPhilip HerbertView Answer on Stackoverflow
Solution 13 - JavaSomwang SouksavatdView Answer on Stackoverflow
Solution 14 - JavaYuliia AshomokView Answer on Stackoverflow
Solution 15 - Javahaythem souissiView Answer on Stackoverflow
Solution 16 - Javaabdelrahman abiedView Answer on Stackoverflow
Solution 17 - JavanbkView Answer on Stackoverflow
Solution 18 - JavaVishal AgrawalView Answer on Stackoverflow