9

Suppose I have an app (in debug/release build , made by me or not), which has an ID for a specific view.

Is it possible to call adb command to click on this view?

I know it's possible to make it click on a specific coordinate, but is it possible to use the ID instead ?

I ask this because I do know that the "Layout Inspector" tool (available via Android Studio) and the "View hierarchy" tool (available via "Android Device Monitor", previously used via DDMS) can show the ids of the views (and even their coordinates and bounding box), so maybe it can be a better way to simulate touches when performing some automatic tests.

I can use a rooted method if needed.


EDIT: I've set a bounty in case there is an easier/better way than what I've written in my own answer, which was to parse the result of "adb shell dumpsys activity top" .

I would like to know if it's possible to get the views coordinates (and sizes of course) that are shown on the screen, including as much information about them (to identify each). This should be possible via the device too. Maybe something that has the same output data of what's available from the "monitor" tool :

enter image description here

Notice how it can get the basic information of the views, including the text, the id, and the bounds of each.

As I've read, this might be possible via AccessibilityService, but sadly I can't understand how it all works, what are its capabilities, how to trigger it, what are its requirements, etc...

android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • Possible duplicate of [Is there a way to get current activity's layout and views via adb?](http://stackoverflow.com/questions/26586685/is-there-a-way-to-get-current-activitys-layout-and-views-via-adb) – Alex P. Sep 04 '16 at 09:02
  • AFAIK, No, to create a click/touch event you have to use the coordinates. – Muhammed Refaat Sep 04 '16 at 09:22
  • but just out of curiosity: why dont you use [this](https://developer.android.com/training/testing/ui-testing/espresso-testing.html) ? – pskink Sep 04 '16 at 13:20
  • @pskink I meant on any app, even one that I don't have its source code. How do I use uiautomator to do it ? Can you please show an example? Suppose someone made an app that has the id "myButton" to some button, how do I find the view and press on it? I could of course use this information in code (Java app on the PC or on Android device itself) . – android developer Sep 04 '16 at 16:20
  • no, you can only do that with your own app – pskink Sep 04 '16 at 16:22
  • @pskink What if I have root? – android developer Sep 04 '16 at 23:28
  • `"so maybe it can be a better way to simulate touches when performing some automatic tests"` you want to test not your own apps? – pskink Sep 05 '16 at 05:50
  • @pskink I want to have a generic way to use it on all apps. – android developer Sep 05 '16 at 07:02
  • 1
    so analyze the output of `adb shell dumpsys activity top`, but this is not a generic way as the format of that output may change in the future – pskink Sep 05 '16 at 12:35
  • @pskink Nice. Thank you. This can be very useful. Has it ever changed? – android developer Sep 05 '16 at 16:30
  • 1
    i have no idea... but even though it can change any moment from now... – pskink Sep 05 '16 at 16:38
  • @pskink Where did you find about this command? Are there any options for it? Why didn't you write it in an answer, so that I can tick it? – android developer Sep 05 '16 at 17:54
  • try `adb shell dumpsys activity -h` and `adb shell dumpsys -l` to get other services (there is ~100 of them or so) – pskink Sep 05 '16 at 17:59
  • The right answer was given to the linked *duplicate* question – Alex P. Sep 05 '16 at 20:30
  • @AlexP. No, it wasn't. The answer there doesn't show any example of how to use it. Here, at least calling this command will give a clue about which view has which id. – android developer Sep 05 '16 at 22:19
  • if you need an example of how to parse an xml file - just google for it – Alex P. Sep 05 '16 at 22:30
  • @AlexP. I didn't talk about parsing. I talked about the command itself. – android developer Sep 05 '16 at 22:34
  • @pskink OK, I've devised a solution based on what you wrote, and it seems to work fine. If you know how to make the result work well even if the status bar is shown, please let me know. Currently I had to get a workaround for this. – android developer Sep 05 '16 at 22:58
  • 1
    android developer: @AlexP. is right, use `uiautomator dump` command (i was not aware of `dump` subcommand) – pskink Sep 06 '16 at 06:45
  • @pskink I don't see this tool. Only similar file is "uiautomatorviewer.bat" . Trying to use via adb also doesn't work (using "adb shell uiautomator dump" ) . It says "adb server version (31) doesn't match this client (36);" – android developer Sep 06 '16 at 06:46
  • does `adb shell` work? – pskink Sep 06 '16 at 06:47
  • @pskink You mean first write "adb shell" and then use this command (because I just wrote that together it doesn't)? If so, I tried it now, and it only showed a single line "Killed" . Nothing to parse. Tested on SGS4 with Android 5.0.1 – android developer Sep 06 '16 at 07:32
  • But why the downvote of what I wrote? It works fine. – android developer Sep 06 '16 at 07:34
  • 1
    no, it does not work, as it doesnt add every parents bounds, while `uiautomator dump` shows accumulated position, do you really need it for 3rd party apps? – pskink Sep 06 '16 at 07:38
  • @pskink I don't understand. From what I see, it shows really a lot, including all IDS of views that I can see. Can you show an example of when it doesn't work? – android developer Sep 06 '16 at 07:42
  • see [here](http://pastebin.com/3yrZVRvM), do you really need it for 3rd party apps? – pskink Sep 06 '16 at 07:47
  • @pskink Please show a sample project, or layout xml file. I will try to see it. For now, it works fine for me on each app that I test. It's much better than "uiautomator " currently, as this really doesn't work at all. – android developer Sep 06 '16 at 07:57
  • see [this](http://pastebin.com/3yrZVRvM) pseudo layout, do you really need it for 3rd party apps? – pskink Sep 06 '16 at 08:01
  • @pskink It's the same thing you provided before. – android developer Sep 06 '16 at 08:08
  • 2
    come on, do you expect me to write the complete project for you? – pskink Sep 06 '16 at 08:23
  • @pskink Not really need a full project, but what you wrote doesn't show enough details for me to understand the scenario. Show a layout XML if you can't do it or show which app you've tested it on . I want to see how you got this issue. I can't reproduce it by what you wrote, and I don't understand what you mean by "will be wrong as it doesnt include height of verticallinearlayout1" . – android developer Sep 06 '16 at 08:38
  • see http://pasteboard.co/hA349pb4g.png red and blue boxes are vertical linear layouts, green and yellow are buttons, your code for green buttons will work, for yellow will NOT – pskink Sep 06 '16 at 08:59
  • if you still dont get it, [here](http://pastebin.com/qmBfe92r) is the layout xml – pskink Sep 06 '16 at 09:22
  • @pskink The image doesn't show me much, but looking at the result, I see that when you write "include" you mean it doesn't have the correct coordinate, as it's based on the parent: for both the first buttons of each layout, the coordinates are the same "30,30-990,174" . Maybe the parsing itself should consider the parent, if that's the case. Maybe it's not the command's fault. In any case, using "uiautomator " is still not possible. If you succeed using it, please show an example of how to use it in an answer. – android developer Sep 06 '16 at 10:07
  • 1
    `Maybe the parsing itself should consider the parent` not maybe: as i said before you have to offset your target view bounds by every parent's position, up to the root of view hierarchy, with `uiautomator` you dont have to do anything as it has the absolute "bounds" (not relative to the parent) – pskink Sep 06 '16 at 10:15
  • @pskink In other words, the coordinates are not absolute . They are relative to the parents. I will need to change the code, but still, no need for a downvote because of this. It's just a mistake and can be fixed. About "uiautomator ", since I can't work with it, it's not an option. Can you please show how you succeeded using it? – android developer Sep 06 '16 at 10:17
  • well you should ask about the downvote to someone who downvoted your answer, not me, as for uiautomator i just run `uiautomator dump`, nothing else – pskink Sep 06 '16 at 11:04
  • @pskink I think I've found the tool (it's outside of the folder of adb, it's in "android-sdk\platforms\android-24" for example, and it's a jar file), but I can't find out how to run it from ADB. I wanted to be able to run it via a rooted device too and not just from the PC. That's why I asked (even in the title) for an adb command. – android developer Sep 06 '16 at 11:47
  • @pskink BTW, calling "uiautomator.jar dump" in the folder I've found it (android-sdk\platforms\android-24 ) doesn't show anything at all. Tested on Nexus 5 with Android 6.0.1 . – android developer Sep 06 '16 at 11:53
  • i still dont understand what is the reason of doing that with apps which are not yours (you have no source code for them, only binaries/apk), with your app the generic solution is as easy as ~15 lines of code – pskink Sep 06 '16 at 11:57
  • @pskink The use is to perform batch automation operations, without the knowledge of where views are. If you wish to see more info and see it works, I will post about it some day on XDA, once I finish all the minimal things I wish to add to it. – android developer Sep 06 '16 at 12:19
  • i asked about 3rd party apps, but seems you ignored my question 4th time – pskink Sep 06 '16 at 12:31
  • 1
    @AlexP. yep, it seems i should stop... – pskink Sep 06 '16 at 15:27
  • I wrote inside the question "made by me or not" . I also wrote in the comments before " I meant on any app, even one that I don't have its source code" ... This means all apps. – android developer Sep 06 '16 at 17:14
  • @pskink OK, I've fixed my code. Now it works and returns the correct absoulute coordinates. – android developer Sep 07 '16 at 05:24
  • doesn't work, simply crashes and i have no time to analyze why – pskink Sep 07 '16 at 06:39
  • @pskink What do you mean? It works fine for me. On which sample have you tried it? on the XML you shown? – android developer Sep 07 '16 at 08:46
  • @pskink I've tested it on your layout, having "it_will_not_work_0" as the parameter, and it found it, returning the rect : Rect(52, 848 - 1028, 974) , while for "it_will_work_0" it returned this rect: Rect(52, 262 - 1028, 388) . – android developer Sep 07 '16 at 16:21
  • i deleted the layout which not worked as it is really not a way to follow: all you did in your answer below can be done by [this](http://pastebin.com/kH01PqYs) service which not only finds the view by name but also clicks on it – pskink Sep 07 '16 at 20:34
  • @pskink Please write as an answer, so that I can check it out. Also please explain your answer. How does it work ? What does it use? Any docs about it? – android developer Sep 07 '16 at 22:45
  • you need to run `adb shell dumpsys` so that it in turn calls `MyAccessibilityService#dump` with a list of parameters with your ids of the views to be pressed, docs are [here](https://developer.android.com/reference/android/accessibilityservice/AccessibilityService.html), also this code accepts your package ids, but `findAccessibilityNodeInfosByViewId` works also with `android:id/identifier` scheme – pskink Sep 08 '16 at 06:05
  • @pskink I'm not familiar with this. Does it require a relatively new API? Does it require a permission (or need confirmation from the user) ? Does it work outside of my app? How does it work? – android developer Sep 08 '16 at 06:39
  • i sent you documentation about AccessibilityService, did you read it? – pskink Sep 08 '16 at 16:18
  • and if you want to use any IDs (not only app's own IDs but for example android:id/some_id) replace `String id = ...;` with `String id = arg.contains(":id/") ? arg : root.getPackageName() + ":id/" + arg;` (tested on home launcher app and couple of other std android apps) – pskink Sep 08 '16 at 18:25
  • @pskink I still don't get where on the docs you got this, and how to really use it. Please do make a sample. – android developer Sep 08 '16 at 21:03
  • see: https://developer.android.com/reference/android/app/Service.html#dump(java.io.FileDescriptor,%20java.io.PrintWriter,%20java.lang.String[]) – pskink Sep 08 '16 at 21:40
  • "Print the Service's state into the given stream" ? What does it mean? – android developer Sep 10 '16 at 08:12
  • it means that whatever you "print" with `PrintWriter writer` it will be shown on the console where you are running `dumpsys` command – pskink Sep 10 '16 at 08:22
  • I am becoming more and more confused, the more I read from what you provide. Does it require root? What are the requirements for it? Does it provide more than what I wrote in my answer ? Is it somehow easier to use? From which API is it supported? – android developer Sep 10 '16 at 12:57
  • why dont you just use it? i gave you COMPLETE java source code, all you need is to integrate it in your manifest.xml and run `dumpsys` command – pskink Sep 10 '16 at 13:56
  • @pskink Use what? I can't find source code. Please post as new answer. – android developer Sep 10 '16 at 22:42
  • find the sentence: `"all you did in your answer below can be done by this service"` in this thread, note word **"this"** – pskink Sep 11 '16 at 05:47
  • @pskink This is only a part of the code. Not complete. I can't find information about what it does, what it can and can't do (can it get more than just ID but even text content, for example, like on Google's tool?) , what are the requirements, what to do to enable it, ... – android developer Sep 11 '16 at 06:56
  • this is a COMPLETE java source code, all you need is to integrate it in your manifest.xml, is it **really** so hard to read `"Accessibility developer guide"` and `android.view.accessibility.AccessibilityNodeInfo` API? BTW [this](http://androidxref.com/7.0.0_r1/xref/frameworks/base/cmds/uiautomator/library/core-src/com/android/uiautomator/core/AccessibilityNodeInfoDumper.java) is a class that makes `uiautomator dump` stuff – pskink Sep 11 '16 at 07:02
  • @pskink Sorry but I still don't understand. How from the code can I see how it works, when it gets triggered, what are its requirements, what capabilities it has (compared to what I wrote) ... ? How can I even know what to put in the manifest? I already tried to read the docs. I don't get what it means. English isn't my main language, so sometimes things can be hard for me. – android developer Sep 11 '16 at 18:44
  • even if english is not your main language you can ask someone to translate some difficult parts or use [this](https://translate.google.com/#en/iw/https%3A%2F%2Fdeveloper.android.com%2Fguide%2Ftopics%2Fui%2Faccessibility%2Findex.html) – pskink Sep 12 '16 at 05:52
  • @pskink See updated question. – android developer Sep 12 '16 at 06:52
  • did you see [this](http://androidxref.com/7.0.0_r1/xref/frameworks/base/cmds/uiautomator/library/core-src/com/android/uiautomator/core/AccessibilityNodeInfoDumper.java) link i already posted? see `dumpNodeRec` method and compare it with the image you added to your question – pskink Sep 12 '16 at 06:59
  • @pskink Yes, I saw. It still doesn't answer the questions I've asked, and I still can't find a way of how to use it. – android developer Sep 13 '16 at 06:43
  • and did you notice where the data is taken from inside `dumpNodeRec` method? it is `AccessibilityNodeInfo` class that holds all the data you need, it also gives you a way to iterate over child nodes (`getChildCount` / `getChild`) so that you can visit the whole view tree for a given root node – pskink Sep 13 '16 at 07:46
  • @pskink Still I can't find the answers to the questions I asked, including how to use your solution. Please write an answer. I will grant the bounty if it really works and when I fully understand it. – android developer Sep 13 '16 at 13:16
  • 3
    i already told you everything you need to do but it seems you dont want to do anything on your own: what did you try in order to add my service in your manifest? what is the problem you are facing? – pskink Sep 13 '16 at 13:21
  • When I add the service, what makes it get triggered to capture the content of the screen, the view's coordinates etc? What do I need to do? Are there any permissions or confirmations (because it does seem like a bit of privacy issue)? – android developer Sep 13 '16 at 22:45
  • as you can see everything is done in `Service#dump(FileDescriptor fd, PrintWriter writer, String[] args)` method, so what you need is to make it invoked with your parameters holding the view ids you want to "click" – pskink Sep 14 '16 at 06:42
  • @pskink Please show a sample of how to do it in an answer. – android developer Sep 14 '16 at 08:50
  • it is described in `Service#dump` method documentation – pskink Sep 14 '16 at 18:25
  • @pskink What is described? I can't find the answers to the questions I asked. Please write in an answer – android developer Sep 15 '16 at 05:17
  • I've found this sample of using AccessibilityService in order to get the front app : https://github.com/seguri/GetForegroundActivity . Sadly, I still can't find how to get the views that appear there. – android developer Sep 15 '16 at 23:12
  • http://www.pcadvisor.co.uk/how-to/google-android/how-use-androids-accessibility-service-3341094/ – pskink Sep 16 '16 at 08:50
  • @pskink I mean in code. Not as a user. In code, how do I get the views info... Look at the code I wrote. It works just as it is, nothing more is needed. You call the function, and it returns you the rectangle. How do I get this functionality in the service without using what I wrote - that's what I'm trying to figure out. – android developer Sep 16 '16 at 18:51
  • the github project you linked to says: "Requires to activate accessibility service." so i posted a link how to do that – pskink Sep 16 '16 at 18:56
  • @pskink Yes I know the sample works. I asked about how to get the views info, not the activity. – android developer Sep 17 '16 at 12:46
  • ooops the source i posted was to "click" on given view, [this](http://pastebin.com/J4jScVeJ) one shows view info – pskink Sep 18 '16 at 16:54
  • What's the trigger to this? Do I use "adb shell dumpsys activity service MyAccessibilityService" ? Is there another way, that will return the result as a function? How do I use it? What are the requirements and restrictions to it? Does it require root or a connected PC ? – android developer Sep 18 '16 at 18:17
  • no, there is no other way than `adb shell dumpsys activity service ...`, *Service#dump* documentation explains when this method is invoked: `Print the Service's state into the given stream. This gets invoked if you run "adb shell dumpsys activity service "` – pskink Sep 18 '16 at 19:36
  • @pskink So there is no such a way for non-rooted devices. But it does provide more information and in a bit easier way to use, without parsing, no? Can you please explain about it? Also, please write it in an answer. – android developer Sep 19 '16 at 07:12
  • of course you can call `adb shell dumpsys activity service ...` on non-rooted devices, you dont need a root for that – pskink Sep 19 '16 at 07:18
  • I meant without a PC being connected (using this service solution). Also, as I've originally asked, how do I get all the views' information like the SDK-tool does, without the use of Android code? After all, when I use what I've shown on the screenshot, it doesn't install any app to work as a mediator with the PC. it does it all by itself, getting not just the ids of views and their coordinates, but also the text they have for TextViews, and probably other useful information too. – android developer Sep 19 '16 at 23:12
  • i gave you a [service](http://pastebin.com/J4jScVeJ) that shows `other useful information too`, all you need is to run adb command if you are on your PC: `adb shell dumpsys activity service ...`, or `dumpsys activity service ...`if you are on your device, do you know what `adb shell` command is for? – pskink Sep 20 '16 at 05:05
  • @pskink You've provided me code without answering the questions I asked. Also, this code only works if I add an Android service. The code I've provided works even outside of Android. I asked about how to do both, as the tool of the SDK does. I asked about using "adb" command as this is what I know about that's available on both PC and Android itself. – android developer Sep 20 '16 at 07:53
  • did `GetForegroundActivity` service work even if it was not part of your app? if so, do the same with the service i gave you, [here](https://docs.google.com/uc?export=download&id=0B_WcN_UEpuxmb2lKM3Z0eHJLVFk) is a minimal apk implementing this – pskink Sep 21 '16 at 18:45
  • @pskink GetForegroundActivity service doesn't require root. Does what you've provided require it (or a PC connected) , or does it have a more official way? Something with accessibility or any other type of permission? – android developer Sep 24 '16 at 20:34
  • did you try it? does it complain if there is no rooted device? – pskink Sep 24 '16 at 20:37
  • so does it work on non-root devices? i just run it on emulators, not real devices – pskink Sep 26 '16 at 11:47
  • I tried to just run the command, but it didn't work as it said "dump" permission is needed. I think it doesn't seem to be granted easily... Maybe more work is needed. When calling it with "su" before, the command "dumpsys activity" worked, so I guess your service would work on this case. Question is, do you know of a way without root ? – android developer Sep 26 '16 at 15:45
  • it doesnt require root: i just installed it on 4.4 kitkat real device, what exact error are you getting when calling `adb shell dumpsys activity service DumpService`? does it happen on emulator also? – pskink Sep 26 '16 at 16:53
  • BTW if `adb shell dumpsys activity top` works just fine why `adb shell dumpsys activity service DumpService` wouldn't work? – pskink Sep 26 '16 at 21:07
  • @pskink Are you sure you've called this command on the device itself, and not via PC ? I got this error for both of those commands: **"Permission Denial: can't dump ActivityManager from from pid=27808, uid=10973 without permission android.permission.DUMP"** . Both require root to work. Only via a PC it doesn't need root. – android developer Sep 27 '16 at 08:20
  • no i called it via `adb shell`, this is what you want as the title of your question says: `Is it possible to use adb commands... ` – pskink Sep 27 '16 at 08:59
  • @pskink adb commands are available on device too, and I wrote I also wish to do it via device too. That's why at first I didn't consider the uiautomator tool to be a good solution: I couldn't find even how to make it work on PC. Now that I have succeeded using it, it is a nice solution, but I don't want it to write into a file. – android developer Sep 27 '16 at 09:20
  • and because you said: `"I can use a rooted method if needed."` whats the problem with running `dumpsys` tool directly on your phone (not using `adb shell`) ? – pskink Sep 27 '16 at 11:37
  • @pskink It is always preferable to rely on official API than using root/PC . Also, seeing that you've shown accessibility solution, I thought this could mean it is possible without root, to help people with eyes issues, for example. I know a lot of apps out there that have special abilities by using accessibility features. I've also added a bounty even though I have found a working solution (and marked it), because I prefer a more official way, plus you guys said it's possible to even get other information of views (like the text of them), so I added this to the question too. – android developer Sep 27 '16 at 13:14

1 Answers1

5

Using what @pskink explained in the comments above, here's how I achieved this:

First, I ran this command:

adb shell dumpsys activity top

Then, I used this code to parse it:

public class ViewCoordsGetter {
    public static Rect getViewBoundyingBox(String viewIdStr) {
        final List<String> viewHierarchyLog = //result of the command
        for (int i = 0; i < viewHierarchyLog.size(); ++i) {
            String line = viewHierarchyLog.get(i);
            if (line.contains(":id/" + viewIdStr + "}")) {
                Rect result = getBoundingBoxFromLine(line);
                if (i == 0)
                    return result;
                int currentLineStart = getStartOfViewDetailsInLine(line);
                for (int j = i - 1; j >= 0; --j) {
                    line = viewHierarchyLog.get(j);
                    if ("View Hierarchy:".equals(line.trim()))
                        break;
                    int newLineStart = getStartOfViewDetailsInLine(line);
                    if (newLineStart < currentLineStart) {
                        final Rect boundingBoxFromLine = getBoundingBoxFromLine(line);
                        result.left += boundingBoxFromLine.left;
                        result.right += boundingBoxFromLine.left;
                        result.top += boundingBoxFromLine.top;
                        result.bottom += boundingBoxFromLine.top;
                        currentLineStart = newLineStart;
                    }
                }
                return result;
            }
        }
        return null;
    }

    private static int getStartOfViewDetailsInLine(String s) {
        int i = 0;
        while (true)
            if (s.charAt(i++) != ' ')
                return --i;
    }

    private static Rect getBoundingBoxFromLine(String line) {
        int endIndex = line.indexOf(',', 0);
        int startIndex = endIndex - 1;
        while (!Character.isSpaceChar(line.charAt(startIndex - 1)))
            --startIndex;
        int left = Integer.parseInt(line.substring(startIndex, endIndex));
        startIndex = endIndex + 1;
        endIndex = line.indexOf('-', startIndex);
        endIndex = line.charAt(endIndex - 1) == ',' ? line.indexOf('-', endIndex + 1) : endIndex;
        int top = Integer.parseInt(line.substring(startIndex, endIndex));
        startIndex = endIndex + 1;
        endIndex = line.indexOf(',', startIndex);
        int right = Integer.parseInt(line.substring(startIndex, endIndex));
        startIndex = endIndex + 1;
        //noinspection StatementWithEmptyBody
        for (endIndex = startIndex + 1; Character.isDigit(line.charAt(endIndex)); ++endIndex)
            ;
        int bot = Integer.parseInt(line.substring(startIndex, endIndex));
        return new Rect(left, top, right, bot);
    }
}
android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • What's with the upvotes? This does not work. Using `line.contains("...")` for the line validation is very poor software design. – Alex P. Sep 18 '16 at 22:32
  • @AlexP. Can you please explain why? The syntax of each line matches this rule fine, as I've found. Just like any parsing mechanism, it relies on how each line is written. No matter what you use, it relies on it. – android developer Sep 19 '16 at 07:14
  • The *dotted* part of the line is boolean flags and the `.` stands for *false* value. So your code only works for views which (and all their parent views as well) have those last 3 flags set to false which is not always the case – Alex P. Sep 19 '16 at 13:51
  • It boggles my mind how one could spend over 2 weeks on a poor attempt to reinvent the wheel instead of using the facility which was specifically created by the platform developers to do exactly what you asked for. – Alex P. Sep 19 '16 at 16:58
  • @AlexP. Which 3 flags? I could be wrong of course with the parsing (because I couldn't find docs about this output, so I figured it by myself), but still it needs parsing. Can you please suggest what to change it to? About poor attempt, I don't see any other answer. The only other answer is written in the comments to my question, which is to create code within an app. This is not what I asked for (though I do want to know how it works too). I want to know how the sdk-tool works, so that I could do the same, using a PC or a rooted device. For now I got only ids and coordinates. I want text too – android developer Sep 19 '16 at 23:17
  • Also, which facility ? I asked about how the current tool works, so that I could use it in different ways and for different purpose. – android developer Sep 19 '16 at 23:32
  • http://stackoverflow.com/questions/39315028/is-it-possible-to-use-adb-commands-to-click-on-a-view-by-finding-its-id#comment65962708_39315028 – Alex P. Sep 20 '16 at 13:15
  • http://stackoverflow.com/questions/39315028/is-it-possible-to-use-adb-commands-to-click-on-a-view-by-finding-its-id#comment66014981_39315028 – Alex P. Sep 20 '16 at 13:16
  • @AlexP. You talk about "uiautomator" tool. As I've found, its path is different than the other tools for some reason. How come? Can this tool work on Android itself (with root if needed) ? If so, how, and what are its requirements? How do I parse its output? What is actually its output? Can you please show it in an answer? – android developer Sep 20 '16 at 20:01
  • I have shown everything I wanted to show in the answer to the question that yours is a duplicate of – Alex P. Sep 20 '16 at 20:03
  • @AlexP. Seems promising but when I've read it before, it didn't have any example of how to use it, so I found a jar file that I thought needs to be used. What are the requirements for this? min API ? permissions (in case I run on Android device, without root) ? Is there a way to avoid it creating a file, and just print the output as what "adb shell dumpsys activity top" does ? Why didn't you write this as an answer? The other answer doesn't containt the parsing part ... – android developer Sep 20 '16 at 21:04
  • @AlexP. Also, regarding my solution, can you please show how to fix it to be always correct ? – android developer Sep 20 '16 at 21:33