Clear Edit Text - adb

15.5k Views Asked by At

How to clear focused Edit text using shell command.

I tried

adb shell input keyevent KEYCODE_CLEAR // Not worked 
adb shell input keyevent KEYCODE_DEL // Delete only one char
adb shell input keyevent KEYCODE_FORWARD_DEL // Not worked

With this I am only able to delete upto One character only, Is there any way I can delete/clear the focused Edit text.

5

There are 5 best solutions below

0
aziannomness On

The only way that I have found so far is to get the proper coordinates to use input swipe x y x y duration to simulate a long press. This will then highlight all the text in the EditText field. You can then send the keys you want to replace what was there.

I just wish that adb shell input keyevent KEYCODE_CLEAR would clear all the text in the field. That would make things so much easier, if someone can find a better way that would be great.

1
J. MA On

If you use uiautomator(https://github.com/xiaocong/uiautomator), you could do this by:

  1. tap the EditText widget to get focus, then

  2. use device(focused=True).clear_text() to clear the view, or by device(focused=True).set_text("new text") to set a new text.

6
Pedro Rodrigues On

This works for me:

function clear_input() {
    adb shell input keyevent KEYCODE_MOVE_END
    adb shell input keyevent --longpress $(printf 'KEYCODE_DEL %.0s' {1..250})
}

Then:

clear_input
2
Rasoul Miri On

You can use this way:

adb shell input keyevent --longpress 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67

67 is the keycode of KEYCODE_DEL

4
Sruli Zuckerman On

This will select all and then backspace.

input keycombination 113 29 && input keyevent 67