Search code examples
androidandroid-layoutuser-interfaceandroid-imageview

CardView Text overlay over imageview in android


Here's an example what i'm trying to do :

enter image description here

The result of my attempt to do so is very bad :

enter image description here

XML code that contains CardView with ImageView , 2 TextViews and ButtonImage:

Here's my XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">

<android.support.v7.widget.CardView
    android:id="@+id/cardView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:background="@color/card"
    app:cardCornerRadius="5dp"
    app:cardElevation="6dp"
    app:cardMaxElevation="10dp"
    app:contentPadding="25dp">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/relativeLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/masarImg"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:focusable="auto"
            android:scaleType="centerCrop" />


        <TextView
            android:id="@+id/masarTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/mapBtn"
            android:layout_alignParentStart="true"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="43dp"
            android:background="#AA000000"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:paddingTop="24dp"
            android:text="SomeText"
            android:textColor="#ffffffff"
            android:textSize="24sp" />

        <TextView
            android:id="@+id/descreption"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/mapBtn"
            android:layout_alignParentStart="true"
            android:layout_gravity="center_horizontal"
            android:background="#AA000000"
            android:paddingBottom="16dp"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:text="SomeText"
            android:textColor="#ffffffff"
            android:textSize="14sp" />

        <ImageButton
            android:id="@+id/mapBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentEnd="true"
            android:padding="8dp"
            app:srcCompat="@drawable/b" />

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

Could you please help me with my XML code to get pretty result ?


Solution

  • For a better design, I recommend you to use RoundedImageView (to set image corner inside the card).

    Layout :

    1. res/layout.xml

    <FrameLayout 
         xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         xmlns:app="http://schemas.android.com/apk/res-auto">
            <android.support.v7.widget.CardView
                android:id="@+id/cardView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="20dp"
                android:background="@android:color/white"
                android:layout_gravity="center"
                app:cardCornerRadius="10dp"
                app:cardElevation="20dp">
    
            <!--Card Image-->
            <com.makeramen.roundedimageview.RoundedImageView
                android:id="@+id/img"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                android:src="@drawable/test"
                app:riv_corner_radius="10dp"/>
    
            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="20dp">
    
                <!--Title & Description Text-->
                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="start|bottom"
                    android:orientation="vertical">
                    <!--Title Text-->
                    <FrameLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="@drawable/transparent_card">
                        <TextView
                            android:id="@+id/title"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:textAlignment="center"
                            android:text="Title"
                            android:textColor="@android:color/white"
                            android:textSize="24sp"
                            android:layout_margin="5dp"/>
                    </FrameLayout>
                    <!--Description Text-->
                    <FrameLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="@drawable/transparent_card"
                        android:layout_marginTop="5dp">
                        <TextView
                            android:id="@+id/description"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:textAlignment="center"
                            android:text="Description"
                            android:textColor="@android:color/white"
                            android:textSize="20sp"
                            android:layout_margin="5dp"/>
                    </FrameLayout>
                </LinearLayout>
    
                <!--Button-->
                <FrameLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/transparent_card"
                    android:layout_gravity="end|bottom"
                    android:clickable="true"
                    android:focusable="true">
                    <FrameLayout
                        android:id="@+id/btn"
                        android:layout_width="30dp"
                        android:layout_height="30dp"
                        android:background="@android:drawable/ic_menu_share"
                        android:layout_margin="5dp"/>
                </FrameLayout>
            </FrameLayout>
        </android.support.v7.widget.CardView>
    </FrameLayout>
    

    2. drawable/transparent_card.xml

        <?xml version="1.0" encoding="utf-8"?>
        <shape
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
    
        <solid
            android:color="#78000000"/>
    
        <corners
            android:radius="5dp"/>
        </shape>
    

    The layout will looks like this:

    enter image description here