Search code examples
androidandroid-layoutandroid-relativelayout

How to put ImageView over RelativeLayout?


I'm designing a Profile Layout. For which I'm using Imageview and RelativeLayout. I'm trying put ImageView on top of RelativeLayout.I used both elevation and android:scaleType="centerCrop" but it shows okay in the design editor but when I compile and run app then ImageView always stays behind the RelativeLayout.

In Design Editor it shows

enter image description here

Which is what I want but in the device, it shows like this

enter image description here

Please help..

Below is my XML File

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/layout">


        <ImageView
            android:id="@+id/header_cover_image"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:background="#000000"
            android:scaleType="centerCrop"
            android:src="@drawable/nav_menu_heade" />


        <ImageView
            android:clickable="true"
            android:id="@+id/profile"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:layout_below="@+id/header_cover_image"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="-130dp"
            android:scaleType="fitStart"
            android:elevation="8dp"
            android:padding="20dp"
            android:src="@drawable/passport" />


        <RelativeLayout
            android:id="@+id/profile_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/header_cover_image"
            android:background="#eb0772ca"
            android:elevation="2dp"
            android:paddingBottom="2dp">



            <TextView
                android:id="@+id/name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="70dp"
                android:text="Sagar Rawal"
                android:textColor="#fff"
                android:textSize="24sp"
                android:textStyle="bold" />



            <TextView
                android:id="@+id/quote"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/name"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="10dp"
                android:text="Don't Cry cuz it's over, Smile cuz it happen"
                android:textColor="#ffffff"
                android:textSize="18sp" />
            <TextView
                android:id="@+id/location"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/quote"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="10dp"
                android:text="Jumla, Nepal"
                android:textColor="#ffffff"
                android:textSize="16sp" />

        </RelativeLayout>



    </RelativeLayout>
</ScrollView>


Solution

  • Edit Xml Like This

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
    
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/layout">
    
    
            <ImageView
                android:id="@+id/header_cover_image"
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:background="#000000"
                android:scaleType="centerCrop"
                android:src="@drawable/back" />
    
    
            <RelativeLayout
                android:id="@+id/profile_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/header_cover_image"
                android:background="#eb0772ca"
                android:elevation="2dp"
                android:paddingBottom="2dp">
    
    
                <TextView
                    android:id="@+id/name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="70dp"
                    android:text="Sagar Rawal"
                    android:textColor="#fff"
                    android:textSize="24sp"
                    android:textStyle="bold" />
    
    
                <TextView
                    android:id="@+id/quote"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/name"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="10dp"
                    android:text="Don't Cry cuz it's over, Smile cuz it happen"
                    android:textColor="#ffffff"
                    android:textSize="18sp" />
    
                <TextView
                    android:id="@+id/location"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/quote"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="10dp"
                    android:text="Jumla, Nepal"
                    android:textColor="#ffffff"
                    android:textSize="16sp" />
    
            </RelativeLayout>
    
            <ImageView
                android:clickable="true"
                android:id="@+id/profile"
                android:layout_width="200dp"
                android:layout_height="200dp"
                android:layout_below="@+id/header_cover_image"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="-130dp"
                android:scaleType="fitStart"
                android:elevation="8dp"
                android:padding="20dp"
                android:src="@drawable/app_icon" />
    
    
        </RelativeLayout>
    </ScrollView>