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.setVisibility(View.VISIBLE);
fab.setImageResource(R.drawable.ic_phone_white_18dp);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fab.setClickable(false);
Timer buttonTimer = new Timer();
buttonTimer.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
fab.setClickable(true);
}
});
}
}, 2000);
I've tried playing around with the StateListDrawable
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"/>
</selector>
To change background color of Floating Action Button in Kotlin Android, set the backgroundTint attribute (in layout file) or backgroundTintList property (in Kotlin file) of FAB with the required color.
To change floating action button background color in Flutter, add a backgroundColor property to the FloatingActionButton widget. Inside the color property, assign the color of your choice.
A floating action button (FAB) is a circular button that triggers the primary action in your app's UI. This page shows you how to add the FAB to your layout, customize some of its appearance, and respond to button taps.
The Floating Action Button is the most unique type of button widget provided in flutter. It is a widget that floats on the screen over other widgets. It appears as a circular icon on the screen with an icon in its center as its child. It is by default placed at the bottom-right corner of the screen.
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.
Just use as app:backgroundTint
a selector as:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="...." android:state_enabled="true"/>
<item android:alpha="..." android:color="...."/>
</selector>
And then just use in your code:
fab.isEnabled = true
fab.isEnabled = false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With