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.
To change the color of a Floating Action Button (FAB) in Android, you can use the setBackgroundTintList() method and pass it a ColorStateList. A color state list defines colors 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 (requires API 23+)
ColorStateList colorStateList = new ColorStateList.Builder()
.addState(new int[]{-android.R.attr.state_enabled}, Color.GRAY)
.addState(new int[]{android.R.attr.state_pressed}, Color.BLUE)
.setFallbackColor(Color.BLUE)
.build();
// set the background color of the FAB
fab.setBackgroundTintList(colorStateList);In the example above, the color state list defines gray for the disabled state and blue for the pressed state. When the FAB is enabled and not pressed, it falls back to the color specified in setFallbackColor().
Note: FloatingActionButton requires the AndroidX AppCompat dependency to function correctly.
I hope this helps. Let me know if you have any questions.