Material Design encourages brand expression through shapes. The Material
Components library offers a
shape library that can be used to create
non-standard shapes using
Drawable that can draw
custom shapes while taking shadows, elevation, scale and color into account.
On top of the shape library, the Material Components library provides a mechanism with which to easily customize component shapes across at the theme level. Shape theming offers a new dimension with which to customize the look and feel of your app.
Design & API Documentation
How it works
MaterialShapeDrawable draws itself using a path generated by a
ShapeAppearanceModel is made of
EdgeTreatments that combine to create a custom shape path, and is
generally passed in to a MaterialShapeDrawable’s constructor.
shape library provides some subclassed
EdgeTreatments to allow for easily building new shapes:
EdgeTreatment can be subclassed to create custom
corners and edges.
Note: When subclassing
EdgeTreatment, make sure to
Cloneable interface to ensure that the
can create deep copies of the corner and edge treatments.
Components backed by
MaterialShapeDrawables can be themed across an
Shape Theme Attributes
Theme attributes are provided to be set at the theme level to change the shape values that components read in to set their shapes.
|Attribute Name||Description||Default Value|
||Style reference that contains shape values that are used to style small components||4dp rounded|
||Style reference that contains shape values that are used to style medium components||4dp rounded|
||Style reference that contains shape values that are used to style large components||0dp rounded|
Aside from defining these attributes in your theme, you likely will not need to reference these attributes at all; the widget styles are already mapped to the appropriate theme attribute to create a seamless shape theming experience.
Building a Shape Appearance
Component shapes are backed by “shape appearances”, which are style reference
that define aspects of the shape.
ShapeAppearanceModel reads in the
shapeAppearance style and creates corner and edge treatments out of the
The following attributes can be used in a
|Attribute Name||Format||Description||Supported Values|
||enum||corner family to be used for all four corners||rounded, cut|
||enum||corner family to be used for the top left corner||rounded, cut|
||enum||corner family to be used for the top right corner||rounded, cut|
||enum||corner family to be used for the bottom right corner||rounded, cut|
||enum||corner family to be used for the bottom left corner||rounded, cut|
||dimension||corner size to be used for all four corners||
||dimension||corner size to be used for the top left corner||
||dimension||corner size to be used for the top right corner||
||dimension||corner size to be used for the bottom right corner||
||dimension||corner size to be used for the bottom left corner||
To build a
shapeAppearance, you need at least a
cornerFamily value specified for each corner.
Two attributes are provided to set a component’s shape style,
||Style reference that contains shape values that are used to style the component. Should point to a theme attribute style reference such as
||Style reference that contains shape values that layer on top of a
shapeAppearanceOverlay attribute is provided to override components on a
case by case basis. This attribute stacks on top of the
shapeAppearance is read in first, and then if there is anything
specified in the
shapeAppearanceOverlay attribute, it overrides what’s set in
shapeAppearance styles require both
be set, while
shapeAppearanceOverlay does not. This means that when defining
shapeAppearance style, you should either inherit from a parent if one
exists, or set both
cornerFamily. However, when defining a
style to be used as a
shapeAppearanceOverlay, you should generally set an
empty parent by setting
parent="". This is because
stacks on top of
shapeAppearance, so all values will be set in the
shapeAppearance in the theme
The Material Components library supports theming shapes at the application level. To theme shapes across your app, specify the shape theme attributes in your theme. This will allow components that support shape theming to read in the customized values and change their shapes accordingly.
Let’s say you want to change the corners in your app to cut corners. To accomplish this, define the shape theme attributes to point to custom style references that contain shape values:
<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light"> ... <item name="shapeAppearanceSmallComponent">@style/ShapeAppearance.MyApp.SmallComponent</item> <item name="shapeAppearanceMediumComponent">@style/ShapeAppearance.MyApp.MediumComponent</item> <item name="shapeAppearanceLargeComponent">@style/ShapeAppearance.MyApp.LargeComponent</item> ... </style>
The shape theme attributes should point to custom
shapeAppearance styles that
might look something like this:
<style name="ShapeAppearance.MyApp.SmallComponent" parent="ShapeAppearance.MaterialComponents.SmallComponent"> <item name="cornerFamily">cut</item> <item name="cornerSize">4dp</item> </style> <style name="ShapeAppearance.MyApp.MediumComponent" parent="ShapeAppearance.MaterialComponents.MediumComponent"> <item name="cornerFamily">cut</item> <item name="cornerSize">8dp</item> </style> <style name="ShapeAppearance.MyApp.LargeComponent" parent="ShapeAppearance.MaterialComponents.LargeComponent"> <item name="cornerFamily">rounded</item> <item name="cornerSize">4dp</item> </style>
Material components that support shape theming read in the theme attributes and style themselves according to the themed shape values.
Customizing component shapes
Theme-wide component overrides
You can change a component’s shape across the entire app by overriding the
component and defining a custom
Let’s say you wanted to modify
MaterialCardView so that it uses 16dp rounded
corners acrss the entire app. All you’d have to do is define your own card style
that extends from the widget’s style, and set the relevant attributes to the
desired theme attributes:
<style name="Widget.MyApp.MaterialCardView" parent="Widget.MaterialComponents.MaterialCardView"> <item name="shapeAppearanceOverlay">@style/ShapeAppearanceOverlay.MyApp.MaterialCardView</item> </style>
ShapeAppearanceOverlay.MyApp.MaterialCardView as follows:
<style name="ShapeAppearanceOverlay.MyApp.MaterialCardView"> <item name="cornerFamily">rounded</item> <item name="cornerSize">16dp</item> </style>
Then make sure to define the custom component style in your theme:
<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light"> ... <item name="materialCardViewStyle">@style/Widget.MyApp.MaterialCardView</item> ... </style>
All cards in your app should now have 16dp rounded corners.
Individual component overrides
You can also change an individual component’s shape on a case by case basis.
Let’s say cards in your theme have 16dp rounded corners, but there’s one card
that should have 16dp cut corners. To change that card’s
cornerFamily, you can
shapeAppearanceOverlay attribute on the card in your layout.
Define a custom
shapeAppearanceOverlay style with just the attribute you want
to overlay on top of the existing
<style name="ShapeAppearanceOverlay.MyApp.MaterialCardView.Rounded" parent=""> <item name="cornerFamily">rounded</item> </style>
Then, set the card’s
shapeAppearanceOverlay attribute to the
ShapeAppearanceOverlay style in your layout:
<com.google.android.material.card.MaterialCardView style="@style/Widget.MyApp.MaterialCardView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/mtrl_card_spacing" android:layout_marginTop="@dimen/mtrl_card_spacing" android:layout_marginRight="@dimen/mtrl_card_spacing" app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.MyApp.MaterialCardView"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/card_text"/> </com.google.android.material.card.MaterialCardView>
The card should now have 16dp rounded corners.
The following is a list of Material components that support shape theming.
Components that support shape theming have a
shapeAppearance attribute, a
shapeAppearanceOverlay attribute, and are backed by a