0

I am developing one app and I have show custom keyboard, I need to disable the android default keyboard from appearing and also cursor should be display when we click on that edittext.

I have follow below links (selected answers) but they didn't work for android 8.0

that code is worked to hide the keyboard from appearing but didn't show the cursor.

Android Edit Text Cursor Doesn't Appear

Android: Disable soft keyboard at all EditTexts

can any one know the solution.

Jayesh
  • 3,661
  • 10
  • 46
  • 76

1 Answers1

1

I got the solution for above question.

I have implement my custom keyboard (view with numbers) to input the numbers only in EditText.

I have used below function set for every edittext I used in screen.

fun disableSoftInputFromAppearing(editText: CustomEditText) {

    if (Build.VERSION.SDK_INT >= 26) {

        editText.setRawInputType(InputType.TYPE_NULL)
        editText.inputType = InputType.TYPE_CLASS_NUMBER
        editText.showSoftInputOnFocus = false
        editText.setOnClickListener {
            hideKeyboard()
        }

        try {               
            val f = TextView::class.java.getDeclaredField("mCursorDrawableRes")
            f.isAccessible = true
            f.set(editText, R.drawable.cursor)
        } catch (ignored: Exception) {
        }

    } else if (Build.VERSION.SDK_INT >= 24) {
        editText.inputType = InputType.TYPE_CLASS_NUMBER
        editText.showSoftInputOnFocus = false
        editText.setOnClickListener {
            hideKeyboard()
        }
        editText.setRawInputType(InputType.TYPE_CLASS_NUMBER)
        editText.setTextIsSelectable(true)
    } else {
        if (Build.VERSION.SDK_INT >= 21 ) {
            editText.showSoftInputOnFocus = false
        } else if (Build.VERSION.SDK_INT >= 11) {
            editText.setRawInputType(InputType.TYPE_CLASS_TEXT)
            editText.isCursorVisible = true
            editText.setTextIsSelectable(true)
        } else {
            editText.setRawInputType(InputType.TYPE_NULL)
            editText.isFocusable = true
        }

        try {
            val f = TextView::class.java.getDeclaredField("mCursorDrawableRes")
            f.isAccessible = true
            f.set(editText, R.drawable.cursor)
        } catch (ignored: Exception) {
            ignored.printStackTrace()
        }
    }
}

EditText in layout file

<EditText
    android:layout_width="@dimen/_25sdp"
    android:layout_height="@dimen/_25sdp"
    android:cursorVisible="true"
    android:digits="0123456789"
    android:gravity="center"
    android:imeOptions="actionNext"
    android:longClickable="false"
    android:maxLength="1"
    android:singleLine="true"
    android:textColor="#000000"
    android:textSize="@dimen/_16sdp"
    tools:text="0" />
Jayesh
  • 3,661
  • 10
  • 46
  • 76