Stack Overflow archive
8 scoreaccepted

Change icon color of FAB based on state w/ compat libs

score
8
question views
1.2K
license
CC BY-SA 3.0

ColorStateList in android:tint was not supported prior to API 21.

See: https://code.google.com/p/android/issues/detail?id=204671


You can use AppCompat's AppCompatResources and support-v4 DrawableCompat to support pre-lollipop. First, remove android:tint="@color/add_button_tint" from your layout. Then set the ColorStateList programmatically:

java
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.search_button);
ColorStateList csl = AppCompatResources.getColorStateList(this, R.color.add_button_tint);
Drawable drawable = DrawableCompat.wrap(fab.getDrawable());
DrawableCompat.setTintList(drawable, csl);
fab.setImageDrawable(drawable);

See How to use setImageTintList() on Android API < 21

Originally posted on Stack Overflow. Public user contributions are licensed under Creative Commons Attribution-ShareAlike.