[ShapeableImageView] Padding is applied to background, unlike normal ImageView
See original GitHub issueDescription: In ShapeableImageView, android:padding
gets applied to both the image itself and the shaped background. This is unusual compared to most views (though maybe typical for Shapeables, not sure).
Expected behavior:
What the normal ImageView does: pads the image, but not the background.
Top: ShapeableImageView with padding. Bottom: normal ImageView with padding.
It seems like the current behavior should be achieved by margin rather than padding, and that padding should leave the background alone, like it does in most Views.
Source code:
<ShapeableImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:background="?colorSecondary"
android:padding="8dp"
app:shapeAppearance="?shapeAppearanceSmall"
app:srcCompat="@drawable/my_icon" />
Android API version: 21-29
Material Library version: 1.2.0-rc01
Device: Pixel 3 emulator
Looking at the ShapeableImageView source, it appears to be clear where the shape is set to honor the padding. I’d like to know if the current behavior is expected for some reason. If it is, I’d like to know if the behavior I want can be accommodated by some new attribute or something.
If there’s a clear path forward I’d be happy to open a PR. And if that PR could potentially land in 1.2, I’d be happy to open a PR quickly. 😃 Let me know!
Issue Analytics
- State:
- Created 3 years ago
- Reactions:16
- Comments:9 (7 by maintainers)
Hmm. That’s unfortunate. Would it not make sense to pad the background by half the stroke width automatically?
I’d argue that CardView was always a little “weird” about padding like this, but (Shapeable)ImageView has an existing behavior that it should aim to uphold. For this reason I would prefer that
android:padding
affect only the image while some custom attribute likeapp:backgroundPadding
be created for the background.But if consistency with MaterialCardView is more important than consistency with ImageView, can I offer a PR with the inverse? I.e. leave padding as is and add some
app:imageInset
attributes?My goal is to ship some styles that match specific design guidelines without requiring a custom view, so the inset drawable solution is undesirable.
is there a workaround for this in the meantime? i’m using material:material:1.3.0 and i’m trying to have the image be padded so that the circle around the image is not touching the image, like so
instead, I get

I’m not sure how to use the
inset
suggestion from @ymarian