From dee1971814e10e1756f6ab6dc73f4bfd3020e9b7 Mon Sep 17 00:00:00 2001 From: SpiritCroc Date: Mon, 28 Dec 2020 14:20:12 +0100 Subject: [PATCH] Workaround for wrongly aligned textView for RTL locales Change-Id: I77e48dbc8a0c93337d9c2d2c56025ae222d291d8 --- .../app/core/ui/views/FooteredTextView.kt | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/vector/src/main/java/im/vector/app/core/ui/views/FooteredTextView.kt b/vector/src/main/java/im/vector/app/core/ui/views/FooteredTextView.kt index 84c4ece519..8760bb6ff6 100644 --- a/vector/src/main/java/im/vector/app/core/ui/views/FooteredTextView.kt +++ b/vector/src/main/java/im/vector/app/core/ui/views/FooteredTextView.kt @@ -1,6 +1,7 @@ package im.vector.app.core.ui.views import android.content.Context +import android.graphics.Canvas import android.util.AttributeSet import androidx.appcompat.widget.AppCompatTextView import kotlin.math.ceil @@ -19,10 +20,16 @@ class FooteredTextView @JvmOverloads constructor( var footerWidth: Int = 0 //var widthLimit: Float = 0f + // Workaround to RTL languages with non-RTL content messages aligning left instead of start + private var requiredHorizontalCanvasMove = 0f + override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { // First, let super measure the content for our normal TextView use super.onMeasure(widthMeasureSpec, heightMeasureSpec) + // Default case + requiredHorizontalCanvasMove = 0f + // Get max available width //val widthMode = MeasureSpec.getMode(widthMeasureSpec) val widthSize = MeasureSpec.getSize(widthMeasureSpec) @@ -70,6 +77,10 @@ class FooteredTextView @JvmOverloads constructor( // Reserve extra horizontal footer space if necessary if (widthWithHorizontalFooter > newWidth) { newWidth = ceil(widthWithHorizontalFooter).toInt() + + if (viewIsRtl) { + requiredHorizontalCanvasMove = widthWithHorizontalFooter - measuredWidth + } } } else { // Reserve vertical footer space @@ -78,4 +89,13 @@ class FooteredTextView @JvmOverloads constructor( setMeasuredDimension(newWidth, newHeight) } + + override fun onDraw(canvas: Canvas?) { + // Workaround to RTL languages with non-RTL content messages aligning left instead of start + if (requiredHorizontalCanvasMove > 0f) { + canvas?.translate(requiredHorizontalCanvasMove, 0f) + } + + super.onDraw(canvas) + } }