0

I need to change the background color of my FloatingActionButton when clicked during runtime. My code:

public static void setButtonTint(FloatingActionButton button, ColorStateList tint) {
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) {
            ((TintableBackgroundView) button).setSupportBackgroundTintList(tint);
        } else {
            ViewCompat.setBackgroundTintList(button, tint);
        }
}

public void switchFABmode(){
        switch (floatingActionButtonMode){
            case 0:
                setButtonTint(shareFAB,getResources().getColorStateList(R.color.colorGrey));
                setButtonTint(deleteFAB,getResources().getColorStateList(R.color.colorPrimary));
                floatingActionButtonMode = 1;
                Toast.makeText(this, "Tap time to delete",Toast.LENGTH_SHORT).show();
                break;
            case 1:
                shareFAB.setBackgroundColor(ContextCompat.getColor(this, R.color.colorPrimary));
                deleteFAB.setBackgroundColor(ContextCompat.getColor(this, R.color.colorGrey));
                Toast.makeText(this,"Tap time to share", Toast.LENGTH_SHORT).show();
                floatingActionButtonMode = 0;
                break;
        }
}

Does not accomplish that. Can someone point out my mistake, or provide me with a solution?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62

2 Answers2

0

Use this:

button.setBackgroundColor(ContextCompat.getColor(this, R.color.colorGrey));

More from here

Jeel Vankhede
  • 11,592
  • 2
  • 28
  • 58
0

Ok, I insert you code in my test project (with little changes), and it's work ok.

FloatingActionButton fab;
int floatingActionButtonMode = 0;

public static void setButtonTint(FloatingActionButton button, ColorStateList tint) {
    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) {
        ((TintableBackgroundView) button).setSupportBackgroundTintList(tint);
    } else {
        ViewCompat.setBackgroundTintList(button, tint);
    }
}

public void switchFABmode(){
    switch (floatingActionButtonMode){
        case 0:
            setButtonTint(fab,getResources().getColorStateList(R.color.colorPrimary));
            floatingActionButtonMode = 1;
            Toast.makeText(getActivity(), "Tap time to delete",Toast.LENGTH_SHORT).show();
            break;
        case 1:
            setButtonTint(fab,getResources().getColorStateList(R.color.colorAccent));
            Toast.makeText(getActivity(),"Tap time to share", Toast.LENGTH_SHORT).show();
            floatingActionButtonMode = 0;
            break;
    }
}

and in fragment's onCreateView:

 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    final View v = inflater.inflate(R.layout.map, container, false);
    fab = v.findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switchFABmode();
        }
    });
return v;
}
igor_rb
  • 1,821
  • 1
  • 19
  • 34
  • Thanks, but its still not changing its color, i guess my mistake lies somewhere else... Its weird though because the Toast and everything else works fine, its just the color that stays no matter what i do:( –  Oct 04 '18 at 09:22