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>
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.
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.
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.
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.
Set parent attribute to CardView. You don't even have to add
Working snippet of code:
<style name="CardViewStyle" parent="CardView">
<item name="cardCornerRadius">4dp</item>
<item name="cardElevation">4dp</item>
</style>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With