A_Matar
A_Matar

Reputation: 2320

Android: Removing the spacing above and below CardViews in a RecyclerView

I am using a RecyclerView with CardViews inside and I want to remove the Margins/Padding/Spacing left in between the children cards inside the RecyclerView. How can I do that? Here is my CardView:

    <android.support.v7.widget.CardView 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="wrap_content"
    card_view:cardUseCompatPadding="false">

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

        <!--status lock-->
        <ImageView
            android:id="@+id/imageField"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_margin="20dp" />

        <!--text info section-->
        <LinearLayout
            android:id="@+id/textInfo"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical">

            <!--NAME-->
            <TextView
                android:id="@+id/nameField"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="item name"
                android:gravity="center_vertical"
                android:textSize="@dimen/abc_text_size_large_material" />

            <!--category-->
            <TextView
                android:id="@+id/categoryField"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:lines="1"
                android:maxLines="1"
                android:text="category"
                android:textSize="@dimen/abc_text_size_small_material" />

            <!--working hours-->
            <LinearLayout
                android:id="@+id/workingHours"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:lines="1"
                android:maxLines="1"
                android:orientation="horizontal">

                <ImageView
                    android:id="@+id/workingHoursIcon"
                    android:layout_width="15dp"
                    android:layout_height="15dp"
                    android:layout_gravity="center_vertical"
                    android:background="@drawable/ic_clock"
                    android:padding="50dp"></ImageView>

                <TextView
                    android:id="@+id/WHStart"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingLeft="5dp" />

                <TextView
                    android:id="@+id/dash"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingLeft="5dp"
                    android:text=" - " />

                <TextView
                    android:id="@+id/WHEnd"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:lines="1"
                    android:maxLines="1"
                    android:paddingLeft="5dp" />

            </LinearLayout>

            <!--message-->
            <LinearLayout
                android:id="@+id/messageSection"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <ImageView
                    android:id="@+id/messageIcon"
                    android:layout_width="15dp"
                    android:layout_height="15dp"
                    android:layout_gravity="center_vertical"
                    android:background="@drawable/ic_message"
                    android:padding="50dp" />

                <TextView
                    android:id="@+id/message"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:lines="1"
                    android:maxLines="1"
                    android:paddingLeft="5dp"
                    android:text="No Message" />
            </LinearLayout>

        </LinearLayout>

        <!--add to favorite button-->
        <ImageButton
            android:id="@+id/btn_add_to_favorite_list"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="20dp"
            android:layout_marginLeft="48dp"
            android:layout_marginRight="20dp"
            android:layout_marginStart="48dp"
            android:background="@drawable/ic_star_gray" />

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

And my RecyclerView:

<android.support.v7.widget.RecyclerView
            android:id="@+id/my_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/my_store"
            android:scrollbars="vertical" />

Here is a screenshot:

enter image description here

Upvotes: 0

Views: 3452

Answers (3)

Naman Choradia
Naman Choradia

Reputation: 644

This might be what you were looking for CardView inside RecyclerView has extra margins

Plus if you are not looking to keep spaces you should simply use a LinearLayout for your adapter layout removing the CardView altogether, that should do it.

Hope that helps.

Upvotes: 1

Michele Lacorte
Michele Lacorte

Reputation: 5363

Try this:

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

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

    <!--status lock-->
    <ImageView
        android:id="@+id/imageField"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_margin="20dp" />

    <!--text info section-->
    <LinearLayout
        android:id="@+id/textInfo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <!--NAME-->
        <TextView
            android:id="@+id/nameField"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="item name"
            android:gravity="center_vertical"
            android:textSize="@dimen/abc_text_size_large_material" />

        <!--category-->
        <TextView
            android:id="@+id/categoryField"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:lines="1"
            android:maxLines="1"
            android:text="category"
            android:textSize="@dimen/abc_text_size_small_material" />

        <!--working hours-->
        <LinearLayout
            android:id="@+id/workingHours"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:lines="1"
            android:maxLines="1"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/workingHoursIcon"
                android:layout_width="15dp"
                android:layout_height="15dp"
                android:layout_gravity="center_vertical"
                android:background="@drawable/ic_clock"
                android:padding="50dp"></ImageView>

            <TextView
                android:id="@+id/WHStart"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="5dp" />

            <TextView
                android:id="@+id/dash"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="5dp"
                android:text=" - " />

            <TextView
                android:id="@+id/WHEnd"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:lines="1"
                android:maxLines="1"
                android:paddingLeft="5dp" />

        </LinearLayout>

        <!--message-->
        <LinearLayout
            android:id="@+id/messageSection"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/messageIcon"
                android:layout_width="15dp"
                android:layout_height="15dp"
                android:layout_gravity="center_vertical"
                android:background="@drawable/ic_message"
                android:padding="50dp" />

            <TextView
                android:id="@+id/message"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:lines="1"
                android:maxLines="1"
                android:paddingLeft="5dp"
                android:text="No Message" />
        </LinearLayout>

    </LinearLayout>

    <!--add to favorite button-->
    <ImageButton
        android:id="@+id/btn_add_to_favorite_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="20dp"
        android:layout_marginLeft="48dp"
        android:layout_marginRight="20dp"
        android:layout_marginStart="48dp"
        android:background="@drawable/ic_star_gray" />

</LinearLayout>

Upvotes: 0

Max McKinney
Max McKinney

Reputation: 1218

Check if it is margin or padding (Dev Options / show layout bounds)

CardView adds padding in pre-L API levels to show shadows. In L, unless you set useCompatPadding=true, there should not be any gap.

Adding negative margins (although it is ugly) should work.

This might work

card_view:contentPaddingLeft="-3dp"
card_view:contentPaddingRight="-3dp"
card_view:contentPaddingTop="-3dp"
card_view:contentPaddingBottom="-3dp"

Upvotes: 0

Related Questions