mcfly soft
mcfly soft

Reputation: 11645

CardView: Setting background color removes the rounded edges

I have a cardview with rounded corners:

 <androidx.cardview.widget.CardView
        android:id="@+id/chronology_card"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="2dp"
        app:cardCornerRadius="12dp">   <------- corner radius set in the layout.

view is showing with rounded corners. -> ok.

When setting the backgroundcolor by code ...

holder.cardView.setBackgroundColor(getBackgroundColor());

... then the rounded corners are gone. It lookes like a rectangle with the correct new color.

How to set the backgroundcolor at runtime and keeping the corners?

Upvotes: 2

Views: 1576

Answers (2)

Gabriele Mariotti
Gabriele Mariotti

Reputation: 364005

The CardView manages its own background drawable with rounded corners.
Using setBackgroundColor you are overriding this background.

You should use the method setCardBackgroundColor instead of setBackgroundColor.

Upvotes: 3

Yusuf Taha
Yusuf Taha

Reputation: 218

You can try to add this to your activity code:

CardView cardView = findViewById(R.id.chronology_card);
cardView.setCardBackgroundColor(Color.parseColor("#put here the HEX code for the color"));

I tried it before and worked fine without any problems in corners radius.

Upvotes: 4

Related Questions