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] Rounded corners stroke is cut-off by default

See original GitHub issue

Description: By default, when a ShapeableImageView has rounded corners and you define strokeWidth and strokeColor, it will be cut-off. In the screenshot just below, you can see it compared to TextInputLayout below it, which renders fine.

screenshot_20200526-105209

Expected behavior: Rendering to not appear cut-off by default, without having to increase the padding.

Source code: Extension:

fun ShapeableImageView.roundAllCorners(@Dimension radius: Float) {
    shapeAppearanceModel = shapeAppearanceModel.toBuilder()
        .setAllCorners(CornerFamily.ROUNDED, radius)
        .build()
}

Code:

myImageView.apply {
    roundAllCorners(dp(8f))
    strokeColor = colorSL(R.color.image_border_on_background)
    strokeWidth = dp(2) // Happens with bigger values as well.
    //padding = strokeWidth // Uncommenting this fixes rendering.
    adjustViewBounds = true
}

The dp extensions come from Splitties.

Android API version: 29

Material Library version: 1.2.0-alpha06

Device: Google Pixel 2 XL

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:8
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

9reactions
procmixincommented, Apr 4, 2021

Setting the padding, and adjusting the shape dimensions WTR to the padding, worked for me.

<com.google.android.material.imageview.ShapeableImageView
      android:id="@+id/image"
      android:layout_width="90dp"
      android:layout_height="90dp"
      android:background="@color/my_color"
      android:padding="4dp"
      android:scaleType="centerCrop"
      android:src="@drawable/some_drawable"
      app:contentPadding="20dp"
      app:shapeAppearance="@style/myShapeAppearance"
      app:strokeColor="@color/stroke_color"
      app:strokeWidth="2dp"
      app:tint="@color/image_tint" />

ShapeAppearance

<style name="myShapeAppearance" parent="ShapeAppearance.MaterialComponents.MediumComponent">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">41dp</item>
</style>

This allowed my image to be drawn without the box clipping effect

0reactions
drchencommented, Feb 18, 2022

There seems like no general solutions for all possible cases.

I’ll close this as infeasible for now. Please feel free to reopen it or create a PR if you have a good idea to solve this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ShapeableImageView's stroke is getting cut by borders
I think it is supposed to just clip its image resource to the specified shape. <com.google.android.material.imageview.ShapeableImageView android ...
Read more >
Deep Dive Into ShapeableImageView in Android - ITNEXT
As before will use the same style we created for the rounded corner Image and will add strokeWidth, and strokeColor to the ShapeableImageView....
Read more >
ShapeableImageView - Android Developers
Gets the stroke width for this ImageView. void, setContentPadding(int left, int top, int right, int bottom). Set additional padding on the image ...
Read more >
Cardview Is Cut At The Sides - ADocLib
[ShapeableImageView] Rounded corners stroke is cutoff by default #1329 of the same width as the stroke would push the borders so the image...
Read more >
ShapeableImageView – Material components for android
For the corner cut imageview, we are going to use cornerFamily . by default, cornerFamily will be rounded. In our case, we need...
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