Sudheej
Sudheej

Reputation: 2019

Unable to remove extra space between CardView items - Scrollable RecyclerView

Folks,

I am out of options while attempting to removing extra space between cardview items in my recyclerview below is the current output. Please suggest.

enter image description here

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#303030"
    android:id="@+id/cv"
    android:layout_marginTop="2dip"
    android:layout_marginBottom="2dip"
    card_view:cardUseCompatPadding="true"
    card_view:cardElevation="5dp"
    android:foreground="?android:attr/selectableItemBackground"
    >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:padding="5dp"
        android:longClickable="true"
        android:background="#303030">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/person_photo"
            android:background="@drawable/vector_red"
            android:layout_alignBottom="@+id/txtSub" />


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/txtMain"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/person_photo"
            android:layout_toEndOf="@+id/person_photo"
            android:elevation="4dp"
            android:textSize="20dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Small Text"
            android:id="@+id/txtSub"
            android:layout_below="@+id/txtMain"
            android:layout_toRightOf="@+id/person_photo"
            android:layout_toEndOf="@+id/person_photo" />

    </RelativeLayout>

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

My Recycler view

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeFragment">

<android.support.v7.widget.RecyclerView
    android:id="@+id/rv"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical"
    card_view:cardUseCompatPadding="false"
    card_view:cardPreventCornerOverlap="false"
    android:background="#0fffffff" />

 </RelativeLayout>

Upvotes: 6

Views: 12549

Answers (5)

fazil
fazil

Reputation: 1155

In the card view xml change in the overall parent layout width to wrap content.

android:layout_width="match_parent"

to

android:layout_width="wrap_content"    

Upvotes: 0

Michele La Ferla
Michele La Ferla

Reputation: 6884

All you need to do is remove the following two lines from the xml for the CardLayout:

android:layout_marginTop="2dip"
android:layout_marginBottom="2dip"

What this does is sets the seperation between the two layouts, in this case you have 4dip (2 at the top and 2 at the bottom). You can also try to reduce it to 1 dip and verify the effect it gives you.

Hope this helps :)

Upvotes: 2

H Raval
H Raval

Reputation: 1903

change your card layout to

<android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         android:background="#303030"
         android:id="@+id/cv"
        card_view:cardCornerRadius="2dp"
        card_view:cardElevation="2dp"
        card_view:cardPreventCornerOverlap="false"
        card_view:cardUseCompatPadding="true">

Upvotes: 0

Junaid
Junaid

Reputation: 7860

I have not implemented a RecyclerView, however I have replicated two rows in a linear layout, minus the padding from your original question:

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


    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:background="#303030"
        android:id="@+id/cv1"
        card_view:cardElevation="5dp"
        android:foreground="?android:attr/selectableItemBackground"
        xmlns:android="http://schemas.android.com/apk/res/android">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:layout_margin="4dp"
            android:longClickable="true"
            android:background="#303030">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/person_photo"
                android:background="@drawable/vector_red"
                android:layout_alignBottom="@+id/txtSub" />


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Large Text"
                android:id="@+id/txtMain"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/person_photo"
                android:layout_toEndOf="@+id/person_photo"
                android:elevation="4dp"
                android:textSize="20dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="Small Text"
                android:id="@+id/txtSub"
                android:layout_below="@+id/txtMain"
                android:layout_toRightOf="@+id/person_photo"
                android:layout_toEndOf="@+id/person_photo" />

        </RelativeLayout>

    </android.support.v7.widget.CardView>
    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:background="#303030"
        android:id="@+id/cv1"
        card_view:cardElevation="5dp"
        android:foreground="?android:attr/selectableItemBackground"
        xmlns:android="http://schemas.android.com/apk/res/android">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:layout_margin="4dp"
            android:longClickable="true"
            android:background="#303030">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/person_photo"
                android:background="@drawable/vector_red"
                android:layout_alignBottom="@+id/txtSub" />


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Large Text"
                android:id="@+id/txtMain"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/person_photo"
                android:layout_toEndOf="@+id/person_photo"
                android:elevation="4dp"
                android:textSize="20dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="Small Text"
                android:id="@+id/txtSub"
                android:layout_below="@+id/txtMain"
                android:layout_toRightOf="@+id/person_photo"
                android:layout_toEndOf="@+id/person_photo" />

        </RelativeLayout>

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

</LinearLayout>

Play around with the padding and you will get better results. You can also try adding a background color to your card, then you can get rid of layout margins in your RelativeLayout!

Upvotes: 5

Hiren Patel
Hiren Patel

Reputation: 52800

Can you please try this one ?

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
...
...
...
</android.support.v7.widget.CardView>

I have checked, it works fine for me.

Check screenshot:

enter image description here

Hope this will help you.

Upvotes: 0

Related Questions