I have some RecyclerView with Card items. And on preLollipop devices it looks fine. But on Lollipop it looks without any spacing between child cards. I tried to use margin and padding, but it didn't help. When I use margin on lollipop it looks fine, but with the same margin on preLollipop spacing is very big. Once again: why is there different spacings? Which property should I use to fix it?
3 Answers
This is happening because on L, shadows are outside the the View bounds but pre-L, they are inside the View (unfortunately there is no real shadow support pre-L).
If you want CardView to have same spacing on L as well, set cardUseCompatPadding to true.
yigit's answer was helpful but didn't have much detail so I'm adding some here for future viewers of this post.
Here is an example of how I got this working (not obvious from linked android site in yigit's answer).
<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_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginLeft="@dimen/activity_vertical_margin"
android:layout_marginRight="@dimen/activity_vertical_margin"
android:layout_marginTop="2dip"
android:layout_marginBottom="2dip"
card_view:cardUseCompatPadding="true">
// Other layouts and views here
</android.support.v7.widget.CardView>
The card_view needing an underscore and to use the res-auto schema wasn't obvious.
There is a good example of this in the Material Design Training.
I had the same problem. I had to create a layout-v21 folder, which will be used by lollipop devices, and duplicate the layout adding the margin/padding I needed to space it out correctly.
That way when your app is launched on lollipop the layout will be read in from the layout-v21 folder with the correct space added, and any other device will go to your standard layout file where the space isn't needed to be explicitly set.
Hope this helps.