1

Hi im trying to add on click listener to editText so i can disable the softkeyboard when user clicks on edittext using this code below, how to do that?

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edittext.getWindowToken(), 0);
Tarek Zoubi
  • 53
  • 1
  • 9

3 Answers3

8

First it needs to be focusable...

<EditText
    ...
    android:inputType="none"
    android:focusable="false"
    ... />

You have to implement it in your code and than just add this to get an click listener...

myEditText.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // hide the keyboard
        // show own keyboard or buttons
    }
});
ahdgfd
  • 325
  • 3
  • 12
  • Change this in your ans above: `android:inputType="none"` and `android:focusable="true"` – Shaishav Aug 01 '16 at 08:52
  • Thanks! Worked for me perfectly. I tried using Text View to look like Edittext. Used a line below TextView, added as a background drawable but it was complex solution. This one is simple. – VJ Vishal Jogiya Aug 28 '17 at 13:54
0

try it and set OnClickListener

  <androidx.appcompat.widget.AppCompatEditText
            android:id="@+id/edt"
            android:inputType="none"
            android:focusable="false"
            android:editable="false"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
Farshid roohi
  • 722
  • 9
  • 23
0

The easiest and straight forward way you can set it is by editing the xml file as follows:

android:onClick="onClickMyEditText"

and define the same method in Activity class:

public void onClickMyEditText(View view) {
        //your code here
    }
Daniel
  • 1