Defining Z Order of Views of Relativelayout in Android

Defining Z order of views of RelativeLayout in Android

The easiest way is simply to pay attention to the order in which the Views are added to your XML file. Lower down in the file means higher up in the Z-axis.

Edit:
This is documented here and here on the Android developer site. (Thanks @flightplanner)

Z order in xml android

Z order determines how objects lay on each other - that is which one is on top. This is good example for HTML (with example pictures). In android is simpler (that is in my opinion conception is simpler, but usage could be less easy) since there is no explicit z index. If you use xml layout z order is determined by order of declaration views declaration in xml but if you prefer you can also change order from java code.

Android RelativeLayout View Order

Ok, I've created similar xml you can check it here :
http://pastebin.com/Dx7MXfj5


<Button
android:id="@+id/registerButton"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:text="Register" />

<ImageView
android:id="@+id/logoImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/registerButton"
android:src="@drawable/ic_confirmation" />

<LinearLayout
android:id="@+id/loginLayout"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:layout_above="@+id/registerButton"
android:layout_centerHorizontal="true"
android:layout_marginBottom="-20dp"
android:background="#F00"
android:orientation="vertical" >

<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Email" >
</EditText>

<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Password" />

<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login" />
</LinearLayout>

and the result:

Xml Result

Placing/Overlapping(z-index) a view above another view in android

You can't use a LinearLayout for this, but you can use a FrameLayout. In a FrameLayout, the z-index is defined by the order in which the items are added, for example:

<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/my_drawable"
android:scaleType="fitCenter"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:padding="5dp"
android:text="My Label"
/>
</FrameLayout>

In this instance, the TextView would be drawn on top of the ImageView, along the bottom center of the image.

Format issue placing two views side by side in RelativeLayout

Replace FrameLayout with LinearLayout and weightsum property

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2"
tools:ignore="ExtraText">

<RelativeLayout
android:id="@+id/view_background"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="#978c8c">

<ImageView
android:id="@+id/delete_icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_lock" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toStartOf="@id/delete_icon"
android:text="@string/dummy_button"
android:textColor="#fff"
android:textSize="12sp" />
</RelativeLayout>

<RelativeLayout
android:id="@+id/view_foreground"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="@android:color/white">

<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="@color/colorAccent"
android:textColor="@color/colorPrimary"
android:textSize="12sp" />

<TextView
android:id="@+id/namecat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_toEndOf="@id/name"
android:background="@color/colorAccent"
android:textColor="@color/colorPrimaryDark"
android:textSize="12sp" />
</RelativeLayout>
</LinearLayout>


Related Topics



Leave a reply



Submit