Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the padding for CardView widget in Android L

I'm using android:paddingLeft and android:paddingTop to set the padding for the new CardView widget but it doesn't work.

I can set the margin for all the controls inside the CardView as a workaround but that's a pain if there are too many controls.

How to set padding for the new cardview 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> 
like image 713
nomongo Avatar asked Jul 22 '14 19:07

nomongo


People also ask

How do I add padding to CardView?

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

How does CardView work in Android Studio?

CardView is a new widget in Android that can be used to display any sort of data by providing a rounded corner layout along with a specific elevation. CardView is the view that can display views on top of each other. The main usage of CardView is that it helps to give a rich feel and look to the UI design.

What is card elevation in Android Studio?

interface CardElevation. Represents the elevation for a card in different states. See CardDefaults. cardElevation for the default elevation used in a Card .


1 Answers

CardView should handle this using the contentPadding attributes 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> 
like image 133
yarian Avatar answered Oct 21 '22 13:10

yarian