developer82
developer82

Reputation: 13733

android - relative layout - put object on top of each other

Is it possible to put one object on top of another?

I have a background image (ImageView) on top of that background I want to put a face (ImageView) and another image that is a speech bubble that comes out of the face (ImageView). So both the face and the speech bubble go on top of the background ImageView.

On top of the speech bubble I want to put TextView.

How can I do such thing?

Thanks

Upvotes: 0

Views: 7669

Answers (3)

Deve1opper
Deve1opper

Reputation: 37

In the design panel's attributes, you can find one called "Elevation" which is layout independent. Increase it to 10 dp and you can give the one at the top a higher one. With varying elevation, you can create a layering effect by increasing or decreasing the layered objects' elevation depending on which one is more on top.

OR if you want to do it manually put this in that object's XML brackets, where the value can be anything greater than 1dp,

android:elevation="10dp"

Upvotes: 0

Libin
Libin

Reputation: 17095

Create a Relativelayout as parent and place the your ImageView and TextView as center to parent , i.e., android:layout_centerInParent= "true"

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:background="@color/white">
<ImageView
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:src="@color/Red"
    android:layout_centerInParent="true"/>

<ImageView
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:src="@color/Blue"
    android:layout_centerInParent="true"/>

<TextView
    android:text="HELLO"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"/>

  </RelativeLayout>

Upvotes: 3

Rarw
Rarw

Reputation: 7663

You should be able to do this if you properly order your child views within the RelativeLayout. A RelativeLayout draws child views in the order in which they appear in the XML. That means the last item in the XML is the last item drawn and as a result will be drawn on top of anything else in the same place. By controlling the order of your child views, putting what you want on top further down in the XML, you should be able order your items so they appear as you like.

Upvotes: 2

Related Questions