0

In my app i want to clear focus on back pressed, show something that was hidden, and hide the keyboard.

Problem : when the Keyboard is up, pressing the back button hides it but it doesn't trigger my onBackPressed event ! Meaning i need to go back twice. First back action : keyboard goes down Second back action : my code get's executed

This is the code that I want to execute when the back button is pressed, regardless of the keyboard visibility

override fun onBackPressed() {
    val materialEditText2 = findViewById<AppCompatEditText>(R.id.material_edit_text_2)
    materialEditText2.visibility = View.VISIBLE
    binding.materialEditText.clearFocus()

}

I tried searching online for way too long, best thing I found is this https://code.luasoftware.com/tutorials/android/edittext-clear-focus-on-keyboard-hidden

I don't understand how to implement it

  • "when the Keyboard is up, pressing the back button hides it but it doesn't trigger my onBackPressed event" -- that is perfectly normal and AFAIK is unavoidable. – CommonsWare Feb 06 '23 at 19:04
  • This is something similar to what you need https://stackoverflow.com/questions/68182271/android-hide-keyboad-and-dismiss-bottomsheetfragment-when-back-pressed/68183349#68183349 – Zain Feb 06 '23 at 19:26

0 Answers0