24

I am working on a dialog at Android with a few EditTexts. I've put this line at the onCreate() in order to disable the soft keyboard:

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

The problem is that it works only when the dialog appear and doing nothing. When I move to the next EditText, the keyboard appears and not going down.

Does anybody have an idea how to solve this issue?

2
  • why would you want this?
    – Macarse
    Apr 27, 2011 at 11:24
  • 11
    I have keypad of my own that I want to use, I don't want the soft keyboard go up every time I click an EditText
    – Yaniv
    Apr 28, 2011 at 5:43

13 Answers 13

35

If you take look on onCheckIsTextEditor() method implementation (in TextView), it looks like this:

@Override
public boolean onCheckIsTextEditor() {
    return mInputType != EditorInfo.TYPE_NULL;
}

This means you don't have to subclass, you can just:

((EditText) findViewById(R.id.editText1)).setInputType(InputType.TYPE_NULL); 

I tried setting android:inputType="none" in layout xml but it didn't work for me, so I did it programmatically.

3
  • when I use this only the first line of text is visible :( . not all data is shown.
    – newday
    Nov 23, 2012 at 6:39
  • in some devices, programmatically set null not work.
    – landry
    Feb 20, 2014 at 7:05
  • 3
    This will prevent any touch interactions with the EditText (i.e. long-press and select). That might not be desired effect many developers want... Apr 1, 2014 at 17:33
26

create your own class that extends EditText and override the onCheckIsTextEditor():

public class NoImeEditText extends EditText {
    public NoImeEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    @Override
    public boolean onCheckIsTextEditor() {
        return false;
    }
}
11
  • It prevents me from running findViewById(). I get class cast exception.
    – Yaniv
    Apr 27, 2011 at 11:44
  • 1
    I forgot to mention, I don't want to disable the EditText, I have keypad of my own that I wrote and want to use it.
    – Yaniv
    Apr 27, 2011 at 11:45
  • you will need to define the edit text as a NoImeEditText in the xml (which is your custom edittext), then NoImeEditText et=(NoImeEditText) findViewById(R.id.itsId) should work. you could handle onFocusChange and if et.hasFocus() show your keypad.
    – jkhouw1
    Apr 27, 2011 at 11:50
  • I tried it now and I get another excpetion. I think the XML can't find the new EditText. Exception: 04-27 14:59:39.590: ERROR/AndroidRuntime(3006): Caused by: java.lang.ClassNotFoundException: android.view.NoImeEditText in loader dalvik.system.PathClassLoader[/data/app/com.keypadTest-2.apk]
    – Yaniv
    Apr 27, 2011 at 12:03
  • in the layout xml use a fully qualified reference to your custom control: <com.yourpackge.path.NoImeEdit android:id="@+id/custControl"...
    – jkhouw1
    Apr 27, 2011 at 13:18
22

Try this out..

edittext.setInputType(InputType.TYPE_NULL);      
if (android.os.Build.VERSION.SDK_INT >= 11)   
{  
    edittext.setRawInputType(InputType.TYPE_CLASS_TEXT);  
    edittext.setTextIsSelectable(true);  
}
4
  • 1
    Great! Text still has cursor and selectable, but what's with api 9? Jun 19, 2013 at 19:39
  • Hi Aleksey..Thanks....!!..'edittext.setInputType(InputType.TYPE_NULL);' worked perfectly for APi 8....but when i tested in API14, it didnt work.....thats why i added the If condition......
    – ASP
    Jun 20, 2013 at 6:32
  • 1
    Thanks, was searching for a way to make edittext contents selectable after making it not-editable.
    – yajnesh
    Feb 4, 2014 at 13:55
  • 1
    Works perfectly in combination with: editText.setInputType(EditorInfo.TYPE_NULL); Apr 1, 2014 at 17:51
5

I have been looking for solutions to this all day, and I came across this approach. I'm putting it here because it seems to answer this question perfectly.

EditText et = ... // your EditText
et.setKeyListener(null) //makes the EditText non-editable so, it acts like a TextView.

No need to subclass. The main difference between this and making your EditText non-focusable, is that the EditText still has its own cursor - you can select text, etc. All it does is suppress the IME from popping up its own soft keyboard.

1
  • I wanted to disable the EditText but still have the setOnClickListener working. This worked good in that case.
    – Adil Malik
    Feb 5, 2013 at 21:41
5

Call TextView.setShowSoftInputOnFocus(false). This method is documented since API level 21, but it's already there since API level 16 (it's just hidden from JavaDoc). I use it with API level 16 in an AlertDialog in combination with dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN).

