Color top section of Android CardView while keeping rounded corners

Viewed 3341

So I feel like I'm missing something, but I just cant seem to figure this out. So I want to make a small strip of color on the top of my CardView, but if I use a frame layout, or any kind of layout and set the background color, it covers up the rounded corners of the CardView. Here is some code and a screenshot of the issue. Thank you ahead of time!

<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
card_view:cardCornerRadius="10dp">

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="16dp"
    android:layout_gravity="end"
    android:background="@color/colorPrimaryDark"
    android:clipChildren="true"/>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/menuCardImage"
        android:layout_width="128dp"
        android:layout_height="128dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_margin="8dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_margin="8dp"
        android:layout_toRightOf="@+id/menuCardImage"
        android:orientation="vertical">

        <TextView
            android:id="@+id/menuCardName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="32sp"
            android:textStyle="bold" />

    </LinearLayout>

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

issue image

Edit: Turns out it was just a display issue in android studio. When I installed the app it worked correctly.

3 Answers

You can set CardView color now.

color = ContextCompat.getColor(cardView.getContext(), R.color.white);
cardView.setCardBackgroundColor(color);

This is the easiest way.

I think you should try using app:cardBackgroudColor and CardView.setCardBackgroundColor. This worked out for me.

Related