How to block virtual keyboard
while clicking on edittext in android
5 Answers
Here is a website that will give you what you need.
As a summary, it provides links to InputMethodManager
and View
from Android Developers. It will reference to the getWindowToken
inside of View
and hideSoftInputFromWindow()
for InputMethodManager
.
A better answer is given in the link, hope this helps.
EDIT
From the link posted above, here is an example to consume the onTouch
event:
editText.setOnTouchListener(otl);
private OnTouchListener otl = new OnTouchListener() {
public boolean onTouch (View v, MotionEvent event) {
return true; // the listener has consumed the event
}
};
Here is another example from the same website. This claims to work but seems like a bad idea since your EditBox
is NULL
it will be no longer an editor:
myEditor.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
int inType = myEditor.getInputType(); // backup the input type
myEditor.setInputType(InputType.TYPE_NULL); // disable soft input
myEditor.onTouchEvent(event); // call native handler
myEditor.setInputType(inType); // restore input type
return true; // consume touch event
}
});
Hope this points you in the right direction!

- 982
- 1
- 12
- 16

- 90,123
- 14
- 117
- 115
-
after trying 5+ other suggestions, finally a solution to this that works. – Jeffrey Blattman Dec 20 '11 at 21:52
-
@JohnyTex I haven't tried the suggested solution, but I would see if the answer below, (e.g., http://stackoverflow.com/a/12950947/166712) solves that problem. – Anthony Forloney Aug 09 '16 at 15:00
-
Tried it, but I don't understand that solution. – JohnyTex Aug 09 '16 at 15:14
-
If you are using data binding, add this to your EditText layout: android:onTouch="@{() -> true}" – divonas Jan 31 '17 at 11:49
-
@Thilaw Brics if you pasted your layout I would be happy to help. Probably worth creating a separate question. – divonas Feb 28 '17 at 07:41
-
What I get in fact is that the xml editor does not recognize the attribute you provided, even when I use data binding – Thilaw Fabrice Mar 03 '17 at 11:09
A simpler way, is to set focusable property of EditText
to false
.
In your xml layout:
<EditText
...
android:focusable="false" />

- 982
- 1
- 12
- 16

- 79
- 1
- 2
-
1Thanks! best solution for me as i only want to show the Time/datepicker when a user clicks on the EditText! – merger Nov 15 '13 at 08:33
-
@Symfony Dev sorry but you can still long press on edittext and edit them. Check Anthony answer as it's a correct solution. – fex Oct 12 '15 at 13:39
Another simpler way is adding android:focusableInTouchMode="false"
line to your EditText
's xml. Hope this helps.

- 1,285
- 17
- 23
For cursor positioning you can use Selection.setSelection(...)
, i just tried this and it worked:
final EditText editText = (EditText) findViewById(R.id.edittext);
editText.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
//change the text here
Selection.setSelection(editText.getText(), editText.length());
return true;
}
});

- 1,651
- 11
- 10
-
I don't understand what you mean with change text here. Would you mind explaining further? – JohnyTex Aug 09 '16 at 15:07
-
I have a hard time seeing this putting the cursor at any other position than at the end, but I don't know...? – JohnyTex Aug 09 '16 at 15:32
The best way to do this is by setting the flag textIsSelectable
in EditText to true. This will hide the SoftKeyboard permanently for the EditText but also will provide the added bonus of retaining the cursor and you'll be able to select/copy/cut/paste.
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

- 1
- 1

- 6,733
- 9
- 31
- 60