@@ -146,6 +146,8 @@ public class BottomAppBar extends Toolbar implements AttachedBehavior {
146
146
@ FabAnimationMode private int fabAnimationMode ;
147
147
private boolean hideOnScroll ;
148
148
private final boolean paddingBottomSystemWindowInsets ;
149
+ private final boolean paddingLeftSystemWindowInsets ;
150
+ private final boolean paddingRightSystemWindowInsets ;
149
151
150
152
/** Keeps track of the number of currently running animations. */
151
153
private int animatingModeChangeCounter = 0 ;
@@ -168,6 +170,8 @@ interface AnimationListener {
168
170
private Behavior behavior ;
169
171
170
172
private int bottomInset ;
173
+ private int rightInset ;
174
+ private int leftInset ;
171
175
172
176
/**
173
177
* Listens to the FABs hide or show animation to kick off an animation on BottomAppBar that reacts
@@ -243,6 +247,10 @@ public BottomAppBar(@NonNull Context context, @Nullable AttributeSet attrs, int
243
247
// Reading out if we are handling bottom padding, so we can apply it to the FAB.
244
248
paddingBottomSystemWindowInsets =
245
249
a .getBoolean (R .styleable .BottomAppBar_paddingBottomSystemWindowInsets , false );
250
+ paddingLeftSystemWindowInsets =
251
+ a .getBoolean (R .styleable .BottomAppBar_paddingLeftSystemWindowInsets , false );
252
+ paddingRightSystemWindowInsets =
253
+ a .getBoolean (R .styleable .BottomAppBar_paddingRightSystemWindowInsets , false );
246
254
247
255
a .recycle ();
248
256
@@ -273,11 +281,31 @@ public WindowInsetsCompat onApplyWindowInsets(
273
281
View view ,
274
282
@ NonNull WindowInsetsCompat insets ,
275
283
@ NonNull RelativePadding initialPadding ) {
284
+ // Just read the insets here. doOnApplyWindowInsets will apply the padding under the
285
+ // hood.
286
+ boolean leftInsetsChanged = false ;
287
+ boolean rightInsetsChanged = false ;
276
288
if (paddingBottomSystemWindowInsets ) {
277
- // Just read the insets here. doOnApplyWindowInsets will apply the bottom padding
278
- // under the hood.
279
289
bottomInset = insets .getSystemWindowInsetBottom ();
280
290
}
291
+ if (paddingLeftSystemWindowInsets ) {
292
+ leftInsetsChanged = leftInset != insets .getSystemWindowInsetLeft ();
293
+ leftInset = insets .getSystemWindowInsetLeft ();
294
+ }
295
+ if (paddingRightSystemWindowInsets ) {
296
+ rightInsetsChanged = rightInset != insets .getSystemWindowInsetRight ();
297
+ rightInset = insets .getSystemWindowInsetRight ();
298
+ }
299
+
300
+ // We may need to change the position of the cutout or the action menu if the side
301
+ // insets have changed.
302
+ if (leftInsetsChanged || rightInsetsChanged ) {
303
+ cancelAnimations ();
304
+
305
+ setCutoutState ();
306
+ setActionMenuViewPosition ();
307
+ }
308
+
281
309
return insets ;
282
310
}
283
311
});
@@ -679,10 +707,14 @@ private float getFabTranslationY() {
679
707
}
680
708
681
709
private float getFabTranslationX (@ FabAlignmentMode int fabAlignmentMode ) {
682
- boolean isRtl = ViewCompat .getLayoutDirection (this ) == ViewCompat .LAYOUT_DIRECTION_RTL ;
683
- return fabAlignmentMode == FAB_ALIGNMENT_MODE_END
684
- ? (getMeasuredWidth () / 2 - fabOffsetEndMode ) * (isRtl ? -1 : 1 )
685
- : 0 ;
710
+ boolean isRtl = ViewUtils .isLayoutRtl (this );
711
+ if (fabAlignmentMode == FAB_ALIGNMENT_MODE_END ) {
712
+ int systemEndInset = isRtl ? leftInset : rightInset ;
713
+ int totalEndInset = fabOffsetEndMode + systemEndInset ;
714
+ return (getMeasuredWidth () / 2 - totalEndInset ) * (isRtl ? -1 : 1 );
715
+ } else {
716
+ return 0 ;
717
+ }
686
718
}
687
719
688
720
private float getFabTranslationX () {
@@ -729,7 +761,11 @@ protected int getActionMenuViewTranslationX(
729
761
@ NonNull ActionMenuView actionMenuView ,
730
762
@ FabAlignmentMode int fabAlignmentMode ,
731
763
boolean fabAttached ) {
732
- boolean isRtl = ViewCompat .getLayoutDirection (this ) == ViewCompat .LAYOUT_DIRECTION_RTL ;
764
+ if (fabAlignmentMode != FAB_ALIGNMENT_MODE_END || !fabAttached ) {
765
+ return 0 ;
766
+ }
767
+
768
+ boolean isRtl = ViewUtils .isLayoutRtl (this );
733
769
int toolbarLeftContentEnd = isRtl ? getMeasuredWidth () : 0 ;
734
770
735
771
// Calculate the inner side of the Toolbar's Gravity.START contents.
@@ -748,10 +784,11 @@ protected int getActionMenuViewTranslationX(
748
784
}
749
785
}
750
786
751
- int end = isRtl ? actionMenuView .getRight () : actionMenuView .getLeft ();
752
- int offset = toolbarLeftContentEnd - end ;
787
+ int actionMenuViewStart = isRtl ? actionMenuView .getRight () : actionMenuView .getLeft ();
788
+ int systemStartInset = isRtl ? rightInset : -leftInset ;
789
+ int end = actionMenuViewStart + systemStartInset ;
753
790
754
- return fabAlignmentMode == FAB_ALIGNMENT_MODE_END && fabAttached ? offset : 0 ;
791
+ return toolbarLeftContentEnd - end ;
755
792
}
756
793
757
794
private void cancelAnimations () {
@@ -838,6 +875,14 @@ private int getBottomInset() {
838
875
return bottomInset ;
839
876
}
840
877
878
+ private int getRightInset () {
879
+ return rightInset ;
880
+ }
881
+
882
+ private int getLeftInset () {
883
+ return leftInset ;
884
+ }
885
+
841
886
@ Override
842
887
public void setTitle (CharSequence title ) {
843
888
// Don't do anything. BottomAppBar can't have a title.
@@ -927,6 +972,14 @@ public void onLayoutChange(
927
972
// Should be moved above the bottom insets with space ignoring any shadow padding.
928
973
int minBottomMargin = bottomMargin - bottomShadowPadding ;
929
974
fabLayoutParams .bottomMargin = child .getBottomInset () + minBottomMargin ;
975
+ fabLayoutParams .leftMargin = child .getLeftInset ();
976
+ fabLayoutParams .rightMargin = child .getRightInset ();
977
+ boolean isRtl = ViewUtils .isLayoutRtl (fab );
978
+ if (isRtl ) {
979
+ fabLayoutParams .leftMargin += child .fabOffsetEndMode ;
980
+ } else {
981
+ fabLayoutParams .rightMargin += child .fabOffsetEndMode ;
982
+ }
930
983
}
931
984
}
932
985
};
0 commit comments