7

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

user3746428
  • 11,047
  • 20
  • 81
  • 137
  • 1
    Use margin instead of padding, take a look at this answer: http://stackoverflow.com/a/34581086 – Daniel Nugent Feb 01 '16 at 00:48
  • I've tried that too unfortunately, and no margin is actually added for some reason. – user3746428 Feb 01 '16 at 01:34
  • Do you have an UI in mind, could you put like screenshot of what you want to achieve? – capt.swag Feb 03 '16 at 08:28
  • @4k3R Sure! I've updated the question. – user3746428 Feb 03 '16 at 08:42
  • Does setting *marginLeft* and *marginRight* to your `CardView` in XML not work? Try [this](http://stackoverflow.com/questions/25632756/cardview-has-lost-margin-when-inflating) answer. – Marko Feb 03 '16 at 08:45
  • 3
    Just a quick question and a suggestion.. why do you need a card view for this.. you can implement this by just creating a drawable xml with rectangle and rounded corners.. and set that drawable as your view's background.. – SureshCS50 Feb 03 '16 at 08:45

1 Answers1

7

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;
    }
}
Jared Rummler
  • 37,824
  • 19
  • 133
  • 148
David Corsalini
  • 7,958
  • 8
  • 41
  • 66