Prevent the keyboard from displaying on activity start
Asked Answered
B

18

289

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?

Bookbinding answered 16/3, 2012 at 6:12 Comment(5)
in your manifest <activity android:windowSoftInputMode="stateHidden" ...>Satiny
possible duplicate of Android on-screen keyboard auto popping upLaky
How to use together with android:windowSoftInputMode="adjustPan"?Multiangular
@János android:windowSoftInputMode="adjustPan|stateHidden"Philana
the answer in this comment, was the answer i'm looking for: https://mcmap.net/q/102262/-hiding-the-android-keyboard-for-edittextKalil
R
449

I think the following may work

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

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

Rothko answered 16/3, 2012 at 6:15 Comment(5)
Is there a way to set it to be a digits-only keypad? i.e. 12Key keyboard?Vicarage
@MohamedKhamis input.setRawInputType(Configuration.KEYBOARD_12KEY);Anh
Yes it still works. @SagarNayak why would you want to hide keyboard for EditText?:) This is to hide keyboard when activity starts which contains EditTextGraham
@Devealte 7 years later and it worked for me, did you place it in onCreate?Washy
@Washy Yes, and I just fixed it several months ago :)Melanesian
I
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.

Ious answered 16/3, 2012 at 6:18 Comment(0)
E
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">
Ethben answered 16/3, 2012 at 7:41 Comment(2)
like this global approach.Featly
This worked as i was using different themes at various placesTarp
S
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 :)

Skyros answered 24/12, 2015 at 6:19 Comment(4)
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?Flyover
In addition to my answer, you need to check the other properties as well which you have in manifest and for edit text.Skyros
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.Flyover
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.Flyover
C
14

Try to declare it in manifest file

<activity
    android:name=".HomeActivity"
    android:label="@string/app_name"
    android:windowSoftInputMode="stateAlwaysHidden" >
Crab answered 4/4, 2014 at 12:6 Comment(0)
T
14

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

Tipper answered 20/3, 2015 at 12:10 Comment(0)
D
11

Just Add in AndroidManifest.xml

<activity android:name=".HomeActivity"  android:windowSoftInputMode="stateHidden">
</activity>
Dingess answered 6/1, 2016 at 14:48 Comment(0)
B
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! ;-)

Brighten answered 17/4, 2018 at 13:57 Comment(0)
L
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();
}
Lancashire answered 5/8, 2016 at 7:59 Comment(0)
I
7

Just add this in your manifest.xml file

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

You are all done.

Ingold answered 9/8, 2018 at 10:1 Comment(0)
D
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)
}
Doable answered 16/3, 2012 at 6:12 Comment(0)
A
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">
Ailurophobe answered 22/12, 2015 at 4:32 Comment(0)
H
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

Hiro answered 26/11, 2018 at 11:41 Comment(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 ReviewEmee
I
1
//to hide the soft keyboard
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
Inmesh answered 30/9, 2015 at 18:22 Comment(0)
G
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);
}
Garonne answered 20/4, 2018 at 15:39 Comment(0)
L
0

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

like this :

<activity android:name=".MainActivity"
  android:windowSoftInputMode="stateAlwaysHidden">
Literal answered 10/9, 2020 at 16:17 Comment(0)
Z
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()
}
Zing answered 17/2, 2022 at 13:7 Comment(0)
B
-1

or You can use focusable tag in xml.

android:focusable="false"

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

Busk answered 3/9, 2020 at 17:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.