skip to Main Content

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


  1. Chosen as BEST ANSWER

    I finally managed to set the margins surrounding the cardview with a linear layout. But the width did not get to fill the full width of the screen even though I have all widths set to match_parent

    <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout 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:layout_margin="12dp"
            android:orientation="vertical">
            <androidx.cardview.widget.CardView
                android:id="@+id/card_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:cardBackgroundColor="#81C784"
                app:cardCornerRadius="9dp"
                app:cardElevation="3dp"
                app:contentPadding="4dp"
                android:layout_margin="5dp"
                >
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
    
                    <TextView
                        android:id="@+id/idCodigo"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        />
    
                    <TextView
                        android:id="@+id/idNombre"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        />
                </LinearLayout>
            </androidx.cardview.widget.CardView>
        </LinearLayout>
    

  2. Just Change your CardView layout as below I created.

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp">
    
        <androidx.cardview.widget.CardView
            android:id="@+id/card_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:cardBackgroundColor="#81C784"
            app:cardCornerRadius="12dp"
            app:cardElevation="3dp"
            app:contentPadding="4dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:ignore="MissingConstraints">
    
            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="12dp"
                android:orientation="horizontal">
    
                <TextView
                    android:id="@+id/idCodigo"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="5dp" />
    
                <TextView
                    android:id="@+id/idNombre"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="5dp" />
            </LinearLayout>
        </androidx.cardview.widget.CardView>
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    If your problem is the same, Let me know

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search