Brian
Brian

Reputation: 313

How to get the hide keyboard event

I want to know how to get the hide event from my SoftKeyboard which is displayed when I click on my EditText (the first button on the left):

enter image description here

<EditText
        android:id="@+id/txtEdit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="text|textAutoComplete"
        android:windowSoftInputMode="stateHidden|adjustResize"/>

Because I don't want to first hide the keyboard then after the keyboard is hidden tap the back button to go back. I want to immediatly go back without hidding first.

enter image description here

I know how to get the back event from my activity :

@Override
public void onBackPressed() {
    // my code
}

But with this Override I can't get the hide event, only the back event. Does someone know how to get that event ?

Upvotes: 2

Views: 1894

Answers (3)

konrad_sx
konrad_sx

Reputation: 485

Much simpler: override onKeyPreIme on the EditText and watch for keyCode == KEYCODE_BACK, in Kotlin:

class ET(context: Context) : EditText(context) {
   override fun onKeyPreIme(keyCode: Int, event: KeyEvent): Boolean {
      if (keyCode == KEYCODE_BACK && event.action == ACTION_UP){
         // do something
      }
      return false
   }
}

Upvotes: 0

Mete
Mete

Reputation: 2884

In this code, i listening to editText. If user close the keyboard, onBackPressed() running.

 var isFirst: Boolean = true

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    editText.getViewTreeObserver().addOnGlobalLayoutListener(ViewTreeObserver.OnGlobalLayoutListener {
        if (isKeyboardOpen()) {
            if (isFirst) {
                 isFirst = false
            }
        } else {
            if (!isFirst) {
                onBackPressed()
            }
        }
    })

}

private fun isKeyboardOpen(): Boolean {
    val r = Rect()
    editText.getWindowVisibleDisplayFrame(r)
    val screenHeight = editText.getRootView().getHeight()
    // r.bottom is the position above soft keypad or device button.
    // if keypad is shown, the r.bottom is smaller than that before.
    val keypadHeight = screenHeight - r.bottom
    // Log.d("TAG", "keypadHeight = $keypadHeight")

    if (keypadHeight > screenHeight * 0.15) { // 0.15 ratio is perhaps enough to determine keypad height.
        // keyboard is opened
        Log.d("TAG", "keypad is open")
    } else {
        // keyboard is closed
        Log.d("TAG", "keypad is close")
    }
    return (keypadHeight > screenHeight * 0.15)
}

override fun onBackPressed() {
    // my code
    Log.d("TAG", "keypad is onBackPressed")
    finish()
}

Upvotes: 2

TanzeemAlam
TanzeemAlam

Reputation: 5

The keyboard appears as you click on any filed which takes user inputs. That keyboard is by Default one, provided by the Android's O.S. To manage keyboards on your own, you can use a bottom sheet. And for that, you have to make a custom keyboard and take every input manually. After that, you can perform the onbackpressed method, to directly go back.

Upvotes: 0

Related Questions