1

I am testing my application on multiple devices and noticed that with the CardView, certain devices are rendering spacing in between each CardView.

I am trying to isolate the problem, and it looks like API 23 does not include any spacing, whereas API < 23 renders some sort of padding/spacing.

Note that I have already tried the solution proposed here and it did not work. It proposed the following in the XML:

app:cardUseCompatPadding="true"

API 23

enter image description here

API < 23

enter image description here

Community
  • 1
  • 1
tccpg288
  • 3,242
  • 5
  • 35
  • 80

2 Answers2

3

The problem is that CardViews are natively supported on api-21 and up, and on KitKat and earlier they are "faked" by the support library.

What I've done in the past with success is to use separate layout files, and just manually pixel-push them until it looks right.

For example, in res/layout, you have this xml:

<android.support.v7.widget.CardView
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:cardview="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools"
     android:id="@+id/my_card"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:layout_marginRight="0dip"
     android:layout_marginLeft="0dip"
     android:layout_marginTop="1dip"
     android:layout_marginBottom="1dip"
     cardview:cardBackgroundColor="@color/even_light_gray"
     android:foreground="@drawable/card_foreground"
     cardview:cardCornerRadius="1dp" >

     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingBottom="8dp"
        android:paddingLeft="2dp"
        android:paddingRight="2dp"
        android:paddingTop="4dp" >

        <!-- .......... -->

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

Then, in res/layout-21, you have this layout with different margin and padding values:

<android.support.v7.widget.CardView
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:cardview="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools"
     android:id="@+id/my_card"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:layout_marginRight="1dip"
     android:layout_marginLeft="1dip"
     android:layout_marginTop="4dip"
     android:layout_marginBottom="4dip"
     cardview:cardBackgroundColor="@color/even_light_gray"
     android:foreground="@drawable/card_foreground"
     cardview:cardCornerRadius="1dp" >

     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingBottom="8dp"
        android:paddingLeft="4dp"
        android:paddingRight="4dp"
        android:paddingTop="4dp" >

        <!-- .......... -->

        </LinearLayout>
</android.support.v7.widget.CardView>
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
  • Would this XML allow for no padding on the CardView? Ultimately, I am trying to remove the padding on all API levels... – tccpg288 Jun 30 '16 at 11:44
  • It actually looks like you might not want to use a CardView at all then. Usually you use a CardView when you want the corner radius and elevation, which you're not even using. Try using just a LinearLayout or RelativeLayout instead of a CardView. – Daniel Nugent Jun 30 '16 at 18:02
3

i find this ... use this in cardview attr,
app:cardPreventCornerOverlap="false"
and use this in different android os version
app:cardMaxElevation="0dp" app:cardElevation="0dp"
in pre-L,make them 0dp ,and in api21 or after,make them non-zero

dx3906
  • 327
  • 1
  • 4
  • 12