Skip to content

Commit 1b956ce

Browse files
committed
Fix #122: Initial work for fan expansion mode
- Update sample app to show FAN expansion mode Change FabWithLabelView to ConstraintLayout. - This allows both horizontal and vertical expansion modes to show labels Set layout params for each menu item to mimic expansion mode for top/bottom/left and right Expose hiding of label from FabWithLabelView - For FAN expansion mode, the text is displayed below mini fab - for right or left expansion modes, the text is always disabled. Same as existing behaviour Update to androidx dependencies and imports Format code according to code style, increase demo app fan radius size
1 parent dc7396d commit 1b956ce

File tree

10 files changed

+261
-68
lines changed

10 files changed

+261
-68
lines changed

library/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,6 @@ dependencies {
5151
api "androidx.appcompat:appcompat:$versions.androidx_appcompat"
5252
api "com.google.android.material:material:$versions.android_material"
5353
api "androidx.cardview:cardview:$versions.androidx_cardview"
54+
api "androidx.constraintlayout:constraintlayout:$versions.androidx_constraintlayout"
5455
annotationProcessor "com.uber.nullaway:nullaway:$versions.nullaway"
5556
}

library/src/main/java/com/leinardi/android/speeddial/FabWithLabelView.java

Lines changed: 60 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,31 +26,34 @@
2626
import android.text.TextUtils;
2727
import android.util.AttributeSet;
2828
import android.util.Log;
29-
import android.view.Gravity;
3029
import android.view.View;
3130
import android.view.ViewGroup;
32-
import android.widget.LinearLayout;
3331
import android.widget.TextView;
3432

3533
import androidx.annotation.ColorInt;
3634
import androidx.annotation.DrawableRes;
35+
import androidx.annotation.IntDef;
3736
import androidx.annotation.Nullable;
3837
import androidx.cardview.widget.CardView;
38+
import androidx.constraintlayout.widget.ConstraintLayout;
3939
import androidx.core.content.res.ResourcesCompat;
4040
import androidx.core.graphics.drawable.DrawableCompat;
4141
import com.google.android.material.floatingactionbutton.FloatingActionButton;
4242
import com.leinardi.android.speeddial.SpeedDialView.OnActionSelectedListener;
4343

44+
import java.lang.annotation.Retention;
45+
4446
import static com.google.android.material.floatingactionbutton.FloatingActionButton.SIZE_AUTO;
4547
import static com.google.android.material.floatingactionbutton.FloatingActionButton.SIZE_MINI;
4648
import static com.google.android.material.floatingactionbutton.FloatingActionButton.SIZE_NORMAL;
4749
import static com.leinardi.android.speeddial.SpeedDialActionItem.RESOURCE_NOT_SET;
50+
import static java.lang.annotation.RetentionPolicy.SOURCE;
4851

