Akash Attal
Akash Attal

Reputation: 63

background image not showing of Card View

I try to create a little round view in relative layout. but when i set image(.jpg) in card view then image is not showing.

                <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1">
                <androidx.cardview.widget.CardView
                    android:id="@+id/View"
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:background="@drawable/background"
                    app:cardCornerRadius="20dp"
                    android:layout_margin="2dp"
                    android:elevation="2dp"
                    android:layout_centerInParent="true">
                </androidx.cardview.widget.CardView>
            </RelativeLayout>

enter image description here

What i want you can see in following image. enter image description here

Upvotes: 0

Views: 1837

Answers (1)

elbert rivas
elbert rivas

Reputation: 1464

I assume you want to create a round view that contains imageview. So first, set your image using ImageView and not the CardView itself.

<androidx.cardview.widget.CardView
      android:id="@+id/View"
      android:layout_width="25dp"
      android:layout_height="25dp"
      app:cardCornerRadius="12.5dp"
      android:layout_margin="2dp"
      android:elevation="2dp"
      android:layout_centerInParent="true">

      <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="@drawable/background"/>

 </androidx.cardview.widget.CardView>

Normally, your imageview image would overlap your cardview, so to avoid that, just set your imageview clipToOutline to true programmatically in your activity.

imageView.clipToOutline = true

Upvotes: 3

Related Questions