android - linear layout bringToFront()
Asked Answered
P

4

8

I have 4 buttons in my linear layout and i need to bring to front first button.

Normal order is

    Button 1 | Button 2 | Button 3 | Button 4

But when I call button1.bringToFront() function , button1 is going to end like

    Button 2 | Button 3 | Button 4 | Button 1

How can I solve this problem. Relative layout doesn't causes this problem but I have to use LinearLayout because buttons will order vertically and I'm deleting a button in some conditions.

Thanks

Pickmeup answered 23/5, 2013 at 16:28 Comment(1)
what output you are looking for ?Sheeting
V
10

LinearLayout doesn't work with the z-axis, hence, it's name linear. Try using a RelativeLayout and then call bringToFront() to get the desired effect. With a RelativeLayout you can call layout_alignBollow to order the views vertically. Or you can nest views and layouts, for instance, within your LinearLayout nest three RelativeLayout within those you can place your Buttons (be careful with this approach as adding too many views can be a bad thing).

Virgievirgil answered 23/5, 2013 at 16:41 Comment(2)
What is layout_alignBollow ?Pickmeup
I had a row of overlapping buttons. Your tip of using a RelativeLayout instead of a LinearLayout worked for me (in combination with calling requestLayout() and invalidate() on that RelativeLayout (it being the parent of the overlapping buttons).Nanananak
C
2

If you have to work with z-axis in a LinearLayout, you may use setTranslationZ function.

Example:

yourView.setTranslationZ(100);
Cellobiose answered 29/1, 2018 at 0:41 Comment(0)
M
0

make your layout call the forceLayout() to disable re-arrangement of the layout.

Monocyclic answered 19/1, 2014 at 6:32 Comment(1)
can you elaborate?Undressed
W
0

Since bringToFront messes up the LinearLayout order, I decided to use a RelativeLayout and put the "top" view (the view I want on top) last in the XML.

Example:

<RelativeLayout ...>

    <ViewBelow
        android:layout_below="@+id/view_on_top"
        ...
        />

    <!-- Last view in XML appears above other views on screen -->
    <ViewOnTop
        android:id="@+id/view_on_top"
        ...
        />

</RelativeLayout>

In the specific case of this question, the ViewOnTop would be the Button 1, and the ViewBelow would be a LinearLayout containing the other buttons.

Witha answered 10/7, 2015 at 12:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.