Reputation: 8487
How to block virtual keyboard
while clicking on edittext in android
Upvotes: 17
Views: 26675
Reputation: 91816
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.
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!
Upvotes: 46
Reputation: 79
A simpler way, is to set focusable property of EditText
to false
.
In your xml layout:
<EditText
...
android:focusable="false" />
Upvotes: 7
Reputation: 6779
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
Upvotes: 0
Reputation: 1315
Another simpler way is adding android:focusableInTouchMode="false"
line to your EditText
's xml. Hope this helps.
Upvotes: 3
Reputation: 1651
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;
}
});
Upvotes: 0