Reputation: 487
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"/> ```
Upvotes: 13
Views: 15289
Reputation: 54214
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)
}
Upvotes: 12
Reputation: 487
This works for me:
btnShowDepartmentThumbnails.setOnClickListener {
btnShowDepartmentThumbnails.setIconTintResource(R.color.orangeLighter)
}
My error was tried to set icon tint, with this way:
btnShowDepartmentThumbnails.setOnClickListener {
it.setIconTintResource(R.color.orangeLighter)
}
Upvotes: 2
Reputation: 281
You will need to pass the ColorStateList to the iconTint programmatically.
btnShowDepartmentList.iconTint = ContextCompat.getColorStateList(activity, R.color.orangeLighter)
Upvotes: 3