Skip to content

Commit db9060e

Browse files
committed
Improve support for RTL layout
1 parent cdda727 commit db9060e

File tree

1 file changed

+77
-10
lines changed

1 file changed

+77
-10
lines changed

dynamic-utils/src/main/java/com/pranavpandey/android/dynamic/util/DynamicViewUtils.java

Lines changed: 77 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@
5151
public class DynamicViewUtils {
5252

5353
/**
54-
* Checks if the supplied view is the only root layout in the view heirarchy.
54+
* Checks if the supplied view is the only root layout in the view hierarchy.
5555
*
5656
* @param view The view to be checked.
5757
* @param <T> The type of the view.
5858
*
59-
* @return {@code true} if the supplied view is the only root layout in the view heirarchy.
59+
* @return {@code true} if the supplied view is the only root layout in the view hierarchy.
6060
*/
6161
public static <T extends View> boolean isRootLayout(@Nullable T view) {
6262
return view != null && !(view.getParent() instanceof View);
@@ -385,12 +385,11 @@ public static void applyWindowInsets(@Nullable View view, final boolean left,
385385
@Override
386386
public @NonNull WindowInsetsCompat onApplyWindowInsets(
387387
@NonNull View v, @NonNull WindowInsetsCompat insets) {
388-
final boolean isRtl = isLayoutRtl(v);
389-
v.setPadding(left ? isRtl ? paddingRight : paddingLeft + insets.getInsets(
388+
v.setPadding(left ? paddingLeft + insets.getInsets(
390389
WindowInsetsCompat.Type.systemBars()).left : paddingLeft,
391390
top ? paddingTop + insets.getInsets(
392391
WindowInsetsCompat.Type.systemBars()).top : paddingTop,
393-
right ? isRtl ? paddingLeft : paddingRight + insets.getInsets(
392+
right ? paddingRight + insets.getInsets(
394393
WindowInsetsCompat.Type.systemBars()).right : paddingRight,
395394
bottom ? paddingBottom + insets.getInsets(
396395
WindowInsetsCompat.Type.systemBars()).bottom : paddingBottom);
@@ -425,6 +424,17 @@ public static void applyWindowInsetsHorizontal(@Nullable View view, boolean cons
425424
applyWindowInsets(view, true, false, true, false, consume);
426425
}
427426

427+
/**
428+
* Apply horizontal window insets padding for the supplied view.
429+
*
430+
* @param view The view to set the insets padding.
431+
*
432+
* @see #applyWindowInsetsHorizontal(View, boolean)
433+
*/
434+
public static void applyWindowInsetsHorizontal(@Nullable View view) {
435+
applyWindowInsetsHorizontal(view, false);
436+
}
437+
428438
/**
429439
* Apply vertical window insets padding for the supplied view.
430440
*
@@ -437,6 +447,17 @@ public static void applyWindowInsetsVertical(@Nullable View view, boolean consum
437447
applyWindowInsets(view, false, true, false, true, consume);
438448
}
439449

450+
/**
451+
* Apply vertical window insets padding for the supplied view.
452+
*
453+
* @param view The view to set the insets padding.
454+
*
455+
* @see #applyWindowInsetsVertical(View, boolean)
456+
*/
457+
public static void applyWindowInsetsVertical(@Nullable View view) {
458+
applyWindowInsetsVertical(view, false);
459+
}
460+
440461
/**
441462
* Apply bottom window insets padding for the supplied view.
442463
*
@@ -510,17 +531,16 @@ public static void applyWindowInsetsMargin(final @Nullable View view, final bool
510531
@Override
511532
public @NonNull WindowInsetsCompat onApplyWindowInsets(
512533
@NonNull View v, @NonNull WindowInsetsCompat insets) {
513-
final boolean isRtl = isLayoutRtl(v);
514534
if (left) {
515-
layoutParams.leftMargin = isRtl ? rightMargin : leftMargin
535+
layoutParams.leftMargin = leftMargin
516536
+ insets.getInsets(WindowInsetsCompat.Type.systemBars()).left;
517537
}
518538
if (top) {
519539
layoutParams.topMargin = topMargin
520540
+ insets.getInsets(WindowInsetsCompat.Type.systemBars()).top;
521541
}
522542
if (right) {
523-
layoutParams.rightMargin = isRtl ? leftMargin : rightMargin
543+
layoutParams.rightMargin = rightMargin
524544
+ insets.getInsets(WindowInsetsCompat.Type.systemBars()).right;
525545
}
526546
if (bottom) {
@@ -560,6 +580,17 @@ public static void applyWindowInsetsMarginHorizontal(@Nullable View view, boolea
560580
applyWindowInsetsMargin(view, true, false, true, false, consume);
561581
}
562582

583+
/**
584+
* Apply horizontal window insets margin for the supplied view.
585+
*
586+
* @param view The view to set the insets margin.
587+
*
588+
* @see #applyWindowInsetsMarginHorizontal(View, boolean)
589+
*/
590+
public static void applyWindowInsetsMarginHorizontal(@Nullable View view) {
591+
applyWindowInsetsMarginHorizontal(view, false);
592+
}
593+
563594
/**
564595
* Apply vertical window insets margin for the supplied view.
565596
*
@@ -572,26 +603,62 @@ public static void applyWindowInsetsMarginVertical(@Nullable View view, boolean
572603
applyWindowInsetsMargin(view, false, true, false, true, consume);
573604
}
574605

606+
/**
607+
* Apply vertical window insets margin for the supplied view.
608+
*
609+
* @param view The view to set the insets margin.
610+
*
611+
* @see #applyWindowInsetsMarginVertical(View, boolean)
612+
*/
613+
public static void applyWindowInsetsMarginVertical(@Nullable View view) {
614+
applyWindowInsetsMarginVertical(view, false);
615+
}
616+
575617
/**
576618
* Apply bottom window insets margin for the supplied view.
577619
*
578620
* @param view The view to set the insets margin.
621+
* @param consume {@code true} to consume the applied window insets.
579622
*
580623
* @see #applyWindowInsetsMargin(View, boolean, boolean, boolean, boolean, boolean)
581624
*/
625+
public static void applyWindowInsetsMarginBottom(@Nullable View view, boolean consume) {
626+
applyWindowInsetsMargin(view, false, false, false, true, consume);
627+
}
628+
629+
/**
630+
* Apply bottom window insets margin for the supplied view.
631+
*
632+
* @param view The view to set the insets margin.
633+
*
634+
* @see #applyWindowInsetsMarginBottom(View, boolean)
635+
*/
582636
public static void applyWindowInsetsMarginBottom(@Nullable View view) {
583-
applyWindowInsetsMargin(view, false, false, false, true, false);
637+
applyWindowInsetsMarginBottom(view, false);
584638
}
585639

586640
/**
587641
* Apply horizontal and bottom window insets margin for the supplied view.
588642
*
589643
* @param view The view to set the insets margin.
644+
* @param consume {@code true} to consume the applied window insets.
590645
*
591646
* @see #applyWindowInsetsMargin(View, boolean, boolean, boolean, boolean, boolean)
592647
*/
648+
public static void applyWindowInsetsMarginHorizontalBottom(
649+
@Nullable View view, boolean consume) {
650+
applyWindowInsetsMargin(view, true, false, true, true, consume);
651+
}
652+
653+
/**
654+
* Apply horizontal and bottom window insets margin for the supplied view.
655+
*
656+
* @param view The view to set the insets margin.
657+
*
658+
* @see #applyWindowInsetsMarginHorizontalBottom(View, boolean)
659+
*/
593660
public static void applyWindowInsetsMarginHorizontalBottom(@Nullable View view) {
594-
applyWindowInsetsMargin(view, true, false, true, true, false);
661+
applyWindowInsetsMarginHorizontalBottom(view, false);
595662
}
596663

597664
/**

0 commit comments

Comments
 (0)