0

I am writing a module to detect which Android activity is currently running, and complete information regarding the view tree of that activity.

I understand Android framework lays out, and draws the views. Where can I find out the information such as which views are currently visible on the screen and their positions?

I tried using reflection as below. But it always returns empty set of views. Does anyone have pointers on how to do that?

try {
  String windowManagerClassName;
  if (android.os.Build.VERSION.SDK_INT >= 17) {
    windowManagerClassName = "android.view.WindowManagerGlobal";
  } else {
    windowManagerClassName = "android.view.WindowManagerImpl";
  }
  windowManager = Class.forName(windowManagerClassName);
} catch (ClassNotFoundException e) {
  throw new RuntimeException(e);
} catch (SecurityException e) {
  throw new RuntimeException(e);
}
Field views;
Field instanceField;
try {
  views = windowManager.getDeclaredField("mViews");
  instanceField = windowManager.getDeclaredField(getWindowManagerString());
  views.setAccessible(true);
  instanceField.setAccessible(true);
  Object instance = instanceField.get(null);
  synchronized (windowManager) {
    if (android.os.Build.VERSION.SDK_INT <= 18) {
      return new HashSet<View>(Arrays.asList(((View[]) views.get(instance))));
  }
}
...
user2423696
  • 91
  • 2
  • 9
  • Those are external tools. I am looking for programmatic way (code) to get the info in my app. Do you have any pointers for code snippet? – user2423696 Oct 16 '15 at 19:33

4 Answers4

0

For and external tool, I would recommend using the Hierarchy View tool in the android device monitor.

enter image description here

[EDIT] If you'd rather do it programmatically. Try getting the root view and iterating through the children recursively.

Cory Roy
  • 5,379
  • 2
  • 28
  • 47
  • Those are external tools. I am looking for programmatic way (code) to get the info in my app. Do you have any pointers for code snippet? – user2423696 Oct 16 '15 at 19:33
0

Use hierarchyviewer to see what all views present in view hierarchy

Tool can be found in android sdk .../sdk/tools/hierarchyviewer

dex
  • 5,182
  • 1
  • 23
  • 41
  • Those are external tools. I am looking for programmatic way (code) to get the info in my app. Do you have any pointers for code snippet? – user2423696 Oct 16 '15 at 19:33
0

You could also try uiautomatorviewer under the sdk/tools folder.

It allows to navigate with the mouse pointer the activity layout displayed on your device / emulator.

Simone Leoni
  • 316
  • 1
  • 9
  • Those are external tools. I am looking for programmatic way (code) to get the info in my app. Do you have any pointers for code snippet? – user2423696 Oct 16 '15 at 19:33
0

You could try writing a custom LayoutInflator as per this answer to related question:


Here's a blog post that contains something interesting - a custom layout inflater that intercepts all view inflation:

It may be possible to use that sample code to intercept the inflate calls, and receive the resource ID of the XML file:


It's a long shot, but it gives you access to the XML files as they are being inflated. I didn't realise you could do this until I saw that previous post which prompted me to do some research.

Community
  • 1
  • 1
Richard Le Mesurier
  • 29,432
  • 22
  • 140
  • 255