3

I need to get an array of all activity components, then I need to iterate through this array and choose, for instance, only buttons. How to do this ?

mtrw
  • 34,200
  • 7
  • 63
  • 71
CROSP
  • 4,499
  • 4
  • 38
  • 89

1 Answers1

9

this may help you...

public ArrayList<Button> getButtons() {
    ArrayList<Button> buttons = new ArrayList<Button>();
    ViewGroup viewGroup = (ViewGroup) getWindow().getDecorView();
    findButtons(viewGroup, buttons);
    return buttons;
}

private static void findButtons(ViewGroup viewGroup,ArrayList<Button> buttons) {
    for (int i = 0, N = viewGroup.getChildCount(); i < N; i++) {
        View child = viewGroup.getChildAt(i);
        if (child instanceof ViewGroup) {
            findButtons((ViewGroup) child, buttons);
        } else if (child instanceof Button) {
            buttons.add((Button) child);
        }
    }
}
Gopal Gopi
  • 11,101
  • 1
  • 30
  • 43
  • This post might be helpful too: http://stackoverflow.com/questions/4486034/get-root-view-from-current-activity – Doc Sep 23 '14 at 17:12