In API level 14, there is a hidden method setSoftInputShownOnFocus() which seems to have the same purpose, but I have not tested that.

The advantage over InputType.TYPE_NULL is, that all the normal input types can be used (e.g. for password input) and the touch event positions the cursor at the correct spot within the text.

4

Its been some time since this post, but here is a simple method which worked for me: in the xml, in the EditText properties, do: android:focusable="false". Now the keyboard will not popup even if the user clicks on it. This is useful if you are providing your own keypad.

0
3

Put this in Oncreate Method

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

Know its too late, but have you tried the following setting to EditText in your layout ?

android:inputType="none"

UPDATE

Use,

editText.setInputType(InputType.TYPE_NULL)
editText.setFocusable(false)
5
  • No, it doesn't work. The keyboard still goes up with this property. Did it work for you?
    – Yaniv
    Mar 14, 2013 at 6:10
  • My bad. It doesn't work! Was using a tab emulator and have use physical keyboard enabled. Let me experiment a bit and update or remove this comment. Thanks for quick response.
    – VenoM
    Mar 15, 2013 at 7:01
  • Ohk, Tried a few stuff! The below answer worked. i.e., manually settings editText#setInputType to 0 or InputType.TYPE_NULL Is this approach bad since no one had given any positive recommendations ?? @Yaniv: Once you have confirmed u read my comments, I will delete this post as it won't work. Thanks!
    – VenoM
    Mar 15, 2013 at 7:41
  • editText.setInputType(InputType.TYPE_NULL) in conjunction with editText.setFocusable(false) worked for me [API: 19]
    – VenoM
    Jun 15, 2014 at 19:09
  • @VenoM It's Superrbb!!
    – GreenROBO
    Sep 30, 2015 at 10:30
0
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edSearch.getWindowToken(), 0);
4
  • I've tried this but I don't have the method 'getSystemService()' when I extends from Dialog.
    – Yaniv
    Apr 27, 2011 at 11:32
  • u can pass activity or applicationcontext in ur constructor
    – Jazz
    Apr 27, 2011 at 11:34
  • 1
    It didn't help, the soft keyboard still appear at the moment I move to the next EditText.
    – Yaniv
    Apr 27, 2011 at 11:42
  • 1
    modify ur manifest, <activity android:configChanges="keyboardHidden" android:name=".sampleactivity"> </activity>
    – Jazz
    Apr 27, 2011 at 11:46
0

in order to disable ANDROID SOFT INPUT KEYBOARD xml file doesn't help in my case calling the setInputType method on EditText object in java file works great. here is the code.

EditTextInputObj = (EditText) findViewById(R.id.EditTextInput);
EditTextInputObj.setInputType(InputType.TYPE_NULL); 
1
  • Please don't use so many periods like that. Just one will do fine. Dec 7, 2012 at 10:34
0

If you put the textViews in the view group you can make make the view get the focus before any of its descendants by using this:

view.setDescendantFocusability(view.FOCUS_BLOCK_DESCENDANTS);
0

by set EditText focusable->false, keyboard will not opened when clicked

<EditText
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:focusable="false" />
2
  • Whilst this code may answer the question, it is better to include a description or extra information on what it does. May 18, 2015 at 11:37
  • Tried this, but I can't re-enable it even though I set it programmatically to true Oct 20, 2015 at 7:17
0

You can do that:

textView.setOnClickListener(null);

It will disable the keyboard to the textView and the click will not work anymore.

Your Answer

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

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