4

Hello I would like to do the next thing : when I click on an EditText I would like to hide the keyboard but seeing the cursor. I tried to do this :

    editText_test!!.setCursorVisible(false);
    editText_test!!.setFocusableInTouchMode(false);
    editText_test!!.setFocusable(true);

Obviously I don't see the keyboard but I can't click on my EditText. How can I do this ? To be precise I am using Kotlin.

Thank you !

Mohammed Rampurawala
  • 3,033
  • 2
  • 22
  • 32
Guy Martino
  • 41
  • 1
  • 2

4 Answers4

12

If you have minimum API >= 21:

editText_test!!.showSoftInputOnFocus = false

To deal with different versions:

if (Build.VERSION.SDK_INT >= 21) {
    editText_test!!.showSoftInputOnFocus = false
} else if (Build.VERSION.SDK_INT >= 11) {
    editText_test!!.setRawInputType(InputType.TYPE_CLASS_TEXT)
    editText_test!!.setTextIsSelectable(true)
} else {
    editText_test!!.setRawInputType(InputType.TYPE_NULL)
    editText_test!!.isFocusable = true
}
BakaWaii
  • 6,732
  • 4
  • 29
  • 41
0

Set a listener for your EditText's onFocus, you can add:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

How to avoid automatically appear android keyboard when activity start

Felix Guo
  • 2,700
  • 14
  • 20
  • I try this : `editText_test!!.setOnFocusChangeListener(OnFocusChangeListener { view, hasFocus -> getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); })` but it does not work :/ – Guy Martino Aug 07 '17 at 23:56
0

inside the listener method force the android to hide the virtual keyboard using the hideSoftInputFromWindow method in the InputMethodManager

View view = this.getCurrentFocus();
if (view != null) {  
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
Mobicoder
  • 9
  • 1
  • 1
    I tried this : `editText_test!!.setOnFocusChangeListener(OnFocusChangeListener { view, hasFocus -> val view = this.currentFocus if (view != null) { val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager imm.hideSoftInputFromWindow(view.windowToken, 0) } })` but unfortunately it does not work when I click on the EditText I see the keyboard :/ – Guy Martino Aug 08 '17 at 00:09
0

try this in manifest file

:
<activity
...
android:windowSoftInputMode="stateHidden|adjustResize"
...
>
V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
Bapusaheb Shinde
  • 839
  • 2
  • 13
  • 16