Fix invisible FloatingActionButton pre-ICS
The mutate workaround doesn't work all of the time.
This CL fixes it by always creating our own
GradientDrawable for the background
BUG: 23373046
Change-Id: I4b478a23b89d6721ebead62c5bb67505a5737611
diff --git a/design/base/android/support/design/widget/FloatingActionButtonImpl.java b/design/base/android/support/design/widget/FloatingActionButtonImpl.java
index b61bf15..dc7d3af 100644
--- a/design/base/android/support/design/widget/FloatingActionButtonImpl.java
+++ b/design/base/android/support/design/widget/FloatingActionButtonImpl.java
@@ -18,8 +18,10 @@
import android.content.res.ColorStateList;
import android.content.res.Resources;
+import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
+import android.graphics.drawable.GradientDrawable;
import android.support.annotation.Nullable;
import android.support.design.R;
import android.view.View;
@@ -50,7 +52,7 @@
mShadowViewDelegate = shadowViewDelegate;
}
- abstract void setBackgroundDrawable(Drawable originalBackground, ColorStateList backgroundTint,
+ abstract void setBackgroundDrawable(ColorStateList backgroundTint,
PorterDuff.Mode backgroundTintMode, int rippleColor, int borderWidth);
abstract void setBackgroundTintList(ColorStateList tint);
@@ -120,4 +122,11 @@
};
}
}
+
+ GradientDrawable createShapeDrawable() {
+ GradientDrawable d = new GradientDrawable();
+ d.setShape(GradientDrawable.OVAL);
+ d.setColor(Color.WHITE);
+ return d;
+ }
}
diff --git a/design/eclair-mr1/android/support/design/widget/FloatingActionButtonEclairMr1.java b/design/eclair-mr1/android/support/design/widget/FloatingActionButtonEclairMr1.java
index 42e2e9d..253e392 100644
--- a/design/eclair-mr1/android/support/design/widget/FloatingActionButtonEclairMr1.java
+++ b/design/eclair-mr1/android/support/design/widget/FloatingActionButtonEclairMr1.java
@@ -67,22 +67,18 @@
}
@Override
- void setBackgroundDrawable(Drawable originalBackground, ColorStateList backgroundTint,
+ void setBackgroundDrawable(ColorStateList backgroundTint,
PorterDuff.Mode backgroundTintMode, int rippleColor, int borderWidth) {
// Now we need to tint the original background with the tint, using
// an InsetDrawable if we have a border width
- mShapeDrawable = DrawableCompat.wrap(mutateDrawable(originalBackground));
+ mShapeDrawable = DrawableCompat.wrap(createShapeDrawable());
DrawableCompat.setTintList(mShapeDrawable, backgroundTint);
if (backgroundTintMode != null) {
DrawableCompat.setTintMode(mShapeDrawable, backgroundTintMode);
}
// Now we created a mask Drawable which will be used for touch feedback.
- // As we don't know the actual outline of mShapeDrawable, we'll just guess that it's a
- // circle
- GradientDrawable touchFeedbackShape = new GradientDrawable();
- touchFeedbackShape.setShape(GradientDrawable.OVAL);
- touchFeedbackShape.setColor(Color.WHITE);
+ GradientDrawable touchFeedbackShape = createShapeDrawable();
// We'll now wrap that touch feedback mask drawable with a ColorStateList. We do not need
// to inset for any border here as LayerDrawable will nest the padding for us
@@ -112,15 +108,6 @@
updatePadding();
}
- private static Drawable mutateDrawable(Drawable drawable) {
- if (Build.VERSION.SDK_INT < 14 && drawable instanceof GradientDrawable) {
- // GradientDrawable pre-ICS does not copy over it's color when mutated. We just skip
- // the mutate and hope for the best.
- return drawable;
- }
- return drawable.mutate();
- }
-
@Override
void setBackgroundTintList(ColorStateList tint) {
DrawableCompat.setTintList(mShapeDrawable, tint);
diff --git a/design/lollipop/android/support/design/widget/FloatingActionButtonLollipop.java b/design/lollipop/android/support/design/widget/FloatingActionButtonLollipop.java
index b26ef76..49e4b0e 100644
--- a/design/lollipop/android/support/design/widget/FloatingActionButtonLollipop.java
+++ b/design/lollipop/android/support/design/widget/FloatingActionButtonLollipop.java
@@ -47,10 +47,10 @@
}
@Override
- void setBackgroundDrawable(Drawable originalBackground, ColorStateList backgroundTint,
+ void setBackgroundDrawable(ColorStateList backgroundTint,
PorterDuff.Mode backgroundTintMode, int rippleColor, int borderWidth) {
- // Now we need to tint the original background with the tint
- mShapeDrawable = DrawableCompat.wrap(originalBackground.mutate());
+ // Now we need to tint the shape background with the tint
+ mShapeDrawable = DrawableCompat.wrap(createShapeDrawable());
DrawableCompat.setTintList(mShapeDrawable, backgroundTint);
if (backgroundTintMode != null) {
DrawableCompat.setTintMode(mShapeDrawable, backgroundTintMode);
diff --git a/design/res/values/attrs.xml b/design/res/values/attrs.xml
index dd8e64f..0179ec7 100644
--- a/design/res/values/attrs.xml
+++ b/design/res/values/attrs.xml
@@ -18,7 +18,6 @@
<declare-styleable name="FloatingActionButton">
<!-- Background for the FloatingActionButton -->
- <attr name="android:background"/>
<attr name="backgroundTint"/>
<attr name="backgroundTintMode"/>
diff --git a/design/src/android/support/design/widget/FloatingActionButton.java b/design/src/android/support/design/widget/FloatingActionButton.java
index 2edf4d6..dd39bc8 100644
--- a/design/src/android/support/design/widget/FloatingActionButton.java
+++ b/design/src/android/support/design/widget/FloatingActionButton.java
@@ -31,6 +31,7 @@
import android.support.design.widget.FloatingActionButtonImpl.InternalVisibilityChangedListener;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
+import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
@@ -57,6 +58,8 @@
@CoordinatorLayout.DefaultBehavior(FloatingActionButton.Behavior.class)
public class FloatingActionButton extends ImageButton {
+ private static final String LOG_TAG = "FloatingActionButton";
+
/**
* Callback to be invoked when the visibility of a FloatingActionButton changes.
*/
@@ -112,7 +115,6 @@
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.FloatingActionButton, defStyleAttr,
R.style.Widget_Design_FloatingActionButton);
- Drawable background = a.getDrawable(R.styleable.FloatingActionButton_android_background);
mBackgroundTint = a.getColorStateList(R.styleable.FloatingActionButton_backgroundTint);
mBackgroundTintMode = parseTintMode(a.getInt(
R.styleable.FloatingActionButton_backgroundTintMode, -1), null);
@@ -157,8 +159,8 @@
R.dimen.design_fab_content_size);
mContentPadding = (getSizeDimension() - maxContentSize) / 2;
- mImpl.setBackgroundDrawable(background, mBackgroundTint,
- mBackgroundTintMode, mRippleColor, mBorderWidth);
+ mImpl.setBackgroundDrawable(mBackgroundTint, mBackgroundTintMode,
+ mRippleColor, mBorderWidth);
mImpl.setElevation(elevation);
mImpl.setPressedTranslationZ(pressedTranslationZ);
}
@@ -250,11 +252,18 @@
}
@Override
- public void setBackgroundDrawable(@NonNull Drawable background) {
- if (mImpl != null) {
- mImpl.setBackgroundDrawable(
- background, mBackgroundTint, mBackgroundTintMode, mRippleColor, mBorderWidth);
- }
+ public void setBackgroundDrawable(Drawable background) {
+ Log.i(LOG_TAG, "Setting a custom background is not supported.");
+ }
+
+ @Override
+ public void setBackgroundResource(int resid) {
+ Log.i(LOG_TAG, "Setting a custom background is not supported.");
+ }
+
+ @Override
+ public void setBackgroundColor(int color) {
+ Log.i(LOG_TAG, "Setting a custom background is not supported.");
}
/**