GilbertLee
GilbertLee

Reputation: 696

Custom CardView border showed twice

I customized some cardviews like this:

public class CustomCard extends CardView {

    public CustomCard(Context context) {
        this(context, null);
    }

    public CustomCard(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0);
    }

    public CustomCard(Context context, AttributeSet attributeSet, int defStyle) {
        super(context, attributeSet, defStyle);

        //R.layout.card_custom is the custom xml file 
        inflate(context, R.layout.card_custom, this);
    }
}

Then I constructed and added them to ViewGroup like below:

CustomCard card = new CustomCard(this);
someLayout.addView(card);

The problem is I will see two layers of CardView border in the UI like below(it is apparent there was two layer of elevations at the border):

Problem

Anyone has an idea? Thanks

Edit:

One xml of the custom CardView:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="14dp"
        android:layout_marginBottom="14dp">

        <!--- Some Details --->

    </RelativeLayout>

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

Some Layout I mentioned above:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background_gray">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <!--- Details --->

    </LinearLayout>

</ScrollView>

Upvotes: 2

Views: 765

Answers (1)

Avinash R
Avinash R

Reputation: 3151

Looks like you are inflating a CardView inside your custom card view, which is causing this issue.

To solve this, change your layout/custom_card.xml to have RelativeLayout as the parent instead of CardView

Upvotes: 2

Related Questions