17

How to block virtual keyboard while clicking on edittext in android

Nikhil
  • 16,194
  • 20
  • 64
  • 81
deepthi
  • 8,477
  • 12
  • 35
  • 36

5 Answers5

46

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!

SerjantArbuz
  • 982
  • 1
  • 12
  • 16
Anthony Forloney
  • 90,123
  • 14
  • 117
  • 115
7

A simpler way, is to set focusable property of EditText to false.

In your xml layout:

<EditText
    ...
    android:focusable="false" />
SerjantArbuz
  • 982
  • 1
  • 12
  • 16
Symfony Dev
  • 79
  • 1
  • 2
  • 1
    Thanks! 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
3

Another simpler way is adding android:focusableInTouchMode="false" line to your EditText's xml. Hope this helps.

ACengiz
  • 1,285
  • 17
  • 23
0

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;
     }
});
GeekYouUp
  • 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
0

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

Community
  • 1
  • 1
Kaushik NP
  • 6,733
  • 9
  • 31
  • 60