Ken
Ken

Reputation: 31161

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.

Upvotes: 0

Views: 2226

Answers (3)

behzad besharati
behzad besharati

Reputation: 6910

best solution: flexbox-layout by Google

FexboxLayout is a library project which brings the similar capabilities of CSS Flexible Box Layout Module to Android.

Then you can set

layout_order

This attribute can change how the ordering of the children views are laid out. By default, children are displayed and laid out in the same order as they appear in the layout XML. If not specified, 1 is set as a default value.

Upvotes: 1

C.d.
C.d.

Reputation: 9995

I guess removing the views and re-inflating the layout is the only solution. Maybe you can try to use a ViewFlipper. I don't know if it's suitable for your case.

Upvotes: 1

Silver
Silver

Reputation: 12475

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);

Upvotes: 1

Related Questions