Z-index in android?
Asked Answered
D

5

14

I've more than one element in one xml.. listview,slidingdrawer,edittext and button... i want to sliding drawer order is always in front of another elements...but i can't..

here my xml

<com.ltvie.chat.MultiDirectionSlidingDrawer
    xmlns:my="http://schemas.android.com/apk/res/com.ltvie.chat"
    android:id="@+id/drawer"
    my:direction="topToBottom"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    my:handle="@+id/handle"
    my:content="@+id/content"
    >
   <include
        android:id="@id/content"
        layout="@layout/pen_content"
        android:gravity="top"           
        />
     <ImageView
        android:id="@id/handle"
        android:layout_width="wrap_content"
        android:layout_height="5dp"
        android:src="@drawable/sliding_drawer_handle_bottom" />          
</com.ltvie.chat.MultiDirectionSlidingDrawer>

<ListView android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:stackFromBottom="true"
    android:transcriptMode="alwaysScroll"
    android:layout_above="@+id/InnerRelativeLayout"
    android:layout_alignParentTop="true" 
/>  
 <RelativeLayout 
    android:id="@+id/InnerRelativeLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" >
    <Button 
        android:text="kirim" 
        android:id="@+id/button_send"
        android:layout_alignParentRight="true" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="tambahItems"          
        >
        
    </Button>           
</RelativeLayout>

the result of my code in my picture below

enter image description here

How to fix it?

Dilative answered 13/1, 2013 at 2:4 Comment(0)
P
24

Your xml does not contain your outer layout, but I assume it is a relative layout. In a RelativeLayout, the elements' z levels are determine by the order they are added to the container. View added later will be on top. Try moving your sliding drawer to the bottom of your outer container. Like this --

<ListView android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:stackFromBottom="true"
    android:transcriptMode="alwaysScroll"
    android:layout_above="@+id/InnerRelativeLayout"
    android:layout_alignParentTop="true" 
/>  
 <RelativeLayout 
    android:id="@+id/InnerRelativeLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" >
    <Button 
        android:text="kirim" 
        android:id="@+id/button_send"
        android:layout_alignParentRight="true" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="tambahItems"          
        >

    </Button>           
</RelativeLayout> 

<com.ltvie.chat.MultiDirectionSlidingDrawer
    xmlns:my="http://schemas.android.com/apk/res/com.ltvie.chat"
    android:id="@+id/drawer"
    my:direction="topToBottom"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    my:handle="@+id/handle"
    my:content="@+id/content"
    >
   <include
        android:id="@id/content"
        layout="@layout/pen_content"
        android:gravity="top"           
        />
     <ImageView
        android:id="@id/handle"
        android:layout_width="wrap_content"
        android:layout_height="5dp"
        android:src="@drawable/sliding_drawer_handle_bottom" />          
</com.ltvie.chat.MultiDirectionSlidingDrawer>
Pablo answered 13/1, 2013 at 2:14 Comment(3)
right and the outer layout you've shown is a relative layout. Move the entire sliding drawer block to the bottom of the xml code you've shown. (see edit).Pablo
slidingdrawer succes in front..but i got new problem...listview doesn't display,,,i get error java.lang.ClassCastException: android.widget.listviewDilative
the xml change should not cause that error. Have you changed your java code recently? I would need to see more of the logcat and code to tell you what is going on there.Pablo
A
16

you can just add translation for Z as you want(1dp,2pd,...) like:

<ImageView
    android:id="@+id/imageView0"
    android:layout_width="110dp"
    android:layout_height="110dp"
    android:layout_marginTop="25dp"
    android:contentDescription="@string/desc"
    android:translationZ="1dp"
    app:srcCompat="@drawable/ic_icon" />
Absolute answered 18/3, 2021 at 1:48 Comment(0)
E
1

If it's not a relative layout, you can use [your_variable].bringToFront().

That works for me! (and I'm using a constraintlayout)

Exemplification answered 29/7, 2021 at 12:27 Comment(0)
B
0

Android XML: z-index - Try this in a RelativeLayout:

example XML:

<ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/todo"
        app:srcCompat="@drawable/ic_offer_close_outline"
        app:tint="@color/colorWhite"
        android:translationZ="999dp" />

example java:

ImageView image = new ImageView(this);
image.SetZ(float z);
Bk answered 17/2, 2023 at 21:15 Comment(0)
M
0

For Z index try the property android:translationZ:

<LinearLayout
        android:layout_width="wrap_content"
        android:layout_alignParentEnd="true"
        android:translationZ="2dp"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="5dp"
        android:layout_marginBottom="5dp"
        android:layout_height="wrap_content">
Maxwellmaxy answered 26/6, 2023 at 2:39 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Av

© 2022 - 2024 — McMap. All rights reserved.