How to install add-on for CardView widget in Android L

I use android:paddingLeft and android:paddingTop to install add-ons for the new CardView widget, but it does not work.

I can set margins for all controls inside CardView as a workaround, but it is a pain if there are too many controls.

How to install add-on for a new widget widget?

 <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto" android:id="@+id/card_view" android:layout_gravity="center" android:layout_width="match_parent" android:layout_height="200dp" android:paddingLeft="20dp" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="20dp" android:paddingBottom="@dimen/activity_vertical_margin" card_view:cardCornerRadius="2dp"> <TextView android:id="@+id/info_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Hello World!"/> </android.support.v7.widget.CardView> 
+59
android android-5.0-lollipop padding android-cardview
Jul 22 '14 at 19:31
source share
5 answers

CardView before L-preview uses RoundRectDrawableWithShadow to draw a background that overrides Drawable.getPadding() to add a shadow padding. The background of the view is set through the code after inflation, which overrides any addition specified in XML.

You have two options:

  • Set padding at runtime with View.setPadding() and be careful to adjust the shadows (but only before the L-preview!).
  • Put everything in a layout that indicates padding.

The latter option is the safest.

 <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto" android:id="@+id/card_view" android:layout_gravity="center" android:layout_width="match_parent" android:layout_height="200dp" card_view:cardCornerRadius="2dp"> <FrameLayout android:paddingLeft="20dp" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="20dp" android:paddingBottom="@dimen/activity_vertical_margin"> <TextView android:id="@+id/info_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Hello World!"/> </FrameLayout> </android.support.v7.widget.CardView> 
+57
Jul 22 '14 at 20:59
source share

CardView should handle this using the contentPadding attributes that it comes with :

 <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto" android:id="@+id/card_view" android:layout_gravity="center" android:layout_width="match_parent" android:layout_height="200dp" card_view:cardCornerRadius="2dp" card_view:contentPaddingLeft="20dp" card_view:contentPaddingRight="@dimen/activity_horizontal_margin" card_view:contentPaddingTop="20dp" card_view:contentPaddingBottom="@dimen/activity_vertical_margin"> <TextView android:id="@+id/info_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Hello World!"/> </android.support.v7.widget.CardView> 
+171
Nov 11 '14 at 0:01
source share

If you want to use the CardView add-on on pre-L devices and look the same on Lollipop + devices, you will need to use setUseCompatPadding(true) or the XML option cardUseCompatPadding="true" .

This is due to the fact that "CardView adds an additional add-on for drawing shadows on platforms up to L". [1] Thus, the default implementation has different versions of the API that look different, and the views may not match correctly. Thus, the easiest way to fix this problem is to use the methods mentioned above, or use the fields.

Code example

 <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_context" card_view:cardUseCompatPadding="true" card_view:contentPadding="8dp" card_view:cardCornerRadius="4dp" > ... </android.support.v7.widget.CardView> 

Sources

[1] CardView.setUseCompatPadding (boolean)

[2] android.support.v7.cardview: cardUseCompatPadding

+29
Nov 30 '14 at 16:57
source share

This is what worked for me - putting each element in a frame layout

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="-4dp"> <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:background="@color/white_three" android:orientation="vertical" card_view:cardCornerRadius="2dp" card_view:cardElevation="@dimen/card_elevation" card_view:cardMaxElevation="0dp" card_view:cardPreventCornerOverlap="false" card_view:cardUseCompatPadding="true" </android.support.v7.widget.CardView> 

Double check that card_view " http://schemas.android.com/apk/res-auto " and not tools, and also set negative margins in the frame view to save shadows - works great.

0
Jun 12 '17 at 12:01
source share

I came here to find a solution on how to set padding for CardView in styles.xml because android:padding did not work. (I know that this is not Wat OP asked, but I believe that it is enough)

In styles.xml padding can be set simply with contentPadding

 <style name="myCardViewStyle" parent="CardView"> <item name="contentPadding">20dp</item> 
0
Jun 10 '19 at 12:01
source share



All Articles