- Hands-on with Material Components for Android: Cards
- Part 6 of a series covering practical usage of Material Components for Android
- Setting up a Material Components theme for Android
- Attribute by attribute
- Basic usage 🏁
- Checking ☑️
- Dragging and swipe-to-dismiss ☝️
- Theming 🎨
- Color
- Typography
- Shape
- Hands-on with Material Components for Android: Cards
- Part 6 of a series covering practical usage of Material Components for Android
- Setting up a Material Components theme for Android
- Attribute by attribute
- Basic usage 🏁
- Checking ☑️
- Dragging and swipe-to-dismiss ☝️
- Theming 🎨
- Color
- Typography
- Shape
- [MaterialCardView] Unwanted border around cardview when having different border radii #911
- Comments
- kelmer44 commented Jan 15, 2020
- pekingme commented Jan 16, 2020
- kelmer44 commented Jan 16, 2020
- kelmer44 commented Jan 16, 2020 •
- pekingme commented Jan 17, 2020
- kelmer44 commented Jan 20, 2020
- ikim24 commented Jan 28, 2020
- Cardview with Recyclerview Android Example [beginners]
- Cardview XML attribute
- Cardview android example with Recyclerview
- Step 1 — Adding dependencies
Hands-on with Material Components for Android: Cards
Part 6 of a series covering practical usage of Material Components for Android
This post will be covering the features and API of Card components. To find out how to handle initial setup of Material Components for Android (including the Gradle dependency and creating an app theme), please see my original post:
Setting up a Material Components theme for Android
Attribute by attribute
Cards are sheets of surface material that contain content and actions related to a single subject. This content may include text, images, links and more, and may vary in size. While there are many patterns and variations in terms of content layout, the only required element is the card container.
Basic usage 🏁
A MaterialCardView can be included in your layout like so:
Checking ☑️
A MaterialCardView can be checked to indicate selection. There is no default behavior for enabling/disabling checked state; this can be done in response to long press, for example.
Firstly, checkable behavior needs to be enabled:
We can then toggle checked state (the below example is in response to a long click):
The UI changes include an overlay color and a checked icon:
The checked icon can be changed using the checkedIcon attribute. The various color options will be discussed in the “Theming” section below.
For an advanced checking sample using RecyclerView selection, see the Material Components for Android catalog.
Dragging and swipe-to-dismiss ☝️
MaterialCardView s have built-in support for state changes specific to dragging. As with checking, no default behavior exists to implement the dragging itself. The recommended way of doing this is with ViewDragHelper and the Material Components for Android catalog includes a handy implementation class; DraggableCoordinatorLayout .
Note: It would be useful if DraggableCoordinatorLayout were included in the core Material Components for Android library to allow it to be used without having to copy the class. At the time of writing, the latest release of Material Components for Android is 1.2.0-alpha06 and this is not the case. You can star the feature request for this on the issue tracker.
Firstly, we need to change the parent container to a DraggableCoordinatorLayout :
We can then add a callback and toggle dragged state:
The UI changes include an overlay color and (animated) increased elevation:
For an advanced dragging sample using RecyclerView drag-and-drop, see the Material Components for Android catalog.
Theming 🎨
Cards can be themed in terms of the three Material Theming subsystems: color, typography and shape. When implementing a global card style, extend the Widget.MaterialComponents.CardView style and reference it in your app theme with the materialCardViewStyle attribute.
Color
The color of the card background can be customized with the cardBackgroundColor attribute. This defaults to colorSurface .
As mentioned above, the foreground color of a card is used to indicate both checked and dragged state. As such, this requires a ColorStateList , meaning a for checked/dragged states is required. It is usually transparent but uses color to indicate checking/dragging, defaulting to colorPrimary (checked)/ colorOnSurface (dragged), with different opacities per state.
The tint color of the checked icon can be customized with the checkedIconTint attribute. This defaults to colorPrimary .
The color of an optional card stroke can be customized with the strokeColor attribute. This is disabled by default.
Lastly, the color of the card touch ripple can be customized with the rippleColor attribute. It too accepts a ColorStateList and defaults to colorOnSurface (unchecked)/ colorPrimary (checked), with different opacities per state.
Typography
There is no primary text as part of card components. Text included in the contents of a card will be styled according to the class/component used as well as the fontFamily app theme attribute.
Shape
The shape of a card background can be customized with the shapeAppearance attribute. This defaults to shapeAppearanceMediumComponent .
The elevation of a card can be customized with the cardElevation attribute. This defaults to 1dp.
Interestingly, the corner size of a card’s current shape appearance can be interpolated between 0.0 and 1.0. This can be useful in animations and transitions. Use MaterialCardView#setProgress and MaterialCardView#getProgress for this.
While not strictly shape theming, it is worth mentioning that the width of an optional card stroke can be adjusted with the strokeWidth attribute. This defaults to 0dp (and will not be rendered if no strokeColor is set, regardless of width).
Источник
Hands-on with Material Components for Android: Cards
Part 6 of a series covering practical usage of Material Components for Android
This post will be covering the features and API of Card components. To find out how to handle initial setup of Material Components for Android (including the Gradle dependency and creating an app theme), please see my original post:
Setting up a Material Components theme for Android
Attribute by attribute
Cards are sheets of surface material that contain content and actions related to a single subject. This content may include text, images, links and more, and may vary in size. While there are many patterns and variations in terms of content layout, the only required element is the card container.
Basic usage 🏁
A MaterialCardView can be included in your layout like so:
Checking ☑️
A MaterialCardView can be checked to indicate selection. There is no default behavior for enabling/disabling checked state; this can be done in response to long press, for example.
Firstly, checkable behavior needs to be enabled:
We can then toggle checked state (the below example is in response to a long click):
The UI changes include an overlay color and a checked icon:
The checked icon can be changed using the checkedIcon attribute. The various color options will be discussed in the “Theming” section below.
For an advanced checking sample using RecyclerView selection, see the Material Components for Android catalog.
Dragging and swipe-to-dismiss ☝️
MaterialCardView s have built-in support for state changes specific to dragging. As with checking, no default behavior exists to implement the dragging itself. The recommended way of doing this is with ViewDragHelper and the Material Components for Android catalog includes a handy implementation class; DraggableCoordinatorLayout .
Note: It would be useful if DraggableCoordinatorLayout were included in the core Material Components for Android library to allow it to be used without having to copy the class. At the time of writing, the latest release of Material Components for Android is 1.2.0-alpha06 and this is not the case. You can star the feature request for this on the issue tracker.
Firstly, we need to change the parent container to a DraggableCoordinatorLayout :
We can then add a callback and toggle dragged state:
The UI changes include an overlay color and (animated) increased elevation:
For an advanced dragging sample using RecyclerView drag-and-drop, see the Material Components for Android catalog.
Theming 🎨
Cards can be themed in terms of the three Material Theming subsystems: color, typography and shape. When implementing a global card style, extend the Widget.MaterialComponents.CardView style and reference it in your app theme with the materialCardViewStyle attribute.
Color
The color of the card background can be customized with the cardBackgroundColor attribute. This defaults to colorSurface .
As mentioned above, the foreground color of a card is used to indicate both checked and dragged state. As such, this requires a ColorStateList , meaning a for checked/dragged states is required. It is usually transparent but uses color to indicate checking/dragging, defaulting to colorPrimary (checked)/ colorOnSurface (dragged), with different opacities per state.
The tint color of the checked icon can be customized with the checkedIconTint attribute. This defaults to colorPrimary .
The color of an optional card stroke can be customized with the strokeColor attribute. This is disabled by default.
Lastly, the color of the card touch ripple can be customized with the rippleColor attribute. It too accepts a ColorStateList and defaults to colorOnSurface (unchecked)/ colorPrimary (checked), with different opacities per state.
Typography
There is no primary text as part of card components. Text included in the contents of a card will be styled according to the class/component used as well as the fontFamily app theme attribute.
Shape
The shape of a card background can be customized with the shapeAppearance attribute. This defaults to shapeAppearanceMediumComponent .
The elevation of a card can be customized with the cardElevation attribute. This defaults to 1dp.
Interestingly, the corner size of a card’s current shape appearance can be interpolated between 0.0 and 1.0. This can be useful in animations and transitions. Use MaterialCardView#setProgress and MaterialCardView#getProgress for this.
While not strictly shape theming, it is worth mentioning that the width of an optional card stroke can be adjusted with the strokeWidth attribute. This defaults to 0dp (and will not be rendered if no strokeColor is set, regardless of width).
Источник
[MaterialCardView] Unwanted border around cardview when having different border radii #911
Comments
kelmer44 commented Jan 15, 2020
Description: When changing the Shape of a Material Card to include only 2 rounded borders, there is an additional
2dp border around the card that clips over content.
Expected behavior: No border would be there. If all borders are rounded/not rounded, this does not happen
Source code:
Android API version: 29
Material Library version: 1.2.0-alpha03
Device: Nexus5, Google Pixel, Samsung S7
The text was updated successfully, but these errors were encountered:
pekingme commented Jan 16, 2020
Hi @kelmer44, thanks for reporting. Could you also provide your layout file?
kelmer44 commented Jan 16, 2020
kelmer44 commented Jan 16, 2020 •
pekingme commented Jan 17, 2020
Hi @kelmer44, thanks for providing the info. After trying your layout and style, I don’t think I fully understand your issue. But here are something I found.
- Apply the ShapeAppearanceOverlay.MyApp.MaterialCardView to app:shapeAppearance instead of style to make it take effect.
- The extra padding inside the card background is to prevent overlapping the content with corner. If this is not desired, you can set app:cardPreventCornerOverlap to false in MaterialCardView to disable it.
kelmer44 commented Jan 20, 2020
This does not fix it, in that case the content extends over the rounded borders. I set up a small github project showcasing the problem:
Click on Open Sheet then enable/disable the red layer to see the problem. You can click on Card Overlap to enable/disable this flag and see the results.
ikim24 commented Jan 28, 2020
@kelmer44 — you’re correct that setting app:cardPreventCornerOverlap to false would cause your content to extend over the rounded borders.
We will clip to the outline (to prevent content from drawing outside the corners) when it is supported for the shape by the framework. (See https://developer.android.com/reference/android/graphics/Outline.html#canClip() «Currently, only Outlines that can be represented as a rectangle, circle, or round rect support clipping.»)
When the framework cannot clip to the outline of the shape, we add padding to ensure all the content is inside the shape (you can turn off this behavior by setting app:cardPreventCornerOverlap to false ).
We do not have plans to support clipping for CardViews beyond what Outline#canClip supports.
Источник
Cardview with Recyclerview Android Example [beginners]
Cardview Android is a new widget for Android, which can be used to display a card sort of a layout in android. Cardview was introduced in Material Design in API level 21 (Android 5.0 i.e Lollipop).
Since, Cardview is part of material design.It’s such a view which has all material design properties, most importantly showing shadows according to the elevation.
The best part about this view is that it extends FrameLayout and it can be displayed on all the platforms of Android since it’s available through the Support v7 library.
The design of the cardview will be looks like,
In the above picture, every boxes made with cardview in android.
Before jumping into the coding part, Let’s see the Cardview XML attribute, that makes the cardview looks beautiful.
Cardview XML attribute
CardView_cardBackgroundColor : ColorStateList: The new ColorStateList to set for the card background
CardView_cardCornerRadius : float: The radius in pixels of the corners of the rectangle shape
CardView_cardElevation : float: The backward compatible elevation in pixels.
CardView_cardMaxElevation : float: The backward compatible maximum elevation in pixels.
CardView_cardPreventCornerOverlap : boolean: Whether CardView should add extra padding to content to avoid overlaps with the CardView corners.
CardView_cardUseCompatPadding : boolean: true> if CardView should add padding for the shadows on platforms Lollipop and above.
CardView_contentPadding : Sets the padding between the Card’s edges and the children of CardView.
CardView_contentPaddingBottom : int: The bottom padding in pixels
CardView_contentPaddingLeft : int: The left padding in pixels
CardView_contentPaddingRight : int: The right padding in pixels
Done with explanation about the android cardview. Let get into the coding part.
Cardview android example with Recyclerview
In this post, I am going to create cardview with recyclerview in android to list the movies with the image and the movie title.
example application demo,
Step 1 — Adding dependencies
In this example, I am using recyclerview with cardview. But I am not going deeper into recyclerview. I am already have a post on recyclerview in android.
Источник