Detecting backspace on an empty EditText

Detecting backspace on an empty EditText



I'm trying to detect a soft keyboard backspace in an empty EditText view. I've searched Stack Overflow and also Google and ~20 similar questions, but
none have a solution for detecting a backspace in an empty EditText.


EditText


EditText



All of the answers evolve around this blog article https://code.i-harness.com/en/q/4a914a
which specifically notes in the end that this does not detect backspace on an empty EditText view.


EditText



Help is much appreciated.




3 Answers
3



Most reliable solution to detect any event or text change of an EditText is using TextWatcher but if EditText is already empty, you can not detect backspace/delete key event on all devices.



Please keep it in mind that it is not possible and hopefully you or any other will not waste your time.





I mean, I am able to detect backspaces on my device, so it is possible.
– LAD
Sep 2 at 2:10



You can detect if edittext is empty or not with this code:


EditText usernameEditText = (EditText) findViewById(R.id.editUsername);
sUsername = usernameEditText.getText().toString();
if (sUsername.matches(""))
Toast.makeText(this, "You did not enter a username", Toast.LENGTH_SHORT).show();
return;





I don't understand, how does this help my scenario?
– gitterio
Sep 2 at 1:38





Check again please my answer
– user10303165
Sep 2 at 1:44





He isn't looking for if the EditText is empty but rather if a backspace is clicked when it is empty.
– LAD
Sep 2 at 1:46


EditText





It still doesn't solve my problem, I need to detect backspace on an empty EditText view..
– gitterio
Sep 2 at 1:47



There are a couple ways you could detect a backspace click on an EditText.


EditText



This way doesn't work on all devices, but from this post you can do the following, which sets an OnKeyListener for the EditText:


OnKeyListener


EditText


editText.setOnKeyListener(new OnKeyListener()
@Override
public boolean onKey(View v, int keyCode, KeyEvent event)
// You can identify which key was pressed by checking the keyCode value with KeyEvent.KEYCODE_
if (keyCode == KeyEvent.KEYCODE_DEL)
// Do something here

return false;

);



Or you can try the following method, which detects if a backspace has been clicked:


@Override
public boolean onKeyUp(int keyCode, KeyEvent event)
switch (keyCode)
case KeyEvent.KEYCODE_DEL:
// Do something here
return true;
default:
return super.onKeyUp(keyCode, event);






None of the methods work on soft keyboards, as mentioned in the answers
– gitterio
Sep 2 at 1:47





Really, cause it works on mine.
– LAD
Sep 2 at 1:48





It doesn't work on all phones, ie my Samsung and my emulated devices, so it's not a true solution.
– gitterio
Sep 2 at 1:49






Which Samsung are you using? I tested it on the Galaxy S8.
– LAD
Sep 2 at 1:49





I have the S7 edge.
– gitterio
Sep 2 at 1:50



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

Node.js puppeteer - Use values from array in a loop to cycle through pages