23

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?

Yaniv
  • 3,321
  • 8
  • 30
  • 45

13 Answers13

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.

zeratul021
  • 2,644
  • 3
  • 32
  • 45
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;
    }
}
Charuක
  • 12,953
  • 5
  • 50
  • 88
jkhouw1
  • 7,320
  • 3
  • 32
  • 24
  • It prevents me from running findViewById(). I get class cast exception. – Yaniv Apr 27 '11 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 '11 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 '11 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 '11 at 12:03
  • in the layout xml use a fully qualified reference to your custom control: – jkhouw1 Apr 27 '11 at 13:18
  • It worked, Thanks! But Now I can't see the cursor. Is there a way to bring it back? – Yaniv Apr 27 '11 at 13:26
  • in the same place you make your keypad appear, you could try yourCustomEditText.setCursorVisible(true) (and accept the answer if it works :) ) – jkhouw1 Apr 27 '11 at 13:59
  • Interesting - I just created a test project with one custom control, and the cursor appears everytime i give that custom EditText focus. (without needing to do the setCursorVisible()) – jkhouw1 Apr 27 '11 at 14:43
  • I don't know, I have EditTexts without a cursor and I move between them by touch or with requestFocus(), but still have no cursor. – Yaniv Apr 27 '11 at 14:49
  • Has cursor problem in some devices. – Bob Sep 08 '12 at 07:36
  • Constructor name is wrong in your code (or class name). It should be NoImeEditText. – Lucas Lima Jun 05 '15 at 13:43
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);  
}
ASP
  • 3,645
  • 1
  • 31
  • 45
  • 1
    Great! Text still has cursor and selectable, but what's with api 9? – Oleksii Malovanyi Jun 19 '13 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 '13 at 06:32
  • 1
    Thanks, was searching for a way to make edittext contents selectable after making it not-editable. – yajnesh Feb 04 '14 at 13:55
  • 1
    Works perfectly in combination with: editText.setInputType(EditorInfo.TYPE_NULL); – IgorGanapolsky Apr 01 '14 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.

43matthew
  • 962
  • 8
  • 14
  • I wanted to disable the EditText but still have the setOnClickListener working. This worked good in that case. – Adil Malik Feb 05 '13 at 21:41
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.

Aswin Kumar
  • 5,158
  • 5
  • 34
  • 39
4

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.

Christian d'Heureuse
  • 5,090
  • 1
  • 32
  • 28
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)
Lal
  • 14,726
  • 4
  • 45
  • 70
VenoM
  • 364
  • 3
  • 10
  • No, it doesn't work. The keyboard still goes up with this property. Did it work for you? – Yaniv Mar 14 '13 at 06: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 '13 at 07: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 '13 at 07:41
  • `editText.setInputType(InputType.TYPE_NULL)` in conjunction with `editText.setFocusable(false)` worked for me [API: 19] – VenoM Jun 15 '14 at 19:09
  • @VenoM It's Superrbb!! – GreenROBO Sep 30 '15 at 10:30
2

Put this in Oncreate Method

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Rollyng
  • 1,387
  • 2
  • 12
  • 18
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); 
Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
Khurram Shehzad
  • 1,010
  • 12
  • 16
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);
Tafari
  • 2,639
  • 4
  • 20
  • 28
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" />
itzhar
  • 12,743
  • 6
  • 56
  • 63
0

You can do that:

textView.setOnClickListener(null);

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

z_inx
  • 345
  • 1
  • 3
  • 11
0
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edSearch.getWindowToken(), 0);