2

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.

android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • never used it, but if `ACTION_CLICK` performs "click" then try `AccessibilityNodeInfo#ACTION_SCROLL_FORWARD` or in general `ACTION_FOCUS` – pskink Nov 24 '16 at 09:01
  • Can you please provide an example? Given an id of a view, how would I set it to work with what you wrote? – android developer Nov 24 '16 at 17:45
  • i already gave you a service that "clicks" on any view using `ACTION_CLICK` just change the action to some other – pskink Nov 24 '16 at 17:57
  • I asked about adb, but what exactly is your solution? Please show in an answer and not a comment – android developer Nov 24 '16 at 21:48
  • http://stackoverflow.com/questions/39315028/is-it-possible-to-use-adb-commands-to-click-on-a-view-by-finding-its-id#comment66085074_39315028 – pskink Nov 24 '16 at 21:56
  • @pskink This is about focusing on a view. Not clicking it. As an example, as I've written, if I have a listView, I'd like to know how to focus on it, and scroll in it using page-down key. Besides, what you've shown there doesn't have a solution in any of the answers, using what you wrote here. – android developer Nov 25 '16 at 17:06
  • so change the ACTION, dont use ACTION_CLICK but one i posted in me first comment here – pskink Nov 25 '16 at 17:16
  • Again, I asked about adb. Not a service. But it could be interesting too, so if you wish to answer using a service, please write as an answer. – android developer Nov 25 '16 at 20:24
  • so you will use adb, `adb shell ...` – pskink Nov 25 '16 at 20:42
  • Yes, and the "..." - what would it be? – android developer Nov 26 '16 at 17:43
  • either "am startservice ..." or "dumpsys activity service ..." – pskink Nov 26 '16 at 17:47
  • What service? I told you. I don't use a service, but if you have a solution with a service, please write it down, because it might be useful – android developer Nov 26 '16 at 20:07
  • then read this comment again http://stackoverflow.com/questions/39315028/is-it-possible-to-use-adb-commands-to-click-on-a-view-by-finding-its-id#comment66085074_39315028 and ^F `can be done by this service` – pskink Nov 26 '16 at 20:19
  • @pskink Again, this question is about adb command, but if you have another possible solution, please write it down as an answer, and not as a comment. – android developer Nov 27 '16 at 07:41
  • again, `adb` is used, when you execute `adb shell am startservice ...` in ytour opinion is `adb` command used or not? – pskink Nov 27 '16 at 07:43
  • @pskink if "..." is the service I need to create, then it's not adb alone. Plus, you didn't show how to use it, especially not here. – android developer Nov 27 '16 at 19:38
  • because i dont want to repeat the same thing twice, you dont want a custom `AccessibilityService`? its ok, wait for anyone else answer (i dont think you will receive any response though...) – pskink Nov 27 '16 at 19:47
  • @pskink You never did write an answer on the thread you've linked to, so it's not twice. – android developer Nov 28 '16 at 06:21
  • you have a full source code of a working service, just integrate it with your app, what more do you expect? – pskink Nov 28 '16 at 06:48
  • @pskink I don't see any answer of a full source code of a working service. Not here and not there. – android developer Nov 28 '16 at 21:55
  • because there is no answer, but you have full source code of a working service which you have to add to your app and modify your manifest file – pskink Nov 28 '16 at 21:59
  • If there is no answer, what did you write all this time about? I don't have any source code of a service. – android developer Nov 29 '16 at 20:19
  • i posted the link to a comment which points to a service code, i posted it twice here, did you even try to open it? am i to post it the 3rd time? – pskink Nov 29 '16 at 20:23
  • You wrote there is no answer, which is correct: Not here, and not there. There is no full code of an answer. Not even a part of it. – android developer Nov 29 '16 at 22:40
  • so you did not find a link i posted here twice? – pskink Dec 01 '16 at 08:31
  • Since I already wrote about it, it should be known that I did see it, and as we both wrote, it's not an answer because it's not written in an answer and since it doesn't include the solution to the current question. – android developer Dec 01 '16 at 14:36
  • that link points to a comment to one of another question, right? and this comment contains a link to a **full** source code of a **working** service which you are supposed to add to your app – pskink Dec 01 '16 at 14:40
  • Again, I asked about adb command, without any service. I wrote you that if you have another solution without it, please post it here as an answer. The link you've provided doesn't contain any answer to the current question (and it's not even an answer to the question I've posted there), and I don't know how to use it. Please, if you do have a solution, write about it. – android developer Dec 01 '16 at 15:11
  • so did you see that service code or not? then simply change `ACTION_CLICK` to `ACTION_SCROLL_FORWARD` or `ACTION_FOCUS`, it is so simple that i will not post the modified code if it requires replacing ONE constant with another, OK in case if you say there is no full sources code then this is the code i'm talking about: http://pastebin.com/kH01PqYs – pskink Dec 01 '16 at 15:39
  • Which of those returns me the id of the focused view? – android developer Dec 02 '16 at 00:18

2 Answers2

3

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]">
droid256
  • 404
  • 5
  • 15
2

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>
S Raghav
  • 1,386
  • 1
  • 16
  • 26