Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change FAB background color when disabled

Tags:

android

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>
like image 632
William Macleod Avatar asked Jul 13 '16 03:07

William Macleod


People also ask

How do you change the color of your fab on Android?

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.

How do you change the background color of a floating action button in flutter?

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.

What is a floating action button?

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.

What is floating action button in flutter?

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.


2 Answers

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.

like image 64
TheAnonymous010 Avatar answered Oct 21 '22 16:10

TheAnonymous010


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
like image 35
Gabriele Mariotti Avatar answered Oct 21 '22 15:10

Gabriele Mariotti