3

I am using recylcer view and each row contain cardview.I need to show cardview full width even my textview content is less .I tried many ways but no use.

xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/primaryDarkColor"
    android:orientation="vertical">
    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/cardview"
        android:background="#ffffff"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:elevation="8dp"
        android:layout_marginLeft="6dp"
        android:layout_marginRight="6dp"
        android:layout_marginTop="4dp"
        android:layout_marginBottom="4dp"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"

            android:padding="2dp"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/content"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingLeft="3dp"
                android:text="content"
                android:layout_weight="1"
                android:textColor="#ff212121"

                />


        </LinearLayout>

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


</LinearLayout>

enter image description here

Even i have set width to match parent but no use.

Updated

Recycler view

<LinearLayout 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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#e5e5e5"
        android:orientation="horizontal">


        <android.support.v7.widget.RecyclerView
            android:id="@+id/List"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

    </LinearLayout>
    </LinearLayout>

Updated :Fixed using following method

I have fixed my issue using following answer

The docs for inflate:

Inflate a new view hierarchy from the specified xml resource. Throws InflateException if there is an error.

Parameters resource ID for an XML layout resource to load (e.g., R.layout.main_page) root view to be the parent of the generated hierarchy (if attachToRoot is true), or else simply an object that provides a set of LayoutParams values for root of the returned hierarchy (if attachToRoot is false.) attachToRoot Whether the inflated hierarchy should be attached to the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML. Returns The root View of the inflated hierarchy. If root was supplied and attachToRoot is true, this is root; otherwise it is the root of the inflated XML file.

It is important here to not supply true, but do supply the parent:

LayoutInflater.from(parent.getContext())
            .inflate(R.layout.card_listitem, parent, false);

Supplying the parent View lets the inflater know what layoutparams to use. Supplying the false parameter tells it to not attach it to the parent just yet. That is what the RecyclerView will do for you.

This answer helped me to fix my problem

Ref: CardView layout_width="match_parent" does not match parent RecyclerView width

Community
  • 1
  • 1
Vision Coderz
  • 8,257
  • 5
  • 41
  • 57

6 Answers6

7

I have fixed my issue using following answer

The docs for inflate:

Inflate a new view hierarchy from the specified xml resource. Throws InflateException if there is an error.

Parameters resource ID for an XML layout resource to load (e.g., R.layout.main_page) root view to be the parent of the generated hierarchy (if attachToRoot is true), or else simply an object that provides a set of LayoutParams values for root of the returned hierarchy (if attachToRoot is false.) attachToRoot Whether the inflated hierarchy should be attached to the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML. Returns The root View of the inflated hierarchy. If root was supplied and attachToRoot is true, this is root; otherwise it is the root of the inflated XML file.

It is important here to not supply true, but do supply the parent:

LayoutInflater.from(parent.getContext())
            .inflate(R.layout.card_listitem, parent, false);

Supplying the parent View lets the inflater know what layoutparams to use. Supplying the false parameter tells it to not attach it to the parent just yet. That is what the RecyclerView will do for you.

This answer helped me to fix my problem

Ref: CardView layout_width="match_parent" does not match parent RecyclerView width

Community
  • 1
  • 1
Vision Coderz
  • 8,257
  • 5
  • 41
  • 57
2

In my case, it works with this change:

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FilialCruzaViewHolder {
    // Before
    // val binding = ListaFilialCruzaCardItemBinding.inflate(LayoutInflater.from(parent.context)) 

    // After
    val binding = ListaFilialCruzaCardItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
    return FilialCruzaViewHolder(binding)
}
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Nicolas400
  • 563
  • 1
  • 7
  • 29
0

You are using layout_margin in CardView tag, remove them. This is how your code should look like:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/primaryDarkColor"
android:orientation="vertical">
<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cardview"
    android:background="#ffffff"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:elevation="8dp">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        android:padding="2dp"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingLeft="3dp"
            android:text="content"
            android:layout_weight="1"
            android:textColor="#ff212121"

            />

    </LinearLayout>

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

Abhi
  • 2,115
  • 2
  • 18
  • 29
0

all them layoutsz

<LinearLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    **android:layout_height="wrap_content"**
    android:background="#e5e5e5"
    android:orientation="horizontal">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/List"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>
</LinearLayout>

From what you can see, you're wrapping content on the second linear layout, simply make changes there and all's good.

Be sure NOT to match parent recklessly for that horizontal linear layout, it might overflow!

Nicholas
  • 1,883
  • 21
  • 39
0

The second LinearLayout's width is wrap_content. It should be match_parent. You should delete the second layout because it is useless. Set the first LinearLayout background to #e5e5e5

csabapap
  • 1,031
  • 1
  • 9
  • 14
0

try to use margin and padding to your cardview and remove all margin attributes. Hope it will work. And try to reduce using layouts. android:layout_margin="5dp" android:padding="5dp"

Kiran Nemani
  • 67
  • 1
  • 6