Search code examples
androidlayoutparent-childviewgroup

Android and reordering the children of a ViewGroup


  1. How do you exchange the indices of two children in a ViewGroup?

  2. How do I use setLeft on a child view? It doesn't seem to be defined for objects of class View.

Edit.

Answer to #2 is that setLeft is only available from API11. Ditto setX.


Solution

  • You can remove all children and than add by required order. I'm not found another way.

    IMHO the best way for reorganize layout is use RelativeLayout.

    As I do this:

    // prepare rules
    lpTopLeft = new RelativeLayout.LayoutParams(minDimension/5, minDimension/5);
    lpTopLeft.setMargins(minDimension/50, minDimension/50, minDimension/50, minDimension/50);
    lpTopLeft.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    lpTopLeft.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    

    And now use prepared settings:

    // rearrange child
    bnReset.setLayoutParams(lpTopLeft);