Skip to content

Commit fb2b633

Browse files
authored
Fix the issue that horizontal margin is ignored on devices with API (#354)
level < 17. Because the FlexboxHelper expected ViewGroup.MarginLayoutParams#getMarginStart (getMarginEnd) methods when calculating the wrap condition. No devices are available on API level < 17 on Firebase Test Lab. So manually ran the tests on an emulator with API level 16.
1 parent 14f2a42 commit fb2b633

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

flexbox/src/androidTest/res/layout/activity_wrap_content_child_bottom_margin_column_grow.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
android:layout_width="24dp"
3030
android:layout_height="0dp"
3131
android:layout_marginEnd="12dp"
32+
android:layout_marginRight="12dp"
3233
android:text="1"
3334
app:layout_flexBasisPercent="50%"
3435
app:layout_flexGrow="1"

flexbox/src/androidTest/res/layout/activity_wrap_content_child_bottom_margin_column_shrink.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
android:layout_width="24dp"
3030
android:layout_height="150dp"
3131
android:layout_marginEnd="12dp"
32+
android:layout_marginRight="12dp"
3233
android:text="1"
3334
app:layout_flexShrink="1" />
3435

flexbox/src/main/java/com/google/android/flexbox/FlexboxHelper.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -738,36 +738,49 @@ private int getFlexItemSizeCross(FlexItem flexItem, boolean isMainHorizontal) {
738738

739739
/**
740740
* Returns the flexItem's start margin in the main axis. Either start or top.
741+
* For the backward compatibility for API level < 17, the horizontal margin is returned using
742+
* {@link FlexItem#getMarginLeft} (ViewGroup.MarginLayoutParams#getMarginStart isn't available
743+
* in API level < 17). Thus this method needs to be used with {@link #getFlexItemMarginEndMain}
744+
* not to misuse the margin in RTL.
745+
*
741746
*
742747
* @param flexItem the flexItem
743748
* @param isMainHorizontal is the main axis horizontal
744749
* @return the flexItem's start margin in the main axis
745750
*/
746751
private int getFlexItemMarginStartMain(FlexItem flexItem, boolean isMainHorizontal) {
747752
if (isMainHorizontal) {
748-
return flexItem.getMarginStart();
753+
return flexItem.getMarginLeft();
749754
}
750755

751756
return flexItem.getMarginTop();
752757
}
753758

754759
/**
755760
* Returns the flexItem's end margin in the main axis. Either end or bottom.
761+
* For the backward compatibility for API level < 17, the horizontal margin is returned using
762+
* {@link FlexItem#getMarginRight} (ViewGroup.MarginLayoutParams#getMarginEnd isn't available
763+
* in API level < 17). Thus this method needs to be used with
764+
* {@link #getFlexItemMarginStartMain} not to misuse the margin in RTL.
756765
*
757766
* @param flexItem the flexItem
758767
* @param isMainHorizontal is the main axis horizontal
759768
* @return the flexItem's end margin in the main axis
760769
*/
761770
private int getFlexItemMarginEndMain(FlexItem flexItem, boolean isMainHorizontal) {
762771
if (isMainHorizontal) {
763-
return flexItem.getMarginEnd();
772+
return flexItem.getMarginRight();
764773
}
765774

766775
return flexItem.getMarginBottom();
767776
}
768777

769778
/**
770779
* Returns the flexItem's start margin in the cross axis. Either start or top.
780+
* For the backward compatibility for API level < 17, the horizontal margin is returned using
781+
* {@link FlexItem#getMarginLeft} (ViewGroup.MarginLayoutParams#getMarginStart isn't available
782+
* in API level < 17). Thus this method needs to be used with
783+
* {@link #getFlexItemMarginEndCross} to not to misuse the margin in RTL.
771784
*
772785
* @param flexItem the flexItem
773786
* @param isMainHorizontal is the main axis horizontal
@@ -778,11 +791,15 @@ private int getFlexItemMarginStartCross(FlexItem flexItem, boolean isMainHorizon
778791
return flexItem.getMarginTop();
779792
}
780793

781-
return flexItem.getMarginStart();
794+
return flexItem.getMarginLeft();
782795
}
783796

784797
/**
785798
* Returns the flexItem's end margin in the cross axis. Either end or bottom.
799+
* For the backward compatibility for API level < 17, the horizontal margin is returned using
800+
* {@link FlexItem#getMarginRight} (ViewGroup.MarginLayoutParams#getMarginEnd isn't available
801+
* in API level < 17). Thus this method needs to be used with
802+
* {@link #getFlexItemMarginStartCross} to not to misuse the margin in RTL.
786803
*
787804
* @param flexItem the flexItem
788805
* @param isMainHorizontal is the main axis horizontal
@@ -793,7 +810,7 @@ private int getFlexItemMarginEndCross(FlexItem flexItem, boolean isMainHorizonta
793810
return flexItem.getMarginBottom();
794811
}
795812

796-
return flexItem.getMarginEnd();
813+
return flexItem.getMarginRight();
797814
}
798815

799816
/**

0 commit comments

Comments
 (0)