Mojtaba Mahamed
Mojtaba Mahamed

Reputation: 505

extending cardview class show white rectangle around the card

I'm extending CardView class because I want to create custom view for each row of my list view. this is my xml layout

<com.mojiapps.myquran.items.TranslationDownloadItemView  xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:foreground="?selectableItemBackground"
android:gravity="right"
card_view:cardCornerRadius="3dp"
card_view:cardElevation="3dp"
card_view:cardPreventCornerOverlap="false">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
...

</com.mojiapps.myquran.items.TranslationDownloadItemView>

and this is my java class

public class TranslationDownloadItemView extends CardView {


public TranslationDownloadItemView(Context context) {
    super(context);
    init();
}

public TranslationDownloadItemView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

private void init() {
    LayoutInflater inflater = LayoutInflater.from(getContext());

    inflater.inflate(R.layout.translation_type_row, this);
...
}
...
}

and this is the result

enter image description here

can anybody help me?

Upvotes: 6

Views: 1337

Answers (3)

Trasd
Trasd

Reputation: 108

I realize this is an older question, but I wanted to point out a simpler method to inflating in a child of View (so often overlooked).

Instead of:

LayoutInflater inflater = LayoutInflater.from(getContext());
inflater.inflate(R.layout.translation_type_row, this);

Try this:

inflate(getContext(), R.layout.translation_type_row, this);

Using the static View inflate() is the cleanest and most efficient way to go; you don't need a LayoutInflater.

Upvotes: 0

Manohar
Manohar

Reputation: 23394

In my case I solved it by setting white background to CardView

<androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:cardBackgroundColor="@color/white">

Or you can set cardElevationto 0dp but this would disable shadow

<androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardBackgroundColor="@color/trans_black_darker"
    app:cardCornerRadius="10dp"
    app:cardPreventCornerOverlap="false"
    app:cardUseCompatPadding="true"
    app:cardElevation="0dp">

Upvotes: 0

Javier Delgado
Javier Delgado

Reputation: 2383

I had this issue setting the CardView background to something transparent. I solved it setting a non-alpha color.

Try to set your android:foreground to something without alphas.

Upvotes: 2

Related Questions