1

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!

3
  • 1
    Are you using .addView(View view) to add views to your layout? If yes, you can use addView(View view, int index), with index 0 to add it to the top. Commented Dec 28, 2015 at 15:18
  • Thank you so much. I was completely unaware that that parameter existed. It works as I intend it to now. Go ahead and answer the question, I'll mark it as the right one. Commented Dec 28, 2015 at 15:26
  • You are more than welcome. The question will maybe be closed, because it is a duplicate of this question. Commented Dec 28, 2015 at 15:35

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.