My Hexaville
Spead the Word
android change children order

Android Change Children Order At Runtime


I just came across an interesting code sample to arrange views at runtime, other than with Scene transition framework and found it useful to fix that ugly login screen animation logic I had before. Let’s check it out

 

 

I’m pretty sure that it would be a pretty clean solution to use Scene transitions, though since what I need is a simple change of order of children in a FrameLayout, scenes might be even unnecessary. Plus you lose the state when using Scene transitions

Bring To Top

Bringing a child to top is possible just by calling this method on a layout child

And it will get to the top most position in the layout

Rearrange Children

Now what about when we have more than two children or maybe you just need to replace children positions? getChildDrawingOrder method from ViewGroup is what we need

getChildDrawingOder‘s second argument is the index of a child in parent from the XML file. And the return value is its resulting index position to be drawn.

You can create an if statement or simply preset different resulting layout configurations with 2 dimensional array

It’s also required to call setChildrenDrawingOrderEnabled(true) so that getChildDrawing order is called

To trigger order change, you’d just call invalidate with presetting a new config

 

This code is pretty simple, there’s no need to worry about state loosing when rearranging views like with Scene transition, but again, it’s just a simple order change. You can get the source code here, it’s a Login Animations update


About the Author Ihor Klimov

Formerly an Android developer, lately picked up some Flutter. This blog is everything that I find exciting about Android and Flutter development. Stay tuned and hope to see you again!

>