6

I want to get all children of a RelativeLayout. I get this using the code:

aditionView.getAllChildren().

But now the subviews are coming in the order in which it is added into that view. But i want them in the increasing order of position in which it is placed in the view.

Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41
Priyanka V
  • 834
  • 2
  • 12
  • 34
  • "Order of position" seems vague. Is your order left to right and top to bottom, or top to bottom and left to right, or something else? There are many orders. – satur9nine Oct 13 '11 at 05:53

1 Answers1

11

You could sort children by View.getLeft()

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    logCoordinates();
}

private void logCoordinates() {
    RelativeLayout layout = (RelativeLayout)findViewById(R.id.layout);
    for (int i = 0; i < layout.getChildCount(); i++) {
        View child = layout.getChildAt(i);
        Log.i("tag", String.format("%d %d %d %d", child.getLeft(), child.getTop(), child.getRight(), child.getBottom()));
    }
}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    logCoordinates();
}

Please, compare log output of logCoordinates() called from onCreate() and from onWindowFocusChanged(). View coordinates are not available within onCreate() (why?). See discussion here How to get height and width of Button

Community
  • 1
  • 1
vokilam
  • 10,153
  • 3
  • 45
  • 56