Skip to content

Commit

Permalink
HMA-5694: TabBarView large long text resize issue (#78)
Browse files Browse the repository at this point in the history
* Support dynamic text in TabLayout layout

* Update changelog
  • Loading branch information
jemmaSlater authored Jun 24, 2022
1 parent 41d7084 commit 8776baf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down

0 comments on commit 8776baf

Please sign in to comment.