From 2cbbfca73fdffba98d4acc771592f4f23d02b5c2 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Wed, 23 Mar 2022 16:27:42 +0000 Subject: [PATCH] adding helper for recalculating percentage heights within a constraint layout - this allows percentages to be used which make of the screen viewport rather than the accumulated scroll height --- .../app/core/extensions/ConstraintLayout.kt | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/vector/src/main/java/im/vector/app/core/extensions/ConstraintLayout.kt b/vector/src/main/java/im/vector/app/core/extensions/ConstraintLayout.kt index b1b30da156..46d173b848 100644 --- a/vector/src/main/java/im/vector/app/core/extensions/ConstraintLayout.kt +++ b/vector/src/main/java/im/vector/app/core/extensions/ConstraintLayout.kt @@ -16,8 +16,12 @@ package im.vector.app.core.extensions +import android.view.View import androidx.constraintlayout.widget.ConstraintLayout import androidx.constraintlayout.widget.ConstraintSet +import androidx.core.view.children +import androidx.core.view.doOnLayout +import kotlin.math.roundToInt fun ConstraintLayout.updateConstraintSet(block: (ConstraintSet) -> Unit) { ConstraintSet().let { @@ -26,3 +30,16 @@ fun ConstraintLayout.updateConstraintSet(block: (ConstraintSet) -> Unit) { it.applyTo(this) } } + +fun ConstraintLayout.realignPercentagesToParent() { + doOnLayout { + val rootHeight = (parent as View).height + children.forEach { child -> + val params = child.layoutParams as ConstraintLayout.LayoutParams + if (params.matchConstraintPercentHeight != 1.0f) { + params.height = (rootHeight * params.matchConstraintPercentHeight).roundToInt() + child.layoutParams = params + } + } + } +}