Change Fab Background Color When Disabled
Solution 1:
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.
Solution 2:
Just use as app:backgroundTint
a selector as:
<selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:color="...."android:state_enabled="true"/><itemandroid:alpha="..."android:color="...."/></selector>
And then just use in your code:
fab.isEnabled = truefab.isEnabled = false
Solution 3:
I just found something that works well...
Firstly I moved the setEnabled() method in the selector XML of the button theme atleast above the color declaration like so.
<?xml version="1.0" encoding="utf-8"?>
<itemandroid:color="@color/themeColorPressed"android:state_pressed="true"/><itemandroid:color="@color/themeColorPressed"android:state_checked="true"/><itemandroid:color="@color/themeColorPressed"android:state_selected="true"/><itemandroid:color="@color/grey"android:state_enabled="false"/><itemandroid:color="@color/themeColor"/>
Setting it initially to false for that respected color it's assigned to.
Then in the Java code, just call the setEnabled method on the button you're targerting (fab in this case) and use a boolean value to dictate whether its activated or deactivated in the onClick method:
fab.setClickable(false);
fab.setEnabled(false);
Timer buttonTimer = newTimer();
buttonTimer.schedule(newTimerTask() {
@Overridepublicvoidrun() {
runOnUiThread(newRunnable() {
@Overridepublicvoidrun() {
fab.setClickable(true);
fab.setEnabled(true);
}
});
}
}, 2000);
Solution 4:
Looking at the implementation of the FloatingActionButton, the fab.setBackgroundColor(int color)
method is not supported.
I managed to change the color of the FAB with the following method (in Kotlin):
privatefuntoggleFabEnabled(enabled: Boolean){
fab.isEnabled = enabled
if (enabled){
fab.backgroundTintList = ColorStateList.valueOf(resources.getColor(R.color.colorAccent, null))
} else {
fab.backgroundTintList = ColorStateList.valueOf(resources.getColor(R.color.disabled, null))
}
}
For API < 23 get the color with the following method resources.getColor(R.color.disabled)
How I handle the different API methods is I create an extension function in Kotlin that handles all APIs.
Post a Comment for "Change Fab Background Color When Disabled"