4952
/**
5053
* View that contains fab button and its label.
5154
*/
5255
@SuppressWarnings({"unused", "WeakerAccess"})
53-
public class FabWithLabelView extends LinearLayout {
56+
public class FabWithLabelView extends ConstraintLayout {
5457
private static final String TAG = FabWithLabelView.class.getSimpleName();
5558

5659
private TextView mLabelTextView;
@@ -66,6 +69,15 @@ public class FabWithLabelView extends LinearLayout {
6669
private float mLabelCardViewElevation;
6770
@Nullable
6871
private Drawable mLabelCardViewBackground;
72+
@Orientation
73+
private int mOrientation = Orientation.HORIZONTAL;
74+
75+
@Retention(SOURCE)
76+
@IntDef({Orientation.HORIZONTAL, Orientation.VERTICAL})
77+
public @interface Orientation {
78+
int HORIZONTAL = 0;
79+
int VERTICAL = 1;
80+
}
6981

7082
public FabWithLabelView(Context context) {
7183
super(context);
@@ -92,15 +104,10 @@ public void setVisibility(int visibility) {
92104
}
93105
}
94106

95-
@Override
96-
public void setOrientation(int orientation) {
97-
super.setOrientation(orientation);
107+
public void setOrientation(@Orientation int orientation) {
108+
mOrientation = orientation;
98109
setFabSize(mCurrentFabSize);
99-
if (orientation == VERTICAL) {
100-
setLabelEnabled(false);
101-
} else {
102-
setLabel(mLabelTextView.getText().toString());
103-
}
110+
setLabel(mLabelTextView.getText().toString());
104111
}
105112

106113
/**
@@ -113,9 +120,9 @@ public boolean isLabelEnabled() {
113120
/**
114121
* Enables or disables label of button.
115122
*/
116-
private void setLabelEnabled(boolean enabled) {
117-
mIsLabelEnabled = enabled;
118-
mLabelCardView.setVisibility(enabled ? View.VISIBLE : View.GONE);
123+
public void setLabelEnabled(boolean enabled) {
124+
mIsLabelEnabled = enabled && !TextUtils.isEmpty(mLabelTextView.getText());
125+
mLabelCardView.setVisibility(mIsLabelEnabled ? View.VISIBLE : View.GONE);
119126
}
120127

121128
/**
@@ -254,7 +261,6 @@ private void init(Context context, @Nullable AttributeSet attrs) {
254261
mLabelCardView = rootView.findViewById(R.id.sd_label_container);
255262

256263
setFabSize(SIZE_MINI);
257-
setOrientation(LinearLayout.HORIZONTAL);
258264
setClipChildren(false);
259265
setClipToPadding(false);
260266

@@ -294,30 +300,58 @@ private void setFabSize(@FloatingActionButton.Size int fabSize) {
294300
int miniFabSizePx = getContext().getResources().getDimensionPixelSize(R.dimen.sd_fab_mini_size);
295301
int fabSideMarginPx = getContext().getResources().getDimensionPixelSize(R.dimen.sd_fab_side_margin);
296302
int fabSizePx = fabSize == SIZE_NORMAL ? normalFabSizePx : miniFabSizePx;
297-
LayoutParams rootLayoutParams;
298303
LayoutParams fabLayoutParams = (LayoutParams) mFab.getLayoutParams();
299-
if (getOrientation() == HORIZONTAL) {
300-
rootLayoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, fabSizePx);
301-
rootLayoutParams.gravity = Gravity.END;
302-
304+
if (mOrientation == Orientation.HORIZONTAL) {
305+
LayoutParams cardParams = getHorizontalLabelLayoutParams(mFab);
306+
mLabelCardView.setLayoutParams(cardParams);
307+
int cardViewId = mLabelCardView.getId();
308+
fabLayoutParams.endToEnd = LayoutParams.PARENT_ID;
309+
fabLayoutParams.startToEnd = cardViewId;
310+
fabLayoutParams.topToTop = LayoutParams.PARENT_ID;
311+
fabLayoutParams.bottomToBottom = LayoutParams.PARENT_ID;
303312
if (fabSize == SIZE_NORMAL) {
304313
int excessMargin = (normalFabSizePx - miniFabSizePx) / 2;
305314
fabLayoutParams.setMargins(fabSideMarginPx - excessMargin, 0, fabSideMarginPx - excessMargin, 0);
306315
} else {
307316
fabLayoutParams.setMargins(fabSideMarginPx, 0, fabSideMarginPx, 0);
308-
309317
}
310318
} else {
311-
rootLayoutParams = new LayoutParams(fabSizePx, ViewGroup.LayoutParams.WRAP_CONTENT);
312-
rootLayoutParams.gravity = Gravity.CENTER_VERTICAL;
313319
fabLayoutParams.setMargins(0, 0, 0, 0);
314-
}
315320

316-
setLayoutParams(rootLayoutParams);
321+
int cardViewId = mLabelCardView.getId();
322+
LayoutParams cardParams = getVerticalLabelLayoutParams(mFab);
323+
mLabelCardView.setLayoutParams(cardParams);
324+
fabLayoutParams.endToEnd = LayoutParams.PARENT_ID;
325+
fabLayoutParams.startToStart = LayoutParams.PARENT_ID;
326+
fabLayoutParams.topToTop = LayoutParams.PARENT_ID;
327+
fabLayoutParams.bottomToTop = cardViewId;
328+
}
317329
mFab.setLayoutParams(fabLayoutParams);
318330
mCurrentFabSize = fabSize;
319331
}
320332

333+
private static LayoutParams getVerticalLabelLayoutParams(View mainFabView) {
334+
int fabId = mainFabView.getId();
335+
LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
336+
ViewGroup.LayoutParams.WRAP_CONTENT);
337+
layoutParams.startToStart = fabId;
338+
layoutParams.endToEnd = fabId;
339+
layoutParams.topToBottom = fabId;
340+
layoutParams.bottomToBottom = LayoutParams.PARENT_ID;
341+
return layoutParams;
342+
}
343+
344+
private static LayoutParams getHorizontalLabelLayoutParams(View mainFabView) {
345+
int fabId = mainFabView.getId();
346+
LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
347+
ViewGroup.LayoutParams.WRAP_CONTENT);
348+
layoutParams.startToStart = LayoutParams.PARENT_ID;
349+
layoutParams.endToStart = fabId;
350+
layoutParams.topToTop = LayoutParams.PARENT_ID;
351+
layoutParams.bottomToBottom = LayoutParams.PARENT_ID;
352+
return layoutParams;
353+
}
354+
321355
/**
322356
* Sets fab drawable.
323357
*
@@ -335,7 +369,7 @@ private void setFabIcon(@Nullable Drawable mDrawable) {
335369
private void setLabel(@Nullable CharSequence sequence) {
336370
if (!TextUtils.isEmpty(sequence)) {
337371
mLabelTextView.setText(sequence);
338-
setLabelEnabled(getOrientation() == HORIZONTAL);
372+
setLabelEnabled(true);
339373
} else {
340374
setLabelEnabled(false);
341375
}

0 commit comments

Comments
 (0)