2626import android .text .TextUtils ;
2727import android .util .AttributeSet ;
2828import android .util .Log ;
29- import android .view .Gravity ;
3029import android .view .View ;
3130import android .view .ViewGroup ;
32- import android .widget .LinearLayout ;
3331import android .widget .TextView ;
3432
3533import androidx .annotation .ColorInt ;
3634import androidx .annotation .DrawableRes ;
35+ import androidx .annotation .IntDef ;
3736import androidx .annotation .Nullable ;
3837import androidx .cardview .widget .CardView ;
38+ import androidx .constraintlayout .widget .ConstraintLayout ;
3939import androidx .core .content .res .ResourcesCompat ;
4040import androidx .core .graphics .drawable .DrawableCompat ;
4141import com .google .android .material .floatingactionbutton .FloatingActionButton ;
4242import com .leinardi .android .speeddial .SpeedDialView .OnActionSelectedListener ;
4343
44+ import java .lang .annotation .Retention ;
45+
4446import static com .google .android .material .floatingactionbutton .FloatingActionButton .SIZE_AUTO ;
4547import static com .google .android .material .floatingactionbutton .FloatingActionButton .SIZE_MINI ;
4648import static com .google .android .material .floatingactionbutton .FloatingActionButton .SIZE_NORMAL ;
4749import 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