From 8776bafc0ac3231b5f1f534d8e4322449dd4c42a Mon Sep 17 00:00:00 2001 From: jemmaSlater <7998113+jemmaSlater@users.noreply.github.com> Date: Fri, 24 Jun 2022 11:21:03 +0100 Subject: [PATCH] HMA-5694: TabBarView large long text resize issue (#78) * Support dynamic text in TabLayout layout * Update changelog --- CHANGELOG.md | 4 ++++ .../hmrc/components/molecule/tabbar/TabBarView.kt | 13 ++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e6cc6965..fbfb7dc2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,10 @@ Allowed headings: ## [Unreleased] +### Changed + +* Updated `TabBarView` to fix bugs when the component is used with large text or long, dynamic tab titles. + ## [4.0.0] - 2022-03-25 ### Changed diff --git a/components/src/main/java/uk/gov/hmrc/components/molecule/tabbar/TabBarView.kt b/components/src/main/java/uk/gov/hmrc/components/molecule/tabbar/TabBarView.kt index 498e9056..6a0d1b9a 100644 --- a/components/src/main/java/uk/gov/hmrc/components/molecule/tabbar/TabBarView.kt +++ b/components/src/main/java/uk/gov/hmrc/components/molecule/tabbar/TabBarView.kt @@ -28,24 +28,31 @@ class TabBarView @JvmOverloads constructor( defStyleAttr: Int = 0 ) : TabLayout(context, attrs, defStyleAttr) { + private var initialHeight: Int? = null + private val parentLayoutListener = object : ViewTreeObserver.OnGlobalLayoutListener { override fun onGlobalLayout() { (parent as View).viewTreeObserver.removeOnGlobalLayoutListener(this) + tabMode = MODE_AUTO // set temporarily to auto to prevent long text auto-resizing to fit smaller views val screenWidth = (parent as View).width measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED) val tabsWidth = measuredWidth if (screenWidth > tabsWidth) { - tabMode = MODE_FIXED + tabMode = MODE_FIXED // need MODE_FIXED because MODE_AUTO centers and we want to fill the space tabGravity = GRAVITY_FILL } else { tabMode = MODE_SCROLLABLE } resources.configuration.fontScale.takeIf { it != 1.0f }?.let { - layoutParams.height = (height * it).toInt() - requestLayout() + if (initialHeight == null) initialHeight = height + // need to use initial height so it doesn't keep growing when toggled + layoutParams.height = (initialHeight!! * it).toInt() } + + // re-select selected tab incase longer text has pushed it sideways off screen and need to scroll + getTabAt(selectedTabPosition)?.select() } }