Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top