Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CardView is wrap_content list item instead of match_parent

Card View wrapping content instead of match parent. All list items is wrapped instead of matching its width. Screenshot

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout      
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_margin="@dimen/card_margin"
        android:elevation="3dp"
        card_view:cardCornerRadius="@dimen/card_album_radius">

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

            <ImageView
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:id="@+id/Album_Art"
                android:layout_alignParentTop="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:src="@drawable/ic_google_music" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="@string/song_name"
                android:id="@+id/Sname"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/Album_Art"
                android:layout_toEndOf="@+id/Album_Art" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/artist_album"
                android:id="@+id/Sdata"
                android:layout_centerVertical="true"
                android:layout_centerHorizontal="true"
                android:layout_toRightOf="@+id/Album_Art"
                android:layout_toEndOf="@+id/Album_Art"/>

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

Manually setting width works. When i set it to a large dp value, it works fine, except that the list item is out of the window. I don't understand why match_parent doesn't work. Any leads on how to fix this would be greatly appreciated.

like image 963
Prashanth Ambati Avatar asked Dec 22 '25 16:12

Prashanth Ambati


2 Answers

Are you using a recyclerView ? if so, you should override this on layoutmanager like this :

    layoutManager = new LinearLayoutManager(getActivity()) {
            @Override
            public RecyclerView.LayoutParams generateDefaultLayoutParams() {
                return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
            }
        };
    }
like image 86
issamux Avatar answered Dec 24 '25 10:12

issamux


If you are using a RecyclerViewAdapter, make sure you are using the first signature of inflate method instead of the second in the onCreateViewHolder.

1. inflate(LayoutInflater inflater, ViewGroup root, boolean attachToRoot)  

2. inflate(LayoutInflater inflater)

And, be sure to pass parent for root & true/false for attachToRoot.

like image 33
Waseem Avatar answered Dec 24 '25 09:12

Waseem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!