Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the icon tint of a MaterialButton icon programmatically [Kotlin]

I need to change the icon tint of my MaterialButton, the icon is a an xml vector asset and I can change the tint easily in the xml layout, but I need to change the color programmatically in a click, I can't be able to find something related of this issue, This is my button:

<com.google.android.material.button.MaterialButton
                android:id="@+id/btnShowDepartmentList"
                style="@style/com.madison.Button.IconButton"
                app:iconSize="32dp"
                android:padding="0dp"
                android:paddingLeft="5dp"
                android:paddingStart="5dp"
                app:icon="@drawable/ic_list_thumbnails"
                android:layout_width="42dp"
                android:layout_height="42dp"
                app:iconTint="@color/orangeLighter"
                tools:ignore="RtlSymmetry"/> ```
like image 298
leoLR Avatar asked Apr 11 '19 23:04

leoLR


People also ask

How to change icon color kotlin?

To change icon color of Floating Action Button in Kotlin Android we have to set the tint attribute (in layout file) or imageTintList parameter (in Kotlin program) of FAB with the required color.


1 Answers

You will want to use the setIconTint(ColorStateList) or setIconTintResource(Int) methods of MaterialButton. For example:

val button = findViewById<MaterialButton>(R.id.btnShowDepartmentList)
button.setOnClickListener {
    button.setIconTintResource(R.color.orangeLighter)
}
like image 89
Ben P. Avatar answered Oct 28 '22 00:10

Ben P.