Skip to content

Instantly share code, notes, and snippets.

@marianeum
Last active November 25, 2015 11:24
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save marianeum/9f9114ad2909a4292976 to your computer and use it in GitHub Desktop.
Save marianeum/9f9114ad2909a4292976 to your computer and use it in GitHub Desktop.
FloatingActionButton with custom ripple colours
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="FloatingActionButton">
<attr name="backgroundColor" format="color" />
<attr name="highlightColor" format="color" />
</declare-styleable>
</resources>
<resources>
<dimen name="fab_elevation">3dp</dimen>
<dimen name="fab_press_translation_z">2dp</dimen>
</resources>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:state_pressed="true">
<set>
<objectAnimator android:duration="@android:integer/config_shortAnimTime"
android:propertyName="translationZ"
android:valueTo="@dimen/fab_press_translation_z"
android:valueType="floatType"/>
</set>
</item>
<item>
<set>
<objectAnimator android:duration="@android:integer/config_shortAnimTime"
android:propertyName="translationZ"
android:valueTo="0"
android:valueType="floatType"/>
</set>
</item>
</selector>
package com.marianeum.fab;
import android.animation.AnimatorInflater;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Outline;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.RippleDrawable;
import android.util.AttributeSet;
import android.widget.ImageButton;
import com.marianeum.fab.R;
public class FloatingActionButton extends ImageButton {
private final Outline outline = new Outline();
public FloatingActionButton(Context context) {
super(context);
init(context, null, 0, 0);
}
public FloatingActionButton(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs, 0, 0);
}
public FloatingActionButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr, 0);
}
public FloatingActionButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs, defStyleAttr, defStyleRes);
}
private void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
ColorStateList backgroundColor = null;
ColorStateList highlightColor = null;
if (attrs != null) {
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.FloatingActionButton, defStyleAttr, defStyleRes);
try {
backgroundColor = ta.getColorStateList(R.styleable.FloatingActionButton_backgroundColor);
highlightColor = ta.getColorStateList(R.styleable.FloatingActionButton_highlightColor);
} finally {
ta.recycle();
}
}
if (backgroundColor == null) {
backgroundColor = new ColorStateList(new int[][]{new int[]{0}}, new int[]{getResources().getColor(R.color.accent)});
}
if (highlightColor == null) {
highlightColor = new ColorStateList(new int[][]{new int[]{0}}, new int[]{getResources().getColor(R.color.accent_highlight)});
}
GradientDrawable background = new GradientDrawable();
background.setShape(GradientDrawable.OVAL);
background.setColor(backgroundColor);
RippleDrawable rippleDrawable = new RippleDrawable(background, null);
rippleDrawable.setColor(highlightColor);
setBackground(rippleDrawable);
if (getElevation() == 0) {
setElevation(getResources().getDimension(R.dimen.fab_elevation));
}
if (getStateListAnimator() == null) {
setStateListAnimator(AnimatorInflater.loadStateListAnimator(getContext(), R.anim.fab_elevation));
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
outline.setOval(0, 0, w, h);
setOutline(outline);
}
}
@INRIX-Iurii-Okhmat
Copy link

Dated sample, will not work in 5.0 (without tweaking).

@alanlavintman
Copy link

@INRIX-Iurii-Okhmat can you point out the tweaking?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment