26
26
import android .text .TextUtils ;
27
27
import android .util .AttributeSet ;
28
28
import android .util .Log ;
29
- import android .view .Gravity ;
30
29
import android .view .View ;
31
30
import android .view .ViewGroup ;
32
- import android .widget .LinearLayout ;
33
31
import android .widget .TextView ;
34
32
35
33
import androidx .annotation .ColorInt ;
36
34
import androidx .annotation .DrawableRes ;
35
+ import androidx .annotation .IntDef ;
37
36
import androidx .annotation .Nullable ;
38
37
import androidx .cardview .widget .CardView ;
38
+ import androidx .constraintlayout .widget .ConstraintLayout ;
39
39
import androidx .core .content .res .ResourcesCompat ;
40
40
import androidx .core .graphics .drawable .DrawableCompat ;
41
41
import com .google .android .material .floatingactionbutton .FloatingActionButton ;
42
42
import com .leinardi .android .speeddial .SpeedDialView .OnActionSelectedListener ;
43
43
44
+ import java .lang .annotation .Retention ;
45
+
44
46
import static com .google .android .material .floatingactionbutton .FloatingActionButton .SIZE_AUTO ;
45
47
import static com .google .android .material .floatingactionbutton .FloatingActionButton .SIZE_MINI ;
46
48
import static com .google .android .material .floatingactionbutton .FloatingActionButton .SIZE_NORMAL ;
47
49
import static com .leinardi .android .speeddial .SpeedDialActionItem .RESOURCE_NOT_SET ;
50
+ import static java .lang .annotation .RetentionPolicy .SOURCE ;
48
51
49
52
/**
50
53
* View that contains fab button and its label.
51
54
*/
52
55
@ SuppressWarnings ({"unused" , "WeakerAccess" })
53
- public class FabWithLabelView extends LinearLayout {
56
+ public class FabWithLabelView extends ConstraintLayout {
54
57
private static final String TAG = FabWithLabelView .class .getSimpleName ();
55
58
56
59
private TextView mLabelTextView ;
@@ -66,6 +69,15 @@ public class FabWithLabelView extends LinearLayout {
66
69
private float mLabelCardViewElevation ;
67
70
@ Nullable
68
71
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
+ }
69
81
70
82
public FabWithLabelView (Context context ) {
71
83
super (context );
@@ -92,15 +104,10 @@ public void setVisibility(int visibility) {
92
104
}
93
105
}
94
106
95
- @ Override
96
- public void setOrientation (int orientation ) {
97
- super .setOrientation (orientation );
107
+ public void setOrientation (@ Orientation int orientation ) {
108
+ mOrientation = orientation ;
98
109
setFabSize (mCurrentFabSize );
99
- if (orientation == VERTICAL ) {
100
- setLabelEnabled (false );
101
- } else {
102
- setLabel (mLabelTextView .getText ().toString ());
103
- }
110
+ setLabel (mLabelTextView .getText ().toString ());
104
111
}
105
112
106
113
/**
@@ -113,9 +120,9 @@ public boolean isLabelEnabled() {
113
120
/**
114
121
* Enables or disables label of button.
115
122
*/
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 );
119
126
}
120
127
121
128
/**
@@ -254,7 +261,6 @@ private void init(Context context, @Nullable AttributeSet attrs) {
254
261
mLabelCardView = rootView .findViewById (R .id .sd_label_container );
255
262
256
263
setFabSize (SIZE_MINI );
257
- setOrientation (LinearLayout .HORIZONTAL );
258
264
setClipChildren (false );
259
265
setClipToPadding (false );
260
266
@@ -296,28 +302,59 @@ private void setFabSize(@FloatingActionButton.Size int fabSize) {
296
302
int fabSizePx = fabSize == SIZE_NORMAL ? normalFabSizePx : miniFabSizePx ;
297
303
LayoutParams rootLayoutParams ;
298
304
LayoutParams fabLayoutParams = (LayoutParams ) mFab .getLayoutParams ();
299
- if (getOrientation () == HORIZONTAL ) {
305
+ if (mOrientation == Orientation . HORIZONTAL ) {
300
306
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 ;
303
314
if (fabSize == SIZE_NORMAL ) {
304
315
int excessMargin = (normalFabSizePx - miniFabSizePx ) / 2 ;
305
316
fabLayoutParams .setMargins (fabSideMarginPx - excessMargin , 0 , fabSideMarginPx - excessMargin , 0 );
306
317
} else {
307
318
fabLayoutParams .setMargins (fabSideMarginPx , 0 , fabSideMarginPx , 0 );
308
-
309
319
}
310
320
} else {
311
- rootLayoutParams = new LayoutParams (fabSizePx , ViewGroup .LayoutParams .WRAP_CONTENT );
312
- rootLayoutParams .gravity = Gravity .CENTER_VERTICAL ;
313
321
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 ;
314
330
}
315
-
316
331
setLayoutParams (rootLayoutParams );
317
332
mFab .setLayoutParams (fabLayoutParams );
318
333
mCurrentFabSize = fabSize ;
319
334
}
320
335
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
+
321
358
/**
322
359
* Sets fab drawable.
323
360
*
@@ -335,7 +372,7 @@ private void setFabIcon(@Nullable Drawable mDrawable) {
335
372
private void setLabel (@ Nullable CharSequence sequence ) {
336
373
if (!TextUtils .isEmpty (sequence )) {
337
374
mLabelTextView .setText (sequence );
338
- setLabelEnabled (getOrientation () == HORIZONTAL );
375
+ setLabelEnabled (true );
339
376
} else {
340
377
setLabelEnabled (false );
341
378
}
0 commit comments