HaBiT
HaBiT

Reputation: 33

CardCornerRadius is not clipping linear layout within view

I am trying to have images in my recycler view have rounded corners to look better. I looked online and found out I could not set the background to a cardview but I could set a background to a linear layout so that is what I did. In the design tab of Android Studio I can see that the cardview has rounded corners but when I run the app the corners are not rounded. I tried googling the problem for a few hours but I could not find an answer.

Here is the contrast on what I am seeing in Android studio and the emulator enter image description here

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="wrap_content"
    android:orientation="vertical"
    android:paddingBottom="5dp">

<androidx.cardview.widget.CardView
    android:id="@+id/cvPhoto"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="10dp"
    android:elevation="0dp"
    app:cardBackgroundColor="#CABCBC"
    app:cardCornerRadius="15dp"
    app:cardPreventCornerOverlap="false"
    app:cardUseCompatPadding="false"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <LinearLayout
        android:id="@+id/llPhoto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/_0443265_10154716579055043_2084238995361491869_o"
        android:orientation="horizontal"></LinearLayout>
</androidx.cardview.widget.CardView>

<TextView
    android:id="@+id/tvName"
    android:layout_width="0dp"
    android:layout_height="19dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="32dp"
    android:layout_marginBottom="8dp"
    android:text="name"
    android:textSize="14sp"
    app:layout_constraintBottom_toTopOf="@+id/tvSurname"
    app:layout_constraintStart_toEndOf="@+id/cvPhoto"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="1.0" />

<TextView
    android:id="@+id/tvSurname"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginBottom="60dp"
    android:text="surname"
    android:textColor="@color/gold"
    android:textStyle="bold"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toEndOf="@+id/cvPhoto" />

<TextView
    android:id="@+id/tvRank"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="24dp"
    android:text="Rank"
    android:textColor="@color/gold"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/imageView"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.503" />

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/next_arrow" />


</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 1

Views: 1300

Answers (2)

mikael ezzati
mikael ezzati

Reputation: 71

set cardPreventCornerOverlap="false" i send example:

    <com.google.android.material.card.MaterialCardView
    style="@style/CustomCardViewStyle"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginStart="5dp"
    android:layout_marginTop="@dimen/dimen5dp"
    android:layout_marginEnd="5dp"
    android:theme="@style/Theme.MaterialComponents.Light"
    app:cardBackgroundColor="@android:color/white"
    app:cardElevation="@dimen/cardview_default_elevation"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    android:padding="0dp"
    app:cardPreventCornerOverlap="false"
    app:strokeColor="@color/white">

Upvotes: 0

Miftah Classifieds
Miftah Classifieds

Reputation: 291

I read some comments that suggest to move your linearlayout to have effects that you would like but I suggest to let your linearlayout and instead do like this:

  1. put some padding between your cardview and your linearlayout:

    app:cardUseCompatPadding="true"
    android:padding="16dp"
    

    don't miss cardUseCompatPadding flag!

  2. put your corners radius and elevation like this:

    app:cardElevation="@dimen/cardview_elevation"
    app:cardCornerRadius="@dimen/cardview_corners_radius"
    
  3. set cardBackground to white color and transparent color for the constraintLayout parent;

    app:cardBackgroundColor="@color/white" for cardview
    android:background="@color/transparent" for parent layout
    

and you're all done.

Upvotes: 0

Related Questions