CardView: How do I add a gradient background while maintaining the radius CardView: How do I add a gradient background while maintaining the radius xml xml

CardView: How do I add a gradient background while maintaining the radius


<androidx.cardview.widget.CardView    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_margin="@dimen/_10sdp"    app:cardCornerRadius="@dimen/_10sdp"    app:cardElevation="@dimen/_1sdp">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="vertical"        android:background="@drawable/gradient_16"        android:padding="@dimen/_6sdp">    </LinearLayout></androidx.cardview.widget.CardView>

and gradient be like

<shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle">    <gradient        android:startColor="#5B86E5"        android:endColor="#36D1DC"        android:angle="180" />    <corners        android:radius="10dp">    </corners></shape>

CardView card_radius and gradient radius should be same dimentions


If I may ask, are you per-chance testing/running on a pre-lollipop Android device? Your code seems to work as you desire (curved corners showing with the gradient) except on Android 4.

To achieve the desired result on pre-lollipop devices, you can add <corners android:radius="4dp" /> to your @drawable/btn_gradient file, (you would have to set the corner radius to match the CardView's cardCornerRadius.


Move this line

android:background="@drawable/btn_gradient"

To the CardView object

UPDATE

My bad:

Inside the place a layout to wrap the content of the .

In this case, I'd go with a <FrameLayout> like this:

<androidx.cardview.widget.CardView        android:layout_width="match_parent"        android:layout_height="match_parent">        <FrameLyout            android:layout_width="match_parent"            android:layout_height="match_parent"            android:background="@drawable/btn_gradient">