Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added new fab_size property for custom size requirements #127

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ dependencies {
}
```

**2)** Add the ``com.melnykov.fab.FloatingActionButton`` to your layout XML file. The button should be placed in the bottom right corner of the screen. The width and height of the floating action button are hardcoded to **56dp** for the normal and **40dp** for the mini button as specified in the [guidelines].
**2)** Add the ``com.melnykov.fab.FloatingActionButton`` to your layout XML file. The button should be placed in the bottom right corner of the screen. The width and height of the floating action button are hardcoded to **56dp** for the normal and **40dp** for the mini button as specified in the [guidelines], but can also be overridden manually via the ``fab_size`` dimension attribute, if this special case is required.

```xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
Expand Down
33 changes: 27 additions & 6 deletions library/src/main/java/com/melnykov/fab/FloatingActionButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

public class FloatingActionButton extends ImageButton {
private static final int TRANSLATE_DURATION_MILLIS = 200;
private static final float SHADOW_SIZE_RELATIVE = 0.2f; // shadow padding relative to width / height, based on fab_shadow & fab_shadow_mini drawables.

@Retention(RetentionPolicy.SOURCE)
@IntDef({TYPE_NORMAL, TYPE_MINI})
Expand All @@ -54,6 +55,7 @@ public class FloatingActionButton extends ImageButton {
private int mColorDisabled;
private boolean mShadow;
private int mType;
private int mSize;

private int mShadowSize;

Expand All @@ -80,8 +82,7 @@ public FloatingActionButton(Context context, AttributeSet attrs, int defStyle) {
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int size = getDimension(
mType == TYPE_NORMAL ? R.dimen.fab_size_normal : R.dimen.fab_size_mini);
int size = mSize;
if (mShadow && !hasLollipopApi()) {
size += mShadowSize * 2;
setMarginsWithoutShadow();
Expand All @@ -97,11 +98,15 @@ private void init(Context context, AttributeSet attributeSet) {
mColorDisabled = getColor(android.R.color.darker_gray);
mType = TYPE_NORMAL;
mShadow = true;
mSize = -1;
mScrollThreshold = getResources().getDimensionPixelOffset(R.dimen.fab_scroll_threshold);
mShadowSize = getDimension(R.dimen.fab_shadow_size);

if (attributeSet != null) {
initAttributes(context, attributeSet);
}

calculateSize();
calculateShadowSize();
updateBackground();
}

Expand All @@ -119,12 +124,30 @@ private void initAttributes(Context context, AttributeSet attributeSet) {
mColorDisabled);
mShadow = attr.getBoolean(R.styleable.FloatingActionButton_fab_shadow, true);
mType = attr.getInt(R.styleable.FloatingActionButton_fab_type, TYPE_NORMAL);

// TYPE_MINI overrides any custom size
if(mType == TYPE_NORMAL) {
mSize = attr.getDimensionPixelSize(R.styleable.FloatingActionButton_fab_size, mSize);
}
} finally {
attr.recycle();
}
}
}

private void calculateSize() {
mSize = mSize == -1 ? getDefaultSize() : mSize;
}

private int getDefaultSize() {
return getDimension(mType == TYPE_NORMAL ? R.dimen.fab_size_normal : R.dimen.fab_size_mini);
}

private void calculateShadowSize() {
// custom shadow size for TYPE_NORMAL only, TYPE_MINI gets default shadow size
mShadowSize = (mType == TYPE_NORMAL) ? (int)(SHADOW_SIZE_RELATIVE * mSize) : getDimension(R.dimen.fab_shadow_size);
}

private void updateBackground() {
StateListDrawable drawable = new StateListDrawable();
drawable.addState(new int[]{android.R.attr.state_pressed}, createDrawable(mColorPressed));
Expand Down Expand Up @@ -194,9 +217,7 @@ private void setBackgroundCompat(Drawable drawable) {
setOutlineProvider(new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
int size = getDimension(mType == TYPE_NORMAL ? R.dimen.fab_size_normal
: R.dimen.fab_size_mini);
outline.setOval(0, 0, size, size);
outline.setOval(0, 0, mSize, mSize);
}
});
setClipToOutline(true);
Expand Down
1 change: 1 addition & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<attr name="fab_colorRipple" format="color" />
<attr name="fab_colorDisabled" format="color" />
<attr name="fab_shadow" format="boolean" />
<attr name="fab_size" format="dimension" />
<attr name="fab_type" format="enum">
<enum name="normal" value="0" />
<enum name="mini" value="1" />
Expand Down