Search code examples
androidxmlkotlinmaterial-designfloating-action-button

How to change the color of a Material Floating Action Button while being pressed?


How can I change the color of the FAB while the button is being pressed? I don't see a property like onPressed().


Solution

  • You can use the app:rippleColor in your layour or the rippleColor attribute in the FAB style.

    Where fab_ripple should be a selector.

    Something like:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
          <item android:alpha="..." android:color="?attr/colorOnSecondary" android:state_pressed="true"/>
          <item android:alpha="..." android:color="?attr/colorOnSecondary" android:state_focused="true"/>
          <item android:alpha="..." android:color="?attr/colorOnSecondary" android:state_hovered="true"/>
          <item android:alpha="..." android:color="?attr/colorOnSecondary"/>
    
    </selector>