For this to work, you would need to subclass EditText
, preferbly AppCompatEditText.
When back key is pressed (hide the keyboard), execute code to clear focus.
class ClearFocusEditText: AppCompatEditText { constructor(context: Context) : super(context) constructor(context: Context, attrs: AttributeSet) : super(context, attrs) constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) override fun onKeyPreIme(keyCode: Int, event: KeyEvent?): Boolean { if(keyCode == KeyEvent.KEYCODE_BACK) { clearFocus() } return super.onKeyPreIme(keyCode, event) }}
<com.mydomain.myapp.view.ClearFocusEditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
This is useful for scenario where android:windowSoftInputMode="adjustPan"
doesn't work properly the second time (after keyboard hidden from the first time).