adb can be used for various operations on views, touches, key-strokes, etc...
Can it also check or change the focus of views?
For example, if there are a lot of views on the current activity, and I'd like to scroll on a listView, I could choose the listView to be focused by providing its id, and it will get a focus so that I could emulate page-down key. Alternatively, I could emulate tab-key till I see that the currently focused view is a listView.
You can figure out what view is focused by dumping the view hierarchy and scanning the output:
# dump and extract
adb shell uiautomator dump && adb pull /sdcard/window_dump.xml
# prettify if you want
xmllint --format window_dump.xml > window_dump.pretty.xml
# then grep
cat window_dump.pretty.xml | grep focused=\"true\"
<node index="2" text="" resource-id="YOUR_PACKAGE:id/resize_button" class="android.widget.FrameLayout" package="YOUR_PACKAGE" content-desc="Enter dominant view" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="true" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[180,60][252,132]">
If anyone is looking for a way to scroll through the views by pressing TAB, the following should do it
adb shell input keyevent KEYCODE_TAB
If someone arrives here looking for a way to change focus on different activities (not views) via ADB
We can get the current focused window/app using
adb shell dumpsys window windows | grep -E "mCurrentFocus|mFocusedApp"
In order to set focus, we need a 2 step process 1) get the task ID to focus 2) focus on it
# get the task ID, it is the number after # in the output
adb shell dumpsys activity recents | grep "#"
# set focus
adb shell am task focus <task ID>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With