William Macleod William Macleod - 3 months ago 41x
Android Question

Change FAB background color when disabled

I'm trying to figure out how I can change the background color of the floating action button when it is disabled for a duration of 2 seconds after being pressed. I would also like it to return to its original color when the 2 second duration is over.

This is the code for the 2 second delay when pressed. This code is in a fragment within the MainActivity.

appBar.setExpanded(true, true);

fab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Timer buttonTimer = new Timer();
buttonTimer.schedule(new TimerTask() {
public void run() {
runOnUiThread(new Runnable() {
public void run() {
}, 2000);

I've tried playing around with the
methods in the documentation but have not come across anything that works.

This is the XML for the color themes of the FAB

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/themeColorPressed" android:state_pressed="true"/>
<item android:color="@color/themeColorPressed" android:state_checked="true"/>
<item android:color="@color/themeColorPressed" android:state_selected="true"/>
<item android:color="@color/themeColorPressed" android:state_enabled="false"/>
<item android:color="@color/themeColor" android:state_enabled="true"/>


Just call fab.setBackgroundColor(Color.GRAY); (or whatever color) when you disable it. Also you can use fab.setBackgroundColor(getResources().getColor(R.color.colorAccent0)); to use a resource color.