58

I have an Activity with some EditText fields and some buttons as a convenience for what normally would be used to populate those fields. However when we the user touches one of the EditText fields the Android soft keyboard automatically appears. I want it to remain hidden by default, unless the user long presses the menu button. I have search for a solution to this and found several answers, but so far I can't get them to work.

I have tried the following:

1 - In the onCreate method,

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

2 - Also in the onCreate method,

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);

3 - and fIn the Manifest file,

<activity android:name=".activityName" android:windowSoftInputMode="stateAlwaysHidden"/>

None of these methods work. Whenever the user clicks on the EditText field, the soft keyboard appears. I only want the soft keyboard to appear if the user explicitly shows it by long pressing the menu key.

Why isn't this working?

ScubaSteve
  • 593
  • 1
  • 4
  • 6
  • Try to use my answer. Maybe it help [from here](https://stackoverflow.com/a/46423101/5595925) – ch13mob Sep 26 '17 at 09:53
  • You may need to achieve an edit text behavior in Android TV were you need to make a non-editable edit text but clickable, this may help in the context - https://stackoverflow.com/a/70285647/4694013 – Anoop M Maddasseri Dec 09 '21 at 06:40

16 Answers16

101

This will help you

editText.setInputType(InputType.TYPE_NULL);

Edit:

To show soft keyboard, you have to write following code in long key press event of menu button

editText.setInputType(InputType.TYPE_CLASS_TEXT);
            editText.requestFocus();
            InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            mgr.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
Sandeep Kumar P K
  • 7,412
  • 6
  • 36
  • 40
  • This does hide the soft keyboard from appearing, but it would be nice if the user could force the soft keyboard to appear by long pressing the menu button. – ScubaSteve Jan 25 '12 at 16:02
  • Thanks for the help. The editText field actually needs to be masked because it's a password. I didn't realize (although it makes perfect sense) that this shows the input as it is typed. – ScubaSteve Feb 01 '12 at 18:04
  • I need it to mask the input. Setting the input type to null of course removes the masking. – ScubaSteve Feb 10 '12 at 17:47
  • According to your question I think you got the answer so it would be nice if you accept this as answer so that it can be helpful for others as well. Now you have problem regarding masking. You can ask that as another question. Do you mean masking edittext field is setting edittext as password field? If so you can do that either using `android:password="true"` in your xml layout or `editText.setTransformationMethod(PasswordTransformationMethod.getInstance());` in your activity. – Sandeep Kumar P K Feb 11 '12 at 04:28
  • Where do you actually place these lines of code, in the onCreate etc..@Sandy? – N MC May 18 '16 at 01:35
39

You need to add the following attribute for the Activity in your AndroidManifest.xml.

<activity
    ...
    android:windowSoftInputMode="stateHidden|adjustResize"
    ...
/>
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
Emran Hamza
  • 3,829
  • 1
  • 24
  • 20
9

After long time looking into TextView class I found a way to prevent keyboard to appears. The trick is hide it right after it appears, so I searched a method that is called after keyboard appear and hide it.

Implemented EditText class

public class NoImeEditText extends EditText {

    public NoImeEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * This method is called before keyboard appears when text is selected.
     * So just hide the keyboard
     * @return
     */
    @Override
    public boolean onCheckIsTextEditor() {
        hideKeyboard();

        return super.onCheckIsTextEditor();
    }

    /**
     * This methdod is called when text selection is changed, so hide keyboard to prevent it to appear
     * @param selStart
     * @param selEnd
     */
    @Override
    protected void onSelectionChanged(int selStart, int selEnd) {
        super.onSelectionChanged(selStart, selEnd);

        hideKeyboard();
    }

    private void hideKeyboard(){
        InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(getWindowToken(), 0);
    }
}

and style

<com.my.app.CustomViews.NoImeEditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:editable="false"
    android:background="@null"
    android:textSize="@dimen/cell_text" />
cristianomad
  • 303
  • 2
  • 9
8

My test result:

with setInputType:

editText.setInputType(InputType.TYPE_NULL);

the soft keyboard disappears, but the cursor will also disappear.

with setShowSoftInputOnFocus:

editText.setShowSoftInputOnFocus(false)

It works as expected.

Edric
  • 24,639
  • 13
  • 81
  • 91
David Guo
  • 1,749
  • 3
  • 20
  • 30
8

I sometimes use a bit of a trick to do just that. I put an invisible focus holder somewhere on the top of the layout. It would be e.g. like this

 <EditText android:id="@id/editInvisibleFocusHolder"
          style="@style/InvisibleFocusHolder"/>

with this style

<style name="InvisibleFocusHolder">
    <item name="android:layout_width">0dp</item>
    <item name="android:layout_height">0dp</item>
    <item name="android:focusable">true</item>
    <item name="android:focusableInTouchMode">true</item>
    <item name="android:inputType">none</item>
</style>

and then in onResume I would call

    editInvisibleFocusHolder.setInputType(InputType.TYPE_NULL);
    editInvisibleFocusHolder.requestFocus();

That works nicely for me from 1.6 up to 4.x

Manfred Moser
  • 29,539
  • 13
  • 92
  • 123
  • This kind of answers a different question - how to hide the soft keyboard on activity launch (OP wants soft keyboard hidden on EditText focus event until long press). However, this answer is a great reference for the more common use case that might be what some users get here via search for, so +1. – Rich Apr 15 '13 at 16:13
  • You don't even need this view to be an `EditText`. It can just be a regular `View` with `focusable=true` and `focusableInTouchMode=true`. – ashughes Jul 04 '13 at 16:31
6

The soft keyboard kept rising even though I set EditorInfo.TYPE_NULL to the view. None of the answers worked for me, except the idea I got from nik431's answer:

editText.setCursorVisible(false);
editText.setFocusableInTouchMode(false);
editText.setFocusable(false);
Alex Burdusel
  • 3,015
  • 5
  • 38
  • 49
5

The following line is exactly what is being looked for. This method has been included with API 21, therefore it works for API 21 and above.

edittext.setShowSoftInputOnFocus(false);
Shnkc
  • 2,108
  • 1
  • 27
  • 35
  • this works. simplest solution for dealing with Custom Keyboard. eg. Custom lockscreen with custom number keypad – pravingaikwad07 Oct 13 '20 at 12:42
  • This should be the accepted answer as it's an ideal solution to prevent the soft keyboard from showing up when the EditText gains focus, while still allowing the user to select and modify the text inside the EditText. – Harshal Pudale Feb 20 '23 at 11:00
4

There seems to be quite a variety of ways of preventing the system keyboard from appearing, both programmatically and in xml. However, this is the way that has worked for me while supporting pre API 11 devices.

// prevent system keyboard from appearing
if (android.os.Build.VERSION.SDK_INT >= 11) {
    editText.setRawInputType(InputType.TYPE_CLASS_TEXT);
    editText.setTextIsSelectable(true);
} else {
    editText.setRawInputType(InputType.TYPE_NULL);
    editText.setFocusable(true);
}
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
1

Three ways based on the same simple instruction:

a). Results as easy as locate (1):

android:focusableInTouchMode="true"

among the configuration of any precedent element in the layout, example:

if your whole layout is composed of:

<ImageView>

<EditTextView>

<EditTextView>

<EditTextView>

then you can write the (1) among ImageView parameters and this will grab android's attention to the ImageView instead of the EditText.

b). In case you have another precedent element than an ImageView you may need to add (2) to (1) as:

android:focusable="true"

c). you can also simply create an empty element at the top of your view elements:

<LinearLayout
  android:focusable="true"
  android:focusableInTouchMode="true"
  android:layout_width="0px"
  android:layout_height="0px" />

This alternative until this point results as the simplest of all I've seen. Hope it helps...

Andrew
  • 63
  • 6
1

Simply Use EditText.setFocusable(false); in activity

or use in xml

android:focusable="false"
Alexan
  • 8,165
  • 14
  • 74
  • 101
Nur Gazi
  • 47
  • 2
1

Simply use below method

private fun hideKeyboard(activity: Activity, editText: EditText) {
    editText.clearFocus()
    (activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager).hideSoftInputFromWindow(editText.windowToken, 0)
}
Praveen
  • 388
  • 3
  • 7
1

Let's try to set the below properties in your xml for EditText

android:focusableInTouchMode="true" android:cursorVisible="false".

if you want to hide the softkeypad at launching activity please go through this link

Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
Nikhil C George
  • 1,207
  • 1
  • 10
  • 14
0
weekText = (EditText) layout.findViewById(R.id.weekEditText);
weekText.setInputType(InputType.TYPE_NULL);
amartynov
  • 4,125
  • 2
  • 31
  • 35
0

Hide the keyboard

editText.setInputType(InputType.TYPE_NULL);

Show Keyboard

etData.setInputType(InputType.TYPE_CLASS_TEXT);
etData.setFocusableInTouchMode(true);

in the parent layout

android:focusable="false"
Woz
  • 350
  • 7
  • 13
0
   public class NonKeyboardEditText extends AppCompatEditText {

    public NonKeyboardEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onCheckIsTextEditor() {
        return false;
    }
}

and add

NonKeyboardEditText.setTextIsSelectable(true);
  • 1
    Hi, Please consider adding some additional information to introduce your snippet, for instance explaining how it solves the issue and why – Matthias Beaupère Apr 24 '19 at 09:33
-1

I also faced the same problem, I fixed that via this method,

   editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    // do something..
            }
            
                closeKeyborad();
                return true;
            }
            return false;
        }
    });

Call that function before return true.

private void closeKeyborad() {
    View view = this.getCurrentFocus();
    if (view != null){
        InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(view.getWindowToken() , 0);
    }
}
Ganesh MB
  • 1,109
  • 2
  • 14
  • 27