L. Swifter
L. Swifter

Reputation: 3237

Can not change FloatingActionButton's background color with background and backgroundTint in support design 23.2.0

This is my code:

<android.support.design.widget.FloatingActionButton
        android:id="@+id/enter_floating_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="40dip"
        android:layout_marginRight="10dip"
        android:clickable="true"
        app:background="#00FF00"
        app:backgroundTint="#00FF00"
        app:rippleColor="@android:color/red"
        app:fabSize="normal"
        android:src="@drawable/ic_done" />

As you can see, I set app:background and app:backgroundTint, but these are useless, FloatingActionButton's background color doesn't change, and either the app:rippleColor, when i press the button it's color isn't which I set, looks like the accentColor in the theme.

Why don't these attributes work?

How can I change the FloatingActionButton's background and ripple color?

I had seen this: Android changing Floating Action Button color. Some answers may work in 22, but I didn't find a useful way in 23.

Upvotes: 0

Views: 657

Answers (2)

swatz
swatz

Reputation: 55

Try to add programmatically ripple color in your FloatingActionButton class:

        public RippleView(Context context, AttributeSet attrs, int defStyle) {
                super(context, attrs, defStyle);
                mContext = context;
                init();
                TypedArray a = context.obtainStyledAttributes(attrs,
                        R.styleable.RippleView);
                mRippleColor = a.getColor(R.styleable.RippleView_rippleColor,
                        mRippleColor);
                mAlphaFactor = a.getFloat(R.styleable.RippleView_alphaFactor,
                        mAlphaFactor);
                mHover = a.getBoolean(R.styleable.RippleView_hover, mHover);
                a.recycle();
            }


    public void init() {
            mDensity = getContext().getResources().getDisplayMetrics().density;

            mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            mPaint.setAlpha(20);
          setRippleColor(Color.BLACK, 0.2f);
          // setRippleColor(Color.parseColor(getResources().getColor(R.color.greycolor), 0.2f);
           // setRippleColor(0x000000, 0.1f);
        }

        public void setRippleColor(int rippleColor, float alphaFactor) {
            mRippleColor = rippleColor;
            mAlphaFactor = alphaFactor;
        }

 public void setHover(boolean enabled) {
        mHover = enabled;
    }

Upvotes: 0

srinivas
srinivas

Reputation: 72

Floating action button will be taking the color from colorAccent attribute in styles.xml.Set the color in a style and set that style for this Floating Action Button then it will work

Upvotes: 1

Related Questions