I cannot figure how to implement onKeyPreIme(int keyCode, KeyEvent event)
in a Fragment
.
@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK &&
event.getAction() == KeyEvent.ACTION_UP) {
// do your stuff
return false;
}
return super.dispatchKeyEvent(event);
}
I tried a lot but nothing works. Also, I could not find anything on Google or Stack Overflow. I would like to perform an action when the back key is pressed and the softkeyboard is up. Setting an onKeyListener
on my EditText
s did not work, since KeyEvent.KEYCODE_BACK
is not called when the soft keyboard is up. I appreciate any help and source code.
this is a more full code of answer by Deminetix.
i have used the answer by Deminetix to filter handheld barcode reader on android and have the result.
to make it usable on a screen only with buttons i have added a EditText with android:textColor="#FF000000" android:background="#00FFFFFF" android:enabled="false" disabled EditText still gets keyboard events.
optionally i could hide the software keyboard using the following but after disabling the EditText it was not required.
MainActivity.java:
activity_main.xml:
ListenerEditText.java:
This is my solution and it works really well for me, but everyones needs are different.
First i subclassed EditText and hooked up a listener (Google should make this the default)
Then you can attach a listener from anywhere like so:
I was able to implement onKeyPreIme by sub-classing my EditText views that were related to the keyboard input. The goal is to make a custom lock screen that the user must enter a pass code or leave the application. When the user taps the "keyboard down" button the keyboard does not disappear.
Make sure to create a separate .java file for the subclassed EditText. Additionally, be sure to use the constructor in the code below (must pass AttrubuteSet).
I realize that my implementation of onKeyPreIme may not match yours, however it does demonstrate how to intercept the keyboard events before the InputMethodManager does it's thing.
I hope this helps.
Screenshot UserLockActivity
EditText Subclass
UserLockActivity.java
activity_user_lock.xml Layout file
I was not able to figure out how to implement the onKeyPreIME, but I was able to perform an action after the keyboard disappeared with the following code:
You man need to change comparison heightDiff > 200. This comparison worked for me because I had a scrollview.