How to Change Background Color of Floating Action Button in Android?
Kotlin Android – Change Background Color of Floating Action Button
To change background color of Floating Action Button in Kotlin Android, set the backgroundTint attribute (in layout file) or backgroundTintList property (in Kotlin file) of FAB with the required color.
To change the background color of Floating Action Button in layout file, set the app:backgroundTint attribute with the required color value as shown in the following code snippet.
To change the background color of Floating Action Button dynamically or programmatically in Kotlin activity file, set the backgroundTintList parameter of the FAB with the required color value as shown in the following code snippet.
Example – Change Background Color of FAB via Layout File
Create an Android Application with Empty Activity and modify the activity_main.xml with the following code.
activity_main.xml
Run this Android Application, and we would get the output as shown in the following screenshot, with the background of Floating Action Button (FAB) changed to the given color value of #FF6CBEFF .
Example – Change Background Color of FAB Programmatically
Create an Android Application with Empty Activity and modify the activity_main.xml and MainActivity.kt with the following code.
activity_main.xml
MainActivity.kt
Run this Android Application, and we would get the output as shown in the following screenshot, with the background of Floating Action Button (FAB) changed to the given color value of Color.rgb(255, 50, 50) .
Conclusion
In this Kotlin Android Tutorial, we learned how to change the background color of Floating Action Button (FAB) widget via layout file or programmatically in Kotlin Activity file, with examples.
Источник