question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[ShapeableImageView] Padding is applied to background, unlike normal ImageView

See original GitHub issue

Description: 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. image

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:closed
  • Created 3 years ago
  • Reactions:16
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

10reactions
drewhamiltoncommented, Jul 17, 2020

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 like app: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.

3reactions
theHilikuscommented, Apr 20, 2021

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 image instead, I get image

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to apply spacing between ShapeableImageView and its ...
I was looking for this myself, and just stumbled on contentPadding in the Android documentation. In XML: app:contentPadding="10dp". In code:
Read more >
Deep Dive Into ShapeableImageView in Android - ITNEXT
Deep Dive Into ShapeableImageView in Android. How to Code a Circular ImageView and other shapes easily. Example for different shapes.
Read more >
Working with the ImageView | CodePath Android Cliffnotes
In this guide, we will take a look at how to use an ImageView, how to manipulate bitmaps, learn about the different density...
Read more >
How to Set a Transparent Background of an ImageView on ...
Step 1: Create a New Project in Android Studio · Step 2: Adding images to your drawable folder · Step 3: Working with...
Read more >
ShapeableImageView - Android Developers
An ImageView that draws the bitmap with the provided Shape. ... The padding on the bottom of the View, applied to both the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found