Skip to content

Commit dee3634

Browse files
committed
Fix #122: Add FAN expansion mode for speed dial items.
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 Format code according to code style Update sample app to show FAN expansion mode
1 parent dc7396d commit dee3634

File tree

10 files changed

+256
-65
lines changed

10 files changed

+256
-65
lines changed

library/build.gradle

+1
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

+60-23
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

@@ -296,28 +302,59 @@ private void setFabSize(@FloatingActionButton.Size int fabSize) {
296302
int fabSizePx = fabSize == SIZE_NORMAL ? normalFabSizePx : miniFabSizePx;
297303
LayoutParams rootLayoutParams;
298304
LayoutParams fabLayoutParams = (LayoutParams) mFab.getLayoutParams();
299-
if (getOrientation() == HORIZONTAL) {
305+
if (mOrientation == Orientation.HORIZONTAL) {
300306
rootLayoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, fabSizePx);
301-
rootLayoutParams.gravity = Gravity.END;
302-
307+
LayoutParams cardParams = getHorizontalLabelLayoutParams(mFab);
308+
mLabelCardView.setLayoutParams(cardParams);
309+
int cardViewId = mLabelCardView.getId();
310+
fabLayoutParams.endToEnd = LayoutParams.PARENT_ID;
311+
fabLayoutParams.startToEnd = cardViewId;
312+
fabLayoutParams.topToTop = LayoutParams.PARENT_ID;
313+
fabLayoutParams.bottomToBottom = LayoutParams.PARENT_ID;
303314
if (fabSize == SIZE_NORMAL) {
304315
int excessMargin = (normalFabSizePx - miniFabSizePx) / 2;
305316
fabLayoutParams.setMargins(fabSideMarginPx - excessMargin, 0, fabSideMarginPx - excessMargin, 0);
306317
} else {
307318
fabLayoutParams.setMargins(fabSideMarginPx, 0, fabSideMarginPx, 0);
308-
309319
}
310320
} else {
311-
rootLayoutParams = new LayoutParams(fabSizePx, ViewGroup.LayoutParams.WRAP_CONTENT);
312-
rootLayoutParams.gravity = Gravity.CENTER_VERTICAL;
313321
fabLayoutParams.setMargins(0, 0, 0, 0);
322+
rootLayoutParams = new LayoutParams(fabSizePx, ViewGroup.LayoutParams.WRAP_CONTENT);
323+
int cardViewId = mLabelCardView.getId();
324+
LayoutParams cardParams = getVerticalLabelLayoutParams(mFab);
325+
mLabelCardView.setLayoutParams(cardParams);
326+
fabLayoutParams.endToEnd = LayoutParams.PARENT_ID;
327+
fabLayoutParams.startToStart = LayoutParams.PARENT_ID;
328+
fabLayoutParams.topToTop = LayoutParams.PARENT_ID;
329+
fabLayoutParams.bottomToTop = cardViewId;
314330
}
315-
316331
setLayoutParams(rootLayoutParams);
317332
mFab.setLayoutParams(fabLayoutParams);
318333
mCurrentFabSize = fabSize;
319334
}
320335

336+
private static LayoutParams getVerticalLabelLayoutParams(View mainFabView) {
337+
int fabId = mainFabView.getId();
338+
LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
339+
ViewGroup.LayoutParams.WRAP_CONTENT);
340+
layoutParams.startToStart = fabId;
341+
layoutParams.endToEnd = fabId;
342+
layoutParams.topToBottom = fabId;
343+
layoutParams.bottomToBottom = LayoutParams.PARENT_ID;
344+
return layoutParams;
345+
}
346+
347+
private static LayoutParams getHorizontalLabelLayoutParams(View mainFabView) {
348+
int fabId = mainFabView.getId();
349+
LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
350+
ViewGroup.LayoutParams.WRAP_CONTENT);
351+
layoutParams.startToStart = LayoutParams.PARENT_ID;
352+
layoutParams.endToStart = fabId;
353+
layoutParams.topToTop = LayoutParams.PARENT_ID;
354+
layoutParams.bottomToBottom = LayoutParams.PARENT_ID;
355+
return layoutParams;
356+
}
357+
321358
/**
322359
* Sets fab drawable.
323360
*
@@ -335,7 +372,7 @@ private void setFabIcon(@Nullable Drawable mDrawable) {
335372
private void setLabel(@Nullable CharSequence sequence) {
336373
if (!TextUtils.isEmpty(sequence)) {
337374
mLabelTextView.setText(sequence);
338-
setLabelEnabled(getOrientation() == HORIZONTAL);
375+
setLabelEnabled(true);
339376
} else {
340377
setLabelEnabled(false);
341378
}

0 commit comments

Comments
 (0)