Search code examples
androidkotlinandroid-edittext

EditText stops being focusable after changing and then reseting attributes


In my project I have Edittexts disgused as textviews that the user can only edit after enabling an edit mode. I have set up 2 extention functions for this

private fun EditText.disguiseAsTextview() {
    this.isCursorVisible = false
    this.isLongClickable = false
    this.isClickable = false
    this.isFocusable = false
    this.isSelected = false
    this.setBackgroundResource(android.R.color.transparent)
}

private fun EditText.breakDisguise() {
    this.isCursorVisible = true
    this.isLongClickable = true
    this.isClickable = true
    this.isFocusable = true
    this.background = editTextDrawableBackground
}

at first the EditText works as expected, but after disguising and undisguising, the EditText can no longer be focused on, and blinks blue, as if it were selected for a split second. dont know why this happens, as a set everything back to its default state after calling breakDisguise.


Solution

  • On your breakDisguise method,

    Replace

    this.isFocusable = true
    

    with

    this.isFocusableInTouchMode = true