Home > Mobile >  How to change the background image of Floating Action Button programatically in Kotlin?
How to change the background image of Floating Action Button programatically in Kotlin?

Time:11-18

I use the following FloatingActionButton and I have set the background image using xml. How can I change the image programmatically in Kotlin? I want to replace the image 'image-color' with 'image-bw'.

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@ id/image_fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/image-color"
    android:scaleType="center"
    app:maxImageSize="56dp"
    app:tint="@null"
    app:layout_anchor="@id/bottom_appbar"
    android:contentDescription="Categories" />

CodePudding user response:

You can use the setImageDrawable function.

imageFab.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.your_drawable))
  • Related