diff --git a/app/src/main/java/org/oppia/android/app/databinding/MarginBindingAdapters.java b/app/src/main/java/org/oppia/android/app/databinding/MarginBindingAdapters.java index 2c7cd8be971..a94e1deb30e 100644 --- a/app/src/main/java/org/oppia/android/app/databinding/MarginBindingAdapters.java +++ b/app/src/main/java/org/oppia/android/app/databinding/MarginBindingAdapters.java @@ -15,7 +15,7 @@ public static void setLayoutMarginStart(@NonNull View view, float marginStart) { if (view.getLayoutParams() instanceof MarginLayoutParams) { MarginLayoutParams params = (MarginLayoutParams) view.getLayoutParams(); MarginLayoutParamsCompat.setMarginStart(params, (int) marginStart); - view.requestLayout(); + view.setLayoutParams(params); } } @@ -25,7 +25,7 @@ public static void setLayoutMarginEnd(@NonNull View view, float marginEnd) { if (view.getLayoutParams() instanceof MarginLayoutParams) { MarginLayoutParams params = (MarginLayoutParams) view.getLayoutParams(); MarginLayoutParamsCompat.setMarginEnd(params, (int) marginEnd); - view.requestLayout(); + view.setLayoutParams(params); } } @@ -36,7 +36,6 @@ public static void setLayoutMarginTop(@NonNull View view, float marginTop) { MarginLayoutParams params = (MarginLayoutParams) view.getLayoutParams(); params.topMargin = (int) marginTop; view.setLayoutParams(params); - view.requestLayout(); } } @@ -47,22 +46,6 @@ public static void setLayoutMarginBottom(@NonNull View view, float marginBottom) MarginLayoutParams params = (MarginLayoutParams) view.getLayoutParams(); params.bottomMargin = (int) marginBottom; view.setLayoutParams(params); - view.requestLayout(); - } - } - - /** Used to set a margin for views. */ - @BindingAdapter("layoutMargin") - public static void setLayoutMargin(@NonNull View view, float margin) { - if (view.getLayoutParams() instanceof MarginLayoutParams) { - MarginLayoutParams params = (MarginLayoutParams) view.getLayoutParams(); - params.setMargins( - (int) margin, - (int) margin, - (int) margin, - (int) margin - ); - view.requestLayout(); } } } diff --git a/app/src/sharedTest/java/org/oppia/android/app/databinding/MarginBindingAdaptersTest.kt b/app/src/sharedTest/java/org/oppia/android/app/databinding/MarginBindingAdaptersTest.kt index 0ac0a8483b9..41caa3690f6 100644 --- a/app/src/sharedTest/java/org/oppia/android/app/databinding/MarginBindingAdaptersTest.kt +++ b/app/src/sharedTest/java/org/oppia/android/app/databinding/MarginBindingAdaptersTest.kt @@ -5,6 +5,7 @@ import android.app.Application import android.content.Context import android.content.Intent import android.view.View +import android.view.ViewGroup import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import androidx.core.view.ViewCompat @@ -112,8 +113,11 @@ class MarginBindingAdaptersTest { @get:Rule val initializeDefaultLocaleRule = InitializeDefaultLocaleRule() - @Inject lateinit var context: Context - @Inject lateinit var testCoroutineDispatchers: TestCoroutineDispatchers + @Inject + lateinit var context: Context + + @Inject + lateinit var testCoroutineDispatchers: TestCoroutineDispatchers @get:Rule val oppiaTestRule = OppiaTestRule() @@ -292,6 +296,78 @@ class MarginBindingAdaptersTest { assertThat(textView.marginEnd.toFloat()).isWithin(TOLERANCE).of(40f) } + @Config(qualifiers = "port") + @Test + fun testMarginBindableAdapters_setLayoutParams_preservesMargins() { + val textView = activityRule.scenario.runWithActivity { + val textView: TextView = it.findViewById(R.id.test_margin_text_view) + + // Set initial margins + setLayoutMarginStart(textView, /* marginStart= */ 24f) + setLayoutMarginEnd(textView, /* marginEnd= */ 40f) + setLayoutMarginTop(textView, /* marginTop= */ 16f) + setLayoutMarginBottom(textView, /* marginBottom= */ 32f) + + return@runWithActivity textView + } + + // Verify that the margins are correctly set after method calls + assertThat(textView.marginStart.toFloat()).isWithin(TOLERANCE).of(24f) + assertThat(textView.marginEnd.toFloat()).isWithin(TOLERANCE).of(40f) + + val layoutParams = textView.layoutParams as ViewGroup.MarginLayoutParams + assertThat(layoutParams.topMargin.toFloat()).isWithin(TOLERANCE).of(16f) + assertThat(layoutParams.bottomMargin.toFloat()).isWithin(TOLERANCE).of(32f) + } + + @Config(qualifiers = "land") + @Test + fun testMarginBindableAdapters_landscapeMode_setLayoutParams_preservesMargins() { + val textView = activityRule.scenario.runWithActivity { + val textView: TextView = it.findViewById(R.id.test_margin_text_view) + + // Set initial margins + setLayoutMarginStart(textView, /* marginStart= */ 24f) + setLayoutMarginEnd(textView, /* marginEnd= */ 40f) + setLayoutMarginTop(textView, /* marginTop= */ 16f) + setLayoutMarginBottom(textView, /* marginBottom= */ 32f) + + return@runWithActivity textView + } + + // Verify that the margins are correctly set after method calls + assertThat(textView.marginStart.toFloat()).isWithin(TOLERANCE).of(24f) + assertThat(textView.marginEnd.toFloat()).isWithin(TOLERANCE).of(40f) + + val layoutParams = textView.layoutParams as ViewGroup.MarginLayoutParams + assertThat(layoutParams.topMargin.toFloat()).isWithin(TOLERANCE).of(16f) + assertThat(layoutParams.bottomMargin.toFloat()).isWithin(TOLERANCE).of(32f) + } + + @Config(qualifiers = "sw600dp-port") + @Test + fun testMarginBindableAdapters_tabletMode_setLayoutParams_preservesMargins() { + val textView = activityRule.scenario.runWithActivity { + val textView: TextView = it.findViewById(R.id.test_margin_text_view) + + // Set initial margins + setLayoutMarginStart(textView, /* marginStart= */ 24f) + setLayoutMarginEnd(textView, /* marginEnd= */ 40f) + setLayoutMarginTop(textView, /* marginTop= */ 16f) + setLayoutMarginBottom(textView, /* marginBottom= */ 32f) + + return@runWithActivity textView + } + + // Verify that the margins are correctly set after method calls + assertThat(textView.marginStart.toFloat()).isWithin(TOLERANCE).of(24f) + assertThat(textView.marginEnd.toFloat()).isWithin(TOLERANCE).of(40f) + + val layoutParams = textView.layoutParams as ViewGroup.MarginLayoutParams + assertThat(layoutParams.topMargin.toFloat()).isWithin(TOLERANCE).of(16f) + assertThat(layoutParams.bottomMargin.toFloat()).isWithin(TOLERANCE).of(32f) + } + private fun testMarginBindableAdapters_topAndBottomIsCorrect() { activityRule.scenario.runWithActivity { val textView: TextView = it.findViewById(R.id.test_margin_text_view)