0

I have a shape in my drawables:

circle_item.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="#ffffff" />
    <size
        android:width="120dp"
        android:height="120dp" />
</shape>

Which is used like so:

icon.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/backgroundVw"
        android:layout_width="160dp"
        android:layout_height="160dp"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_gravity="center"
        android:scaleType="fitXY"
        android:background="@drawable/circle_item"/>

    <ImageView
        android:id="@+id/icon"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_gravity="center"
        android:layout_margin="8dp"
        android:src="@drawable/ic_home" />
</FrameLayout>

The image is changed via code like so:

binding.icon.setImageResource(item!!.Image)

Changing the image works perfectly, but I'm struggling to change the actual color property of circle_item through the ImageView

This is what I've tried so far:

binding.backgroundVw.background.colorFilter = BlendModeColorFilter(item!!.backgroundColor, BlendMode.SRC_IN)

But this just gives it a weird color which isn't defined anywhere in the project.

KTOV
  • 559
  • 3
  • 14
  • 39
  • possible duplicate https://stackoverflow.com/questions/17823451/set-android-shape-color-programmatically – Krishan Madushanka Mar 21 '22 at 16:54
  • @KrishanMadushanka Not a duplicate if I'm using Kotlin and the setColorFilter is deprecated... – KTOV Mar 22 '22 at 09:55
  • Does this answer your question? [Set android shape color programmatically](https://stackoverflow.com/questions/17823451/set-android-shape-color-programmatically) – sideshowbarker Apr 06 '22 at 13:48

1 Answers1

0

Try it

imageView.drawable.colorFilter = PorterDuffColorFilter(ContextCompat.getColor(context, R.color.colorBlack), PorterDuff.Mode.SRC_IN)