19

I'm using the new ShapeableImageView to make a circular image, but the overlay is showing up as black. How can I make it transparent? This is what it looks like:

enter image description here

And this is the code:

<com.google.android.material.imageview.ShapeableImageView
        android:id="@+id/app_icon"
        android:layout_width="100dp"
        android:layout_height="100dp"
        app:layout_constraintBottom_toTopOf="@+id/guideline2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:shapeAppearanceOverlay="@style/CircleImageView"
        app:srcCompat="@mipmap/ic_launcher_round" />
<style name="CircleImageView">
        <item name="cornerFamily">rounded</item>
        <item name="cornerSize">50%</item>
    </style>
Cameron
  • 1,281
  • 1
  • 19
  • 40

4 Answers4

30

Just like @Matteo Innnocenti mentioned at the comments, It is just a preview issue when you run the app you should not see the black background

For more people to see I post it as an answer

ysfcyln
  • 2,857
  • 6
  • 34
  • 61
0

Instead of setting the background colour directly, try to set it this way

// instead of this:
// iconImage.setBackgroundResource(item.colorResId)

// use this as workaround
val shapeDrawable = ShapeDrawable(RectShape()).apply {
        colorFilter = PorterDuffColorFilter(ContextCompat.getColor(iconImage.context, item.colorResId), PorterDuff.Mode.SRC_IN)
    }
iconImage.setBackgroundCompat(shapeDrawable)
braintrapp
  • 748
  • 9
  • 11
0

Try to add android:padding="1dp" or app:contentPadding="1dp" on the ShapeableImageView
and check the overlay.
It worked for me, based on
https://github.com/material-components/material-components-android/issues/1329

darrk
  • 51
  • 4
0

Removing android:hardwareAccelerated="false" from manifest solves the problem. See discussion here

Julia B
  • 35
  • 5