Reputation: 7525
I am using CardView
as custom item for RecyclerView
. They looks good on Android 5+ but so different on older Android versions.
The code is the same:
<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:minHeight="?android:attr/listPreferredItemHeight"
card_view:cardCornerRadius="1dp"
card_view:cardElevation="1dp">
... other items ...
</android.support.v7.widget.CardView>
Is there a way to achieve the Android 5+ behavior on pre-Lollipop devices?
Upvotes: 1
Views: 966
Reputation: 16537
Using the support CardView? No.
Personally I think that the support CardView is broken and shouldn't be used at all. It looks and works a little bit different on Lollipop and on older systems. The shadow is different, the padding is different, content clipping doesn't work on pre-Lollipop devices, etc. The API is also weird and confusing. That's why it's hard to get good results on all platforms. If you can live without cards, I would go that way.
Of course it's possible to create a custom, nice-looking, backwards-compatible card, but it's a bit complex task. To create a card on your own you have to implement:
drawChild(...)
in a parent container, where I could draw shadows around cards freely. Shadow generation method doesn't matter - it could be a gradient, a static 9-patch or a RenderScript-blurred black shape.I was frustrated by the look and the API of CardView as well, so I created my own implementation. It can be found on GitHub - the library is called Carbon and using it is probably the easiest way to get a decent card. After importing the library simply add style="?attr/carbon_cardViewStyle"
to any layout to make it look like a card:
<carbon.widget.RelativeLayout
style="?attr/carbon_cardViewStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Upvotes: 1