24

I've come about as far as this which gets me halfway there, but not quite. I have a dialer Fragment that has all the usual Buttons to enter a number including backspace, so I don't need the soft keyboard. I'd also like to give the user the ability to paste text (long click... works fine per default), as well as to edit what has been entered so I need the cursor.

The easiest way I found to make sure the soft keyboard doesn't pop up if the user clicks inside the EditText is to set the inputType to null - but that kills the cursor as well.

So, how do I declare my EditText and what kind of commands should I launch to have my EditText field never ever show the soft keyboard no matter what the user attempts, but still retain paste functionality and the cursor?

I've also tried android:windowSoftInputMode="stateAlwaysHidden" in my manifest, but to no avail.

Community
  • 1
  • 1
Stephan Steiner
  • 729
  • 1
  • 5
  • 17
  • Did you check this ? http://stackoverflow.com/questions/1109022/close-hide-the-android-soft-keyboard – sdabet Nov 27 '12 at 14:27
  • Yup - most of the methods. And for the rest you'll find another thread right here where somebody reports that the keyboard will still appear if the user clicks inside the EditText box. I suppose you could use the focus listener and hide the keyboard again, but the user will still see the keyboard appear quickly before it is sent away. I'm looking for a way to tell the OS to buzz off with the keyboard forever in this particular fragment. – Stephan Steiner Nov 27 '12 at 17:01

14 Answers14

40

This worked for me:

        // Update the EditText so it won't popup Android's own keyboard, since I have my own.
    EditText editText = (EditText)findViewById(R.id.edit_mine);
    editText.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            v.onTouchEvent(event);
            InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm != null) {
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
            }                
            return true;
        }
    });
Eddie Sullivan
  • 765
  • 5
  • 8
7

I have finally found a (for me) working solution to this.

First part (in onCreate):

// Set to TYPE_NULL on all Android API versions
mText.setInputType(InputType.TYPE_NULL);
// for later than GB only
if (android.os.Build.VERSION.SDK_INT >= 11) {
    // this fakes the TextView (which actually handles cursor drawing)
    // into drawing the cursor even though you've disabled soft input
    // with TYPE_NULL
    mText.setRawInputType(InputType.TYPE_CLASS_TEXT);
}

In addition, android:textIsSelectable needs to be set to true (or set in onCreate) and the EditText must not be focused on initialization. If your EditText is the first focusable View (which it was in my case), you can work around this by putting this just above it:

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

You can see the results of this in the Grapher application, free and available in Google Play.

opticron
  • 89
  • 3
4

Setting the flag textIsSelectable to true disables the soft keyboard.

You can set it in your xml layout like this:

<EditText
    android:id="@+id/editText"
    ...
    android:textIsSelectable="true"/>

Or programmatically, like this:

EditText editText = (EditText) findViewById(R.id.editText);
editText.setTextIsSelectable(true);

The cursor will still be present, you'll be able to select/copy/cut/paste but the soft keyboard will never show.

Andrei Lupsa
  • 2,496
  • 3
  • 22
  • 18
4

Best solution from @Lupsaa here:

Setting the flag textIsSelectable to true disables the soft keyboard.

You can set it in your xml layout like this:

<EditText
android:id="@+id/editText"
...
android:textIsSelectable="true"/>

Or programmatically, like this:

EditText editText = (EditText) findViewById(R.id.editText);

editText.setTextIsSelectable(true);

The cursor will still be present, you'll be able to select/copy/cut/paste but the soft keyboard will never show.

Community
  • 1
  • 1
Santacrab
  • 3,035
  • 2
  • 23
  • 30
3

use

android:windowSoftInputMode="stateHidden" 

in your manifest file instead of android:windowSoftInputMode="stateAlwaysHidden"

kleopatra
  • 49,346
  • 26
  • 88
  • 189
Droid Diva
  • 81
  • 8
3

This is what I did. First, in manifest inside activity

android:windowSoftInputMode="stateAlwaysHidden|adjustNothing"

Second, in onCreate if inside activity or onActivityCreated if inside fragment

editText.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            hideSoftKeyboard(v);
        }
    });

Do not forget to request focus to the editText

editText.requestFocus();

Then add the hideSoftKeyboard(v) method same as the other answer.

private void hideSoftKeyboard(View v){
    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
}

The key here is to requestFocus before clicking the EditText. If without focus, first click will make the keyboard show up(my experience). However, this is applied if you have a single EditText in an activity. With this, you still can type with custom keyboard(if any), can copy and paste, and cursor is still visible.

Tuss
  • 865
  • 1
  • 8
  • 12
3

The exact functionality that you require is provided by setting the flag textIsSelectable in EditText to true. With this, the cursor will still be present, and you'll be able to select/copy/cut/paste, but SoftKeyboard will never show. Requires API 11 and above.

You can set it in your xml layout like this:

<EditText
    android:textIsSelectable="true"
    ...
/>

Or programmatically, like this:

EditText editText = (EditText) findViewById(R.id.editText);
editText.setTextIsSelectable(true);

For anyone using API 10 and below, hack is provided here : https://stackoverflow.com/a/20173020/7550472

Community
  • 1
  • 1
Kaushik NP
  • 6,188
  • 8
  • 29
  • 57
2

This works perfectly (for me) in 2 steps:

  1. <activity... android:windowSoftInputMode="stateHidden"> in manifest file

  2. Add these properties in your editText XML code

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

You have to put both 1 and 2, only then it will work.

Cheers

famousgarkin
  • 12,308
  • 5
  • 52
  • 71
2
    EditText text = (EditText) findViewById(R.id.text);
    if (Build.VERSION.SDK_INT >= 11) {
        text.setRawInputType(InputType.TYPE_CLASS_TEXT);
        text.setTextIsSelectable(true);
    } else {
        text.setRawInputType(InputType.TYPE_NULL);
        text.setFocusable(true);
    }
aikrikunov95
  • 187
  • 2
  • 12
2

If your min SDK is 21, you can this method from java code:

editText.setShowSoftInputOnFocus(false);

Credits to Chen Su article.

Alex Misiulia
  • 963
  • 10
  • 14
1

First add android:windowSoftInputMode="stateHidden" in your manifest file, under the activity. like this

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

The on your xml add this android:textIsSelectable="true" . This will make the pointer visible.

Then on onCreate method of the activity, add this:

EditText editText = (EditText)findViewById(R.id.edit_text);
edit_text.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        v.onTouchEvent(event);
        InputMethodManager inputMethod = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputMethod!= null) {
            inputMethod.hideSoftInputFromWindow(v.getWindowToken(), 0);
        }                
        return true;
    }
});
Jerin A Mathews
  • 8,097
  • 3
  • 22
  • 41
0

EditText editText = (EditText)findViewById(R.id.edit_mine); editText.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        v.onTouchEvent(event);
        InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
        }                
        return true;
    }
});

ha... this is the correct way of doing...this job done... this gonna work !

Ajay Jain
  • 21
  • 5
0

You can use the following line of code in the activity's onCreate method to make sure the keyboard only pops up when a user clicks or touch into an EditText Field. I tried lots of methods and codes from stackoverflow but didnt work any but this Works Perfectly for me!! Try this.. :)`

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

You can use the following line of code in the activity's onCreate method to make sure the keyboard only pops up when a user clicks or touch into an EditText Field. I tried lots of methods and codes from stackoverflow but didnt work any but this Works Perfectly for me!! Try this.. :)`

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