Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CardView padding and rounded corners

I am trying to add rounded corners and padding to my card views, corner radius don't seem to work when I have content padding.

This is my current XML:

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/cardView"
    android:layout_width="71dp"
    android:layout_height="39dp"
    card_view:cardElevation="0dp"
    card_view:cardUseCompatPadding="false"
    card_view:cardPreventCornerOverlap="false"
    card_view:cardCornerRadius="7dp"
    card_view:contentPaddingLeft="4dp"
    card_view:contentPaddingRight="4dp">

    <TextView
        android:id="@+id/title"
        android:layout_width="71dp"
        android:layout_height="39dp"
        android:textColor="#ffffff"
        android:background="#FF9400"
        android:gravity="center" />
</android.support.v7.widget.CardView>

If I remove the content padding, then the corner radius works, but I need both.

Anyone have any ideas? I know I can set cardUseCompatPadding to true, but then the entire card has padding which messes with the text view.

EDIT:

Here is the design I currently have, and what I'm replicating:

enter image description here

like image 640
user3746428 Avatar asked Feb 01 '16 00:02

user3746428


1 Answers

If that's an horizontal RecyclerView, add an ItemDecorator to it to have some spacing between objects.

SpaceItemDecorator itemDecorator = new SpacesItemDecorator(16)
mList.addItemDecoration(itemDecorator);

With an SpaceItemDecorator similar to this:

public class SpacesItemDecorator extends RecyclerView.ItemDecoration {

    private final int space;

    public SpacesItemDecorator(int spaceInPx) {
        this.space = spaceInPx;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, 
            RecyclerView.State state) {
        outRect.left = space;
        outRect.right = space;
    }
}
like image 128
David Corsalini Avatar answered Sep 24 '22 23:09

David Corsalini