Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set CardView attributes in style.xml file in android

I have set a style for my CardView in style.xml file but its giving me error on compile time for app:cardCornerRadius, app:cardElevation, app:cardPreventCornerOverlap and app:contentPadding attributes. What is the correct way to set style for a CardView in Android?

Below is some of my code:

<style name="CardViewStyle" parent="CardView">
    <item name="android:layout_marginBottom">@dimen/cardMarginVertical</item>
    <item name="android:layout_marginTop">@dimen/cardMarginVertical</item>
    <item name="android:layout_marginLeft">@dimen/cardMarginHorizontal</item>
    <item name="android:layout_marginRight">@dimen/cardMarginHorizontal</item>
    <item name="app:cardCornerRadius">2dp</item>
    <item name="app:cardElevation">2dp</item>
    <item name="app:cardPreventCornerOverlap">false</item>
    <item name="app:contentPadding">0dp</item>
    <item name="android:layout_width">match_parent</item>
</style>
like image 529
user3391170 Avatar asked Jul 04 '15 14:07

user3391170


People also ask

How do I customize my CardView?

Customized CardView First, add a CardView dependency to the application-level build. gradle file. Then create a drawable background for the cards. For that, create a new drawable resource file inside the drawable folder.

What is CardView in Android with example?

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.

Where is the style XML?

Defining Styles This XML file resides under res/values/ directory of your project and will have <resources> as the root node which is mandatory for the style file. The name of the XML file is arbitrary, but it must use the . xml extension.

How do I create a style folder in Android?

You have to create a value folder with values-v21 caption. and create style. xml file in this with the same name as values folder like styles. xml.


1 Answers

Set parent attribute to CardView. You don't even have to add

  • app: qualifier
  • xmlns:card_view="http://schemas.android.com/apk/res-auto". is not required

Working snippet of code:

<style name="CardViewStyle" parent="CardView">
 <item name="cardCornerRadius">4dp</item>
 <item name="cardElevation">4dp</item>
</style>
  • similar ques
  • How to put a CardView attribute in a style?
like image 139
Abhishek Avatar answered Sep 28 '22 07:09

Abhishek