Android changing Floating Action Button color
To change the color of a Floating Action Button (FAB) in Android, you can use the setBackgroundTintList()
method and pass it a color state list. A color state list is a set of colors that defines the colors of the FAB for different states such as enabled, pressed, and disabled.
Here is an example of how to change the color of a FAB in Android:
FloatingActionButton fab = findViewById(R.id.fab);
// create a color state list for the FAB
ColorStateList colorStateList = new ColorStateList(
new int[][]{
new int[]{-android.R.attr.state_enabled}, // disabled
new int[]{android.R.attr.state_pressed} // pressed
},
new int[]{
Color.GRAY, // disabled
Color.BLUE // pressed
}
);
// set the background color of the FAB
fab.setBackgroundTintList(colorStateList);
In the example above, the color state list defines two colors for the FAB: gray for the disabled state and blue for the pressed state. When the FAB is enabled and not pressed, it will use the default color.
I hope this helps. Let me know if you have any questions.