Why is the FloatingActionButton transparent?

2

I have a problem after I updated lib support.design:22.2.0 to 23.0.1, floatingActionButton became transparent when run in API 10.

Here is the code:

<android.support.design.widget.FloatingActionButton
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    app:borderWidth="0dp"
    android:src="@drawable/ic_mode_edit_white_24dp"
    app:layout_anchor="@id/appBar"
    app:layout_anchorGravity="bottom|right|end"
    android:elevation="2dp"
    android:id="@+id/btnClick"
    app:fabSize="normal"
    android:backgroundTint="@color/colorFAB"
    android:layout_gravity="right|bottom" />  

    
asked by anonymous 11.10.2015 / 03:51

1 answer

1

I think this is an Android 10 bug that was fixed in version 14, at issue # 183315 but that appeared during version 23.0.0 of the design library.

There are two solutions, one simple and one that depends on an overwriting the behavior of FAB .

1) Back to version 22.2.1.

Sometimes this is common (unfortunately). If there is a bug in the library that has no workaround, simply go back if you do not use any version-specific functions.

2) Create a class that inherits from the FloatActionButton of the design library as a "workaround" suggested by the comment # 9 :

public class TintFloatingActionButton extends FloatingActionButton implements TintableBackgroundView {

    static final int[] PRESSED_ENABLED_STATE_SET = {android.R.attr.state_pressed,
            android.R.attr.state_enabled};
    static final int[] FOCUSED_ENABLED_STATE_SET = {android.R.attr.state_focused,
            android.R.attr.state_enabled};

    private static final int[] TINT_ATTRS = {
            android.R.attr.background
    };

    private TintInfo mBackgroundTint;

    public TintFloatingActionButton(Context context) {
        super(context);
    }

    public TintFloatingActionButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TintFloatingActionButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        if (TintManager.SHOULD_BE_USED) {
            TintTypedArray a = TintTypedArray.obtainStyledAttributes(getContext(), attrs,
                    TINT_ATTRS, defStyleAttr, 0);

            if (a.hasValue(0)) {
                setSupportBackgroundTintList(createColorStateList(a.getResourceId(0, -1)));
            }

            a.recycle();
        }
    }

    private static ColorStateList createColorStateList(int selectedColor) {
        final int[][] states = new int[3][];
        final int[] colors = new int[3];
        int i = 0;

        states[i] = FOCUSED_ENABLED_STATE_SET;
        colors[i] = selectedColor;
        i++;

        states[i] = PRESSED_ENABLED_STATE_SET;
        colors[i] = selectedColor;
        i++;

        // Default enabled state
        states[i] = new int[0];
        colors[i] = Color.TRANSPARENT;
        i++;

        return new ColorStateList(states, colors);
    }

    @Override
    public void setSupportBackgroundTintList(ColorStateList tint) {
        if (mBackgroundTint == null) {
            mBackgroundTint = new TintInfo();
        }
        mBackgroundTint.mTintList = tint;
        mBackgroundTint.mHasTintList = tint != null;
        applySupportBackgroundTint();
    }

    @Nullable
    @Override
    public ColorStateList getSupportBackgroundTintList() {
        return mBackgroundTint != null ? mBackgroundTint.mTintList : null;
    }

    @Override
    public void setSupportBackgroundTintMode(@Nullable PorterDuff.Mode tintMode) {
        if (mBackgroundTint == null) {
            mBackgroundTint = new TintInfo();
        }
        mBackgroundTint.mTintMode = tintMode;
        mBackgroundTint.mHasTintMode = tintMode != null;
        applySupportBackgroundTint();
    }

    @Nullable
    @Override
    public PorterDuff.Mode getSupportBackgroundTintMode() {
        return mBackgroundTint != null ? mBackgroundTint.mTintMode : null;
    }

    private void applySupportBackgroundTint() {
        if (getBackground() != null && mBackgroundTint != null) {
            TintManager.tintViewBackground(this, mBackgroundTint);
        }
    }
}

Complete code in this gist: link

    
13.10.2015 / 00:08