Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent keyboard from popping up on EditText click?

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" >
like image 437
user352806 Avatar asked Mar 25 '17 13:03

user352806


1 Answers

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!

like image 92
Dimitar Avatar answered Nov 15 '22 07:11

Dimitar