material-components-android icon indicating copy to clipboard operation
material-components-android copied to clipboard

[Shape] Equivalent of MaterialShape from compose

Open brahmkshatriya opened this issue 5 months ago • 10 comments

I want to use Material 3 shapes, but I couldnt really find any documentation on how create the actual shape sets

The only documentation I found was this

I was able to find it for compose https://developer.android.com/reference/kotlin/androidx/compose/material3/MaterialShapes

I am wondering how to create the same shape sets either through xml or programatically

brahmkshatriya avatar Jul 11 '25 07:07 brahmkshatriya

Hey @brahmkshatriya

We do have a MaterialShapes class that can be used to set your View's background with View.setBackground(MaterialShapes.createShapeDrawable(MaterialShapes.<SHAPE>).

The class is currently set to @RestrictTo(Scope.LIBRARY_GROUP) which is why it's not in the documentation. Let me check with the team why it's restricted.

hunterstich avatar Jul 11 '25 13:07 hunterstich

Thats awesome, I'd definitely like to use those shapes outside of the library

brahmkshatriya avatar Jul 11 '25 19:07 brahmkshatriya

I spoke to the team about this. One of the main concerns is that MaterialShapes are designed to be used with square components - icon buttons, avatars, and other square/circular containers - and we're not sure how to expose them with that restriction in mind. If you applied a fans or gem shape to an Extended FAB, how would those shapes scale to fit a rectangle?

To help us think about if/how to expose this, could you tell us about how you plan to use the shapes?

  • Where in your UI would you use them? As the background of Material Components or in some other part of your UI?
  • Are you planning on using the AndroidX graphics-shapes lib to morph or work with the shapes?

If you want to try using them, you can add @SuppressWarnings("RestrictTo") to your calling method or class to get around the restricted API error. Just keep in mind that the API could change.

hunterstich avatar Jul 11 '25 19:07 hunterstich

Things I would like to do with this:

  1. Use the MaterialShape for masking (ex. Image masked to material shape)
  2. As a background for button, something like this Image
  3. To use to it for creating a M3E SwipeRefreshLayout

brahmkshatriya avatar Jul 11 '25 20:07 brahmkshatriya

IMO, its fine for the shapes to have an aspect ratio of 1:1, so you avoid having to think bout shapes that would not look good in different aspect ratios

brahmkshatriya avatar Jul 11 '25 20:07 brahmkshatriya

Okay sounds good! Thanks for the info.

We're working on exposing the MaterialShapes class and it should be out soon.

hunterstich avatar Jul 14 '25 12:07 hunterstich

Image class Material shspe not work

HanzoDev1375 avatar Jul 19 '25 17:07 HanzoDev1375

class Material shspe not work

loved the part where you sent the entire stack trace

brahmkshatriya avatar Jul 20 '25 07:07 brahmkshatriya

Hey, any updates on this? @pekingme

brahmkshatriya avatar Aug 28 '25 05:08 brahmkshatriya

Yes, this is still on our roadmap. Need some internal discussion about the APIs.

pekingme avatar Aug 28 '25 17:08 pekingme