1

i would like to know if its possible to detect whenever a user presses the back button when in the EditText field. I've tried using the KeyEvent.BACK and the KeyEvent.CANCELED flags in my setOnKeyListener like so:

textView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN || keyCode == KeyEvent.FLAG_CANCELED || keyCode == KeyEvent.BACK) {
                    Log.e("TextView", String.valueOf(thiz.getText()));
                }
                return false;
            }
        }); 

but this didn't work. If somebody could help me i would appreciate that :)

Siju
  • 2,585
  • 4
  • 29
  • 53
Arc Mods
  • 21
  • 5
  • If you mean that the user performs system BACK navigation (e.g., via a BACK button in a navigation bar at the bottom of the screen), that closes the soft keyboard and AFAIK is not passed along to apps. – CommonsWare Jul 23 '21 at 16:44
  • I would like to log the text of the edittext whenever an user closes the keyboard – Arc Mods Jul 23 '21 at 16:46
  • I dont know if i made it clearer of what im trying to do – Arc Mods Jul 23 '21 at 16:51
  • "I would like to log the text of the edittext whenever an user closes the keyboard" -- what if the user does not close the keyboard? For example, the user could be using a physical keyboard, whether [part of the device](https://www.kickstarter.com/projects/jellyphone/titan-pocket-the-smallest-qwerty-android-11-smartphone) or an external keyboard (USB, Bluetooth). – CommonsWare Jul 23 '21 at 17:10
  • Instead of checking whether the soft keyboard is hidden or not you can check if the EditText is focused or not. Because keyboard can be opened for other views as well. – salmanwahed Jul 23 '21 at 18:17

2 Answers2

1

As per my understanding if you want to do something("I would like to log the text of the edittext whenever an user closes the keyboard") when keyboard is hidden, you can use ViewTreeObserver like this example

Zain
  • 37,492
  • 7
  • 60
  • 84
0

As I understand that you want, for some reason, to log text from EditText when the user closes the virtual keyboard. If yes, check this question, possibly this is what you searching for. Especially you may take look at this answer

AShX
  • 358
  • 3
  • 14