From 707012ee5c35b4b3787197102e0a0780a29f58a8 Mon Sep 17 00:00:00 2001 From: SpiritCroc Date: Wed, 19 May 2021 16:43:54 +0200 Subject: [PATCH] Fix RTL paddings Android apparently has an issue with the layout in RTL, since it applies the start/end padding to both sides of the bubble. As a workaround, put the bubbleLayout always in LTR, and handle the correct padding selection from code. Change-Id: Icddbe4b8d99383448d68bcf97f3ea166fd4cd3e6 --- .../detail/timeline/item/AbsMessageItem.kt | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/item/AbsMessageItem.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/item/AbsMessageItem.kt index b0fe63ee3a..3e3f7125d9 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/item/AbsMessageItem.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/item/AbsMessageItem.kt @@ -221,7 +221,7 @@ abstract class AbsMessageItem : AbsBaseMessageItem val memberNameView by bind(R.id.messageMemberNameView) val timeView by bind(R.id.messageTimeView) val eventBaseView by bind(R.id.eventBaseView) - val bubbleView by bind(R.id.bubbleView) + val bubbleView by bind(R.id.bubbleView) val bubbleMemberNameView by bind(R.id.bubbleMessageMemberNameView) val bubbleTimeView by bind(R.id.bubbleMessageTimeView) val bubbleFootView by bind(R.id.bubbleFootView) @@ -419,6 +419,10 @@ abstract class AbsMessageItem : AbsBaseMessageItem val bubbleView = holder.bubbleView val contentInBubble = infoInBubbles(holder.memberNameView.context) + val defaultDirection = holder.eventBaseView.resources.configuration.layoutDirection; + val defaultRtl = defaultDirection == View.LAYOUT_DIRECTION_RTL + val reverseDirection = if (defaultRtl) View.LAYOUT_DIRECTION_LTR else View.LAYOUT_DIRECTION_RTL + when (bubbleStyle) { BubbleThemeUtils.BUBBLE_STYLE_START, BubbleThemeUtils.BUBBLE_STYLE_BOTH, @@ -447,12 +451,13 @@ abstract class AbsMessageItem : AbsBaseMessageItem shortPadding = 0//if (attributes.informationData.showInformation && !hideSenderInformation()) { 8 } else { 0 } } val density = bubbleView.resources.displayMetrics.density - if (reverseBubble) { - (bubbleView.layoutParams as ViewGroup.MarginLayoutParams).marginStart = getBubbleMargin(bubbleView.resources, bubbleStyle, reverseBubble) - (bubbleView.layoutParams as ViewGroup.MarginLayoutParams).marginEnd = 0 + if (reverseBubble != defaultRtl) { + // Use left/right instead of start/end: bubbleView is always LTR + (bubbleView.layoutParams as ViewGroup.MarginLayoutParams).leftMargin = getBubbleMargin(bubbleView.resources, bubbleStyle, reverseBubble) + (bubbleView.layoutParams as ViewGroup.MarginLayoutParams).rightMargin = 0 } else { - (bubbleView.layoutParams as ViewGroup.MarginLayoutParams).marginStart = 0 - (bubbleView.layoutParams as ViewGroup.MarginLayoutParams).marginEnd = getBubbleMargin(bubbleView.resources, bubbleStyle, reverseBubble) + (bubbleView.layoutParams as ViewGroup.MarginLayoutParams).leftMargin = 0 + (bubbleView.layoutParams as ViewGroup.MarginLayoutParams).rightMargin = getBubbleMargin(bubbleView.resources, bubbleStyle, reverseBubble) } /* (bubbleView.layoutParams as RelativeLayout.LayoutParams).marginStart = round(20*density).toInt() @@ -461,15 +466,16 @@ abstract class AbsMessageItem : AbsBaseMessageItem */ val shortPaddingDp = round(shortPadding * density).toInt() val longPaddingDp = round(longPadding * density).toInt() - if (reverseBubble) { - bubbleView.setPaddingRelative( + if (reverseBubble != defaultRtl) { + // Use left/right instead of start/end: bubbleView is always LTR + bubbleView.setPadding( shortPaddingDp, shortPaddingDp, longPaddingDp, shortPaddingDp ) } else { - bubbleView.setPaddingRelative( + bubbleView.setPadding( longPaddingDp, shortPaddingDp, shortPaddingDp, @@ -580,9 +586,6 @@ abstract class AbsMessageItem : AbsBaseMessageItem } } - val defaultDirection = holder.eventBaseView.resources.configuration.layoutDirection; - val defaultRtl = defaultDirection == View.LAYOUT_DIRECTION_RTL - val reverseDirection = if (defaultRtl) View.LAYOUT_DIRECTION_LTR else View.LAYOUT_DIRECTION_RTL /* holder.eventBaseView.layoutDirection = if (shouldRtl) View.LAYOUT_DIRECTION_RTL else View.LAYOUT_DIRECTION_LTR setRtl(shouldRtl) @@ -591,6 +594,9 @@ abstract class AbsMessageItem : AbsBaseMessageItem //holder.informationBottom.layoutDirection = if (shouldRtl) View.LAYOUT_DIRECTION_RTL else View.LAYOUT_DIRECTION_LTR setFlatRtl(holder.reactionsContainer, if (reverseBubble) reverseDirection else defaultDirection, holder.eventBaseView.resources.configuration.layoutDirection) + // Layout is broken if bubbleView is RTL (for some reason, Android uses left/end padding for right/start as well...) + setFlatRtl(holder.bubbleView, View.LAYOUT_DIRECTION_LTR, + holder.eventBaseView.resources.configuration.layoutDirection) } private fun tintFooter(holder: H, color: Int) {