I have an application with a recyclerview that contains several cardviews. And the thing is that I don’t know how to set the width of the cardview so that it occupies the entire width of the screen. I also don’t know how to make each cardview separate from the previous one and the next one. I have added margins to all the elements but the cardviews are displayed together. This is a screenshot of what the layout looks like:screenshot
and this is the code of the two layouts. First the one from the mainactivity that contains the recyclerview:
<?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="wrap_content"
android:orientation="vertical">
<include layout="@layout/toolbar"/>
<Button
android:id="@+id/escanearCodigo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Escanear codigo"
android:layout_weight="1"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
/>
</LinearLayout>
and this is the one for each item of the recyclerview (the one that contains the carviews):
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.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="wrap_content"
android:id="@+id/card_view"
android:layout_margin="20dp"
app:cardBackgroundColor="#81C784"
app:cardCornerRadius="12dp"
app:cardElevation="3dp"
app:contentPadding="4dp"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="12dp"
>
<TextView
android:id="@+id/idCodigo"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_margin="5dp"
/>
<TextView
android:id="@+id/idNombre"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_margin="5dp"
/>
</LinearLayout>
</androidx.cardview.widget.CardView>
2
Answers
I finally managed to set the margins surrounding the
cardview
with alinear layout
. But the width did not get to fill the full width of the screen even though I have all widths set tomatch_parent
Just Change your
CardView
layout as below I created.If your problem is the same, Let me know