3

I created a layout in my application that includes the followings:
main_layout (FrameLayout, fills whole screen)
-> includes: main_interface (LinearLayout, height/width fills parent)

Now I want to add another LinearLayout in the main_layout BEFORE the main_interface. I tried this:

LinearLayout bar = new LinearLayout(this); bar.setGravity(Gravity.TOP); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); bar.setLayoutParams(params); main_layout.addView(bar); 

but this just overlaps my main_interface.

Any Help?

1 Answer 1

12

If you want to add a View to specific position in a ViewGroup, you can use addView(view, position). In this case, assuming main_interface is the first View, you can call main_layout.addView(bar, 0).

However, you need to fix your layout first. If you use FrameLayout as your main_layout and set main_interface height and width to match_parent, then they will surely overlap. Try making your main_layout a LinearLayout with android:orientation="vertical" if you want vertically stacked Views.

If you could post your actual XML layout, I could help you modify and test it.

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

1 Comment

Thanks, you were right in every point! I changed the Layout to LinearLayout and set the index off addView() to zero. THANKS!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.