Skip to content
This repository was archived by the owner on Jul 10, 2020. It is now read-only.
/ FABsMenu Public archive

Commit adc2c5d

Browse files
leinardijahirfiquitiva
authored andcommitted
Fix for #27: Exception in API < 20 when loading plus vector (#29)
1 parent d953d82 commit adc2c5d

File tree

3 files changed

+65
-51
lines changed

3 files changed

+65
-51
lines changed

library/src/main/java/jahirfiquitiva/libs/fabsmenu/FABsMenu.java

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import android.support.annotation.NonNull;
4141
import android.support.design.widget.CoordinatorLayout;
4242
import android.support.design.widget.FloatingActionButton;
43+
import android.support.graphics.drawable.VectorDrawableCompat;
4344
import android.support.v4.content.ContextCompat;
4445
import android.support.v7.widget.RecyclerView;
4546
import android.util.AttributeSet;
@@ -67,6 +68,7 @@ public class FABsMenu extends ViewGroup {
6768
public static final int EXPAND_RIGHT = 3;
6869
public static final int LABELS_ON_LEFT_SIDE = 0;
6970
public static final int LABELS_ON_RIGHT_SIDE = 1;
71+
private static final String TAG = FABsMenu.class.getSimpleName();
7072
private static final float COLLAPSED_PLUS_ROTATION = 0f;
7173
private static final float EXPANDED_PLUS_ROTATION = 90f + 45f;
7274
private static Interpolator expandInterpolator = new OvershootInterpolator();
@@ -115,17 +117,16 @@ public FABsMenu(Context context, AttributeSet attrs, int defStyle) {
115117
}
116118

117119
private void init(Context context, AttributeSet attributeSet) {
118-
try {
119-
buttonSpacing = (int) DimensionUtils.convertDpToPixel(16, context);
120-
labelsMargin = getResources().getDimensionPixelSize(R.dimen.fab_labels_margin);
121-
labelsVerticalOffset = (int) DimensionUtils.convertDpToPixel(-1.5f, context);
122-
123-
touchDelegateGroup = new TouchDelegateGroup(this);
124-
setTouchDelegate(touchDelegateGroup);
120+
buttonSpacing = (int) DimensionUtils.convertDpToPixel(16, context);
121+
labelsMargin = getResources().getDimensionPixelSize(R.dimen.fab_labels_margin);
122+
labelsVerticalOffset = (int) DimensionUtils.convertDpToPixel(-1.5f, context);
125123

126-
TypedArray attr = context.obtainStyledAttributes(attributeSet, R.styleable.FABsMenu,
127-
0, 0);
124+
touchDelegateGroup = new TouchDelegateGroup(this);
125+
setTouchDelegate(touchDelegateGroup);
128126

127+
TypedArray attr = context.obtainStyledAttributes(attributeSet, R.styleable.FABsMenu,
128+
0, 0);
129+
try {
129130
menuMargins = attr.getDimensionPixelSize(R.styleable.FABsMenu_fab_menuMargins, 0);
130131

131132
menuTopMargin = attr.getDimensionPixelSize(R.styleable.FABsMenu_fab_menuTopMargin,
@@ -152,7 +153,10 @@ private void init(Context context, AttributeSet attributeSet) {
152153
: (int) DimensionUtils
153154
.convertDpToPixel(16, context));
154155

155-
menuButtonIcon = attr.getDrawable(R.styleable.FABsMenu_fab_moreButtonPlusIcon);
156+
int drawableId = attr.getResourceId(R.styleable.FABsMenu_fab_moreButtonPlusIcon, 0);
157+
if(drawableId != 0){
158+
menuButtonIcon = VectorDrawableCompat.create(getResources(), drawableId, context.getTheme());
159+
}
156160

157161
menuButtonColor = attr.getColor(R.styleable.FABsMenu_fab_moreButtonBackgroundColor,
158162
getColor(android.R.color.holo_blue_dark));
@@ -165,18 +169,20 @@ private void init(Context context, AttributeSet attributeSet) {
165169
expandDirection = attr.getInt(R.styleable.FABsMenu_fab_expandDirection, EXPAND_UP);
166170

167171
labelsPosition = attr.getInt(R.styleable.FABsMenu_fab_labelsPosition,
168-
isRtl() ? LABELS_ON_RIGHT_SIDE : LABELS_ON_LEFT_SIDE);
172+
isRtl() ? LABELS_ON_RIGHT_SIDE : LABELS_ON_LEFT_SIDE);
169173

170-
attr.recycle();
171174

172-
if (menuListener == null) {
173-
setMenuListener(new FABsMenuListener() {
174-
});
175-
}
176-
createMenuButton(context);
177175
} catch (Exception e) {
178-
e.printStackTrace();
176+
Log.w(TAG, "Failure reading attributes", e);
177+
} finally {
178+
attr.recycle();
179+
}
180+
181+
if (menuListener == null) {
182+
setMenuListener(new FABsMenuListener() {
183+
});
179184
}
185+
createMenuButton(context);
180186
}
181187

182188
private boolean expandsHorizontally() {
@@ -235,15 +241,15 @@ public void addAllButtons(TitleFAB... buttons) throws IllegalArgumentException {
235241
public void addButton(TitleFAB button, int index) throws IllegalArgumentException {
236242
if (buttonsCount >= 6)
237243
throw new IllegalArgumentException("A floating action buttons menu should have no " +
238-
"more than six options.");
244+
"more than six options.");
239245
addView(button, index);
240246
buttonsCount += 1;
241247
createLabels();
242248
if (buttonsCount > 1 && getVisibility() != View.VISIBLE) {
243249
show(false);
244250
}
245251
if (buttonsCount < 3)
246-
Log.w("FABsMenu", "A floating action buttons menu should have at least three options");
252+
Log.w(TAG, "A floating action buttons menu should have at least three options");
247253
}
248254

249255
public void addButton(TitleFAB button) throws IllegalArgumentException {
@@ -284,7 +290,7 @@ public void removeButton(TitleFAB button) {
284290
buttonsCount -= 1;
285291
if (buttonsCount <= 1) hide();
286292
} catch (Exception e) {
287-
e.printStackTrace();
293+
Log.e(TAG, "Failure removing Button", e);
288294
}
289295
}
290296

@@ -896,7 +902,7 @@ public void setMenuButtonIcon(@NonNull Uri uri) {
896902
Drawable icon = Drawable.createFromStream(inputStream, uri.toString());
897903
if (icon != null) setMenuButtonIcon(icon);
898904
} catch (Exception e) {
899-
e.printStackTrace();
905+
Log.e(TAG, "Failure setting MenuButton icon", e);
900906
}
901907
}
902908

@@ -907,7 +913,7 @@ public void setMenuButtonIcon(@DrawableRes int resId) {
907913
else throw new NullPointerException(
908914
"The icon you try to assign to FABsMenu does not exist");
909915
} catch (Exception e) {
910-
e.printStackTrace();
916+
Log.e(TAG, "Failure setting MenuButton icon", e);
911917
}
912918
}
913919

@@ -993,7 +999,7 @@ public void setMenuButtonIcon(@NonNull Bitmap bitmap) {
993999
try {
9941000
setMenuButtonIcon(new BitmapDrawable(getResources(), bitmap));
9951001
} catch (Exception e) {
996-
e.printStackTrace();
1002+
Log.e(TAG, "Failure setting MenuButton icon", e);
9971003
}
9981004
}
9991005

@@ -1158,4 +1164,4 @@ public void onAnimationStart(Animator animation) {
11581164
});
11591165
}
11601166
}
1161-
}
1167+
}

library/src/main/java/jahirfiquitiva/libs/fabsmenu/FABsMenuLayout.java

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@
2727
import android.support.annotation.Nullable;
2828
import android.support.design.widget.CoordinatorLayout;
2929
import android.util.AttributeSet;
30+
import android.util.Log;
3031
import android.view.View;
3132
import android.view.ViewGroup;
3233
import android.widget.FrameLayout;
3334

3435
@CoordinatorLayout.DefaultBehavior(FABSnackbarBehavior.class)
3536
public class FABsMenuLayout extends FrameLayout {
3637

38+
private static final String TAG = FABsMenuLayout.class.getSimpleName();
3739
@ColorInt
3840
private int overlayColor;
3941
private View overlayView;
@@ -56,25 +58,27 @@ public FABsMenuLayout(@NonNull Context context, @Nullable AttributeSet attrs, @A
5658
}
5759

5860
private void init(Context context, AttributeSet attrs) {
61+
TypedArray attr = context.getTheme().obtainStyledAttributes(attrs,
62+
R.styleable.FABsMenuLayout, 0,
63+
0);
5964
try {
60-
TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
61-
R.styleable.FABsMenuLayout, 0,
62-
0);
63-
overlayColor = a.getColor(R.styleable.FABsMenuLayout_fabs_menu_overlayColor,
65+
overlayColor = attr.getColor(R.styleable.FABsMenuLayout_fabs_menu_overlayColor,
6466
Color.parseColor("#4d000000"));
65-
clickableOverlay = a.getBoolean(R.styleable.FABsMenuLayout_fabs_menu_clickableOverlay,
67+
clickableOverlay = attr.getBoolean(R.styleable.FABsMenuLayout_fabs_menu_clickableOverlay,
6668
true);
67-
a.recycle();
68-
69-
overlayView = new View(context);
70-
overlayView.setLayoutParams(new FrameLayout.LayoutParams(
71-
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
72-
overlayView.setBackgroundColor(overlayColor);
73-
overlayView.setVisibility(View.GONE);
74-
addView(overlayView);
7569
} catch (Exception e) {
76-
e.printStackTrace();
70+
Log.e(TAG, "Failure setting MenuButton icon", e);
71+
}finally {
72+
attr.recycle();
7773
}
74+
75+
overlayView = new View(context);
76+
overlayView.setLayoutParams(new FrameLayout.LayoutParams(
77+
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
78+
overlayView.setBackgroundColor(overlayColor);
79+
overlayView.setVisibility(View.GONE);
80+
addView(overlayView);
81+
7882
}
7983

8084
@ColorInt
@@ -153,4 +157,4 @@ public void onAnimationEnd(Animator animation) {
153157
}
154158
}).start();
155159
}
156-
}
160+
}

library/src/main/java/jahirfiquitiva/libs/fabsmenu/TitleFAB.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import android.support.v4.view.animation.FastOutLinearInInterpolator;
3131
import android.support.v4.view.animation.LinearOutSlowInInterpolator;
3232
import android.util.AttributeSet;
33+
import android.util.Log;
3334
import android.view.View;
3435
import android.view.animation.Interpolator;
3536

@@ -51,6 +52,7 @@ public class TitleFAB extends FloatingActionButton {
5152
private static final int ANIM_STATE_NONE = 0;
5253
private static final int ANIM_STATE_HIDING = 1;
5354
private static final int ANIM_STATE_SHOWING = 2;
55+
private static final String TAG = TitleFAB.class.getSimpleName();
5456

5557
int mAnimState = ANIM_STATE_NONE;
5658

@@ -80,26 +82,28 @@ public TitleFAB(Context context, AttributeSet attrs, int defStyle) {
8082
}
8183

8284
void init(Context context, AttributeSet attributeSet) {
85+
TypedArray attr =
86+
context.obtainStyledAttributes(attributeSet, R.styleable.TitleFAB, 0, 0);
8387
try {
84-
TypedArray attr =
85-
context.obtainStyledAttributes(attributeSet, R.styleable.TitleFAB, 0, 0);
8688
title = attr.getString(R.styleable.TitleFAB_fab_title);
8789
titleClickEnabled = attr.getBoolean(R.styleable.TitleFAB_fab_enableTitleClick, true);
8890
titleBackgroundColor = attr.getInt(R.styleable.TitleFAB_fab_title_backgroundColor,
89-
ContextCompat
90-
.getColor(context, android.R.color.white));
91+
ContextCompat
92+
.getColor(context, android.R.color.white));
9193
titleTextColor = attr.getInt(R.styleable.TitleFAB_fab_title_textColor,
92-
ContextCompat.getColor(context, android.R.color.black));
94+
ContextCompat.getColor(context, android.R.color.black));
9395
titleCornerRadius =
9496
attr.getDimensionPixelSize(R.styleable.TitleFAB_fab_title_cornerRadius, -1);
9597
titleTextPadding =
9698
attr.getDimensionPixelSize(R.styleable.TitleFAB_fab_title_textPadding,
97-
(int) DimensionUtils.convertDpToPixel(8, context));
98-
setSize(FloatingActionButton.SIZE_MINI);
99+
(int) DimensionUtils.convertDpToPixel(8, context));
100+
} catch (Exception e) {
101+
Log.w(TAG, "Failure reading attributes", e);
102+
} finally {
99103
attr.recycle();
100-
setOnClickListener(null);
101-
} catch (Exception ignored) {
102104
}
105+
setOnClickListener(null);
106+
setSize(FloatingActionButton.SIZE_MINI);
103107
}
104108

105109
@Override
@@ -207,7 +211,7 @@ public void setTitleTextPadding(int titleTextPadding) {
207211
LabelView label = getLabelView();
208212
if (label != null && label.getContent() != null) {
209213
label.getContent().setPadding(titleTextPadding, titleTextPadding / 2, titleTextPadding,
210-
titleTextPadding / 2);
214+
titleTextPadding / 2);
211215
}
212216
}
213217

@@ -348,4 +352,4 @@ private boolean shouldAnimateVisibilityChange() {
348352
return ViewCompat.isLaidOut(this) && !isInEditMode();
349353
}
350354
}
351-
}
355+
}

0 commit comments

Comments
 (0)