Home » Android » android – How to bring ImageView over the CardView? Please see details

android – How to bring ImageView over the CardView? Please see details

Posted by: admin June 15, 2020 Leave a comment

Questions:

I’m developing an app and I have made the below shown layout:

Screenshot with an avatar, name and twitter handle where the name & twitter pane is covering part of the avatar

There is a CircleImageView and a CardView.

Here’s the XML code:

<RelativeLayout
    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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.playrapp.playr.Profile"
    tools:showIn="@layout/activity_profile">

    <android.support.v7.widget.CardView
        android:id="@+id/profileCard"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        app:contentPadding="10dp">

    </android.support.v7.widget.CardView>

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/userImageProfile"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_marginTop="50dp"
        android:src="@mipmap/ic_launcher"
        android:layout_centerHorizontal="true"/>

</RelativeLayout>

Here, the CardView is over the ImageView. I want ImageView to be over the CardView. How can I do that?

Please let me know.

How to&Answers:

Elevation of CardView is more higher than CircleImageView. So, you have to set higher elevation to CircleImageView to show it over CardVew.

Set CardView elevation as app:cardElevation="4dp" and set CircleImageView elevation as android:elevation="8dp"

Here is the working XML code. Try this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <android.support.v7.widget.CardView
        android:id="@+id/profileCard"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_margin="16dp"
        app:cardCornerRadius="4dp"
        app:cardElevation="4dp"
        app:cardUseCompatPadding="false" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:gravity="center_horizontal"
            android:layout_marginTop="75dp"
            android:padding="24dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hammad Nasir"
                android:textSize="32sp"
                android:textColor="#727272"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="\@hammadnasir"
                android:textSize="24sp"
                android:textColor="#727272" />

        </LinearLayout>
    </android.support.v7.widget.CardView>


    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/userImageProfile"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_marginTop="75dp"
        android:layout_centerHorizontal="true"
        android:src="@mipmap/ic_launcher"
        android:elevation="8dp" />

</RelativeLayout>

OUTPUT:

enter image description here

Hope this will help~

Answer:

The CardView has an elevation set by default set app:elevation="0dp"in the xml file.