Christopher
Christopher

Reputation: 10269

Android support Cardview contentPadding not working

I would like to add a contentPadding to my support cardview. This padding is calculated on 4.4 devices correctly, but on a 4.1.2 device the contentpadding is not working.

build.gradle:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile 'com.android.support:cardview-v7:21.0.3'
}

xml-layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MainActivity">

<android.support.v7.widget.CardView
    android:id="@+id/card"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    card_view:cardCornerRadius="4dp"
    card_view:contentPaddingLeft="32dp"
    card_view:contentPaddingRight="32dp"
    card_view:contentPaddingTop="32dp"
    card_view:contentPaddingBottom="32dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="This is a very long text with more blablablablablablablablabla bla bla bla blablablablabla bla blablablablablabla blablablablabla blablablablabla blablabla"/>

    </LinearLayout>

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

</RelativeLayout>

The result on device running 4.1.2: Android 4.1.2 - Sony Xperia L

The contentPadding is not recognized.

Same app running a 4.4.2 device: Android 4.4.2 - Samsung Galaxy S4

The contentPadding is recognized correctly.

I found a workaround for this issue on this thread: How to set the padding for CardView widget in Android L But I would like to know, if someone else is facing this issue or could provide a better solution?

EDIT: Currently it seems to be only a bug on Sony XPERIA L device running Android 4.1.2. I tested with a Galaxy Nexus running 4.1.2 and in this case everything looks nice.

Upvotes: 4

Views: 3506

Answers (1)

smbd uknow
smbd uknow

Reputation: 2282

Use setUseCompatPadding(true), or the XML parameter app:cardUseCompatPadding="true"

Upvotes: 2

Related Questions