Kyle
Kyle

Reputation: 618

Adding one layout on top of another programatically

I've got a button that when tapped, creates a RelativeLayout with simple Views like text in it. They're all to be contained in a vertical linear layout.

When tapping in succession, multiple relative layouts get created, with the latest layout created stacked below the previous one. I would, however, like to reverse this order - the latest layout gets stacked ABOVE the previous one.

Relative layouts don't have attributes like layout_below etc, however. The only messy way I can do this is with a grid layout, with the latest ones having a row number less than the previous one. Again, this is messy and consumes unnecessarily memory by preloading a bunch of invisible rows.

Does anyone have an elegant way of doing this? I would be extremely grateful!

Upvotes: 1

Views: 64

Answers (1)

Marko
Marko

Reputation: 20513

If you are using addView(View view) to inflate views to your layout, you can use

addView(View child, int index)

with index 0 to add it to the top.

Upvotes: 2

Related Questions