4

How do I prevent my keyboard to open when I'm selecting text from edit text? I want the text inside to be still selectable, while not opening the keyboard when I click it

 <EditText
        android:id="@+id/displayalllinks"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:ems="10" >
azizbekian
  • 53,978
  • 11
  • 145
  • 225
user352806
  • 133
  • 1
  • 9
  • 3
    If you do not want the soft keyboard to appear, why are you using an `EditText`? Why not use a `TextView` with `android:textIsSelectable="true"`? – CommonsWare Mar 25 '17 at 13:52
  • Why don't you want it to pop up? If you want it to go away after a button is clicked or something, try this [link](http://stackoverflow.com/questions/1109022/close-hide-the-android-soft-keyboard?rq=1) – Appafly Mar 25 '17 at 13:53
  • Your question is not clear, you should have either provided more details (e.g. not having to type in the View just selecting an already set text) or not using `EditText` at all. – users at 4325010 Mar 25 '17 at 18:42
  • My reason would be a calculator app that does not need the soft-keyboard but a custom layout with buttons. The reason for an EditText is because it can display a cursor so you can move it around and add digits between instead of removing all of the input first – Denny Jan 03 '18 at 19:31

3 Answers3

2
editText = (EditText) findViewById(R.id.editText_1);
boolean disabled = false;

//We listen for selection
editText.setOnTouchListener(new View.OnTouchListener() {
      @Override
      public boolean onTouch(View v, MotionEvent event) {

        if (MotionEvent.ACTION_UP == event.getAction()) {

          //When selected, we disable input.
          if (editText.hasSelection()) {
              editText.setInputType(InputType.TYPE_NULL);
              disabled = true;
          } else 
              //Then we restore it, if previously unset.
              if (disabled) {  
                  editText.setInputType(TYPE_CLASS_TEXT);
                  disabled = false;
              }  

        return false;
      }
});

Hope this helps!

users at 4325010
  • 3,596
  • 4
  • 28
  • 46
1

You can achieve this by adding inputType of none and textIsSelectable:

 <EditText
        android:id="@+id/displayalllinks"
        android:inputType="none"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:ems="10" />  

And

 EditText editText = (EditText)findViewById(R.id.edit_mine);
    editText.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            v.onTouchEvent(event);
            InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm != null  && editText.hasSelection()){
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
            }                
            return true;
        }
    });

Joel
  • 798
  • 4
  • 12
0

It's a hacky behaviour, because as a user if you are clicking on an EditText you are expecting something to do with that text.

I've come up with this solution: extending AppCompatEditText and overriding onTouchEvent() behaviour.

public class MyEditText extends AppCompatEditText {
    public MyEditText(Context context) {
        super(context);
    }

    public MyEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        final int currentInputType = getInputType();
        setInputType(InputType.TYPE_NULL);
        boolean b = super.onTouchEvent(event);
        setInputType(currentInputType);
        return b;
    }
}

This is the result:

Clicking on EditText doesn't open soft keyboard

In this gif you see, that it doesn't break long click functionality.

azizbekian
  • 53,978
  • 11
  • 145
  • 225