I’m encountering a problem with TextView height adjustment in my Android application. When I select a quantity of one, only “Tamara” is displayed and the view appears correctly. However, when I change the quantity to three, both “Tabby” and “Tamara” are enabled and displayed perfectly. The issue arises when I change the quantity back to one; the height seems to be based on the previous selection when both “Tabby” and “Tamara” were visible.
fun adjust() {
// Check if both views are visible
val bothViewsVisible = binding.tabbyFrameLayout.isVisible && binding.tamaraFrameLayout.isVisible
if (bothViewsVisible) {
binding.tamaraMessageTv.viewTreeObserver.addOnGlobalLayoutListener(object :
ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
// Remove the listener to avoid multiple calls
binding.tamaraMessageTv.viewTreeObserver.removeOnGlobalLayoutListener(this)
// Measure the height of both TextViews
val height1 = binding.tamaraMessageTv.height
val height2 = binding.tabbyMessageTv.height
// Get the maximum height of both TextViews
val maxHeight = max(height1, height2)
// Set the height of both TextViews to the maximum height
binding.tabbyMessageTv.height = maxHeight
binding.tamaraMessageTv.height = maxHeight
}
})
} else {
binding.buyNowPayLaterContainer.requestLayout()
Log.d("", "layoutHeight:: ${binding.tamaraMessageTv.height}")
}
}
Here is the XML layout
`
<data>
</data>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dimen_10dp"
android:visibility="gone"
tools:visibility="visible"
android:id="@+id/buyNowPayLaterContainer"
android:orientation="horizontal">
<FrameLayout
android:id="@+id/tabbyFrameLayout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:minHeight="@dimen/dimen_40dp"
android:layout_weight="1">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:background="@drawable/bg_border_grey"
android:layout_marginTop="@dimen/dimen_8dp"
android:paddingStart="@dimen/dimen_12dp"
android:paddingEnd="@dimen/dimen_7dp"
android:orientation="vertical">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/tabbyArrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="@+id/tabbyMessageTv"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/tabbyMessageTv"
app:srcCompat="@drawable/ic_arrow_with_padding" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tabbyMessageTv"
style="@style/Italian_plate_no2_expanded_very_dark_grey_10sp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dimen_20dp"
android:layout_marginBottom="@dimen/dimen_10dp"
android:layout_marginEnd="@dimen/dimen_7dp"
android:ellipsize="end"
android:maxLines="3"
android:textSize="@dimen/dimen_12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/tabbyArrow"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Pay 4 interest-free payments of AED xx.xx, Pay 4 interest-free payments of AED xx.xx" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="@dimen/dimen_47dp"
android:layout_height="@dimen/dimen_20dp"
android:layout_marginStart="@dimen/dimen_12dp"
android:elevation="@dimen/dimen_6dp"
app:srcCompat="@drawable/bg_tabby" />
</FrameLayout>
<View
android:id="@+id/view"
android:layout_width="@dimen/dimen_8dp"
android:layout_height="@dimen/dimen_65dp" />
<FrameLayout
android:id="@+id/tamaraFrameLayout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:background="@drawable/bg_white_rounded_tamara_border"
android:layout_marginTop="@dimen/dimen_8dp"
android:paddingStart="@dimen/dimen_12dp"
android:paddingEnd="@dimen/dimen_7dp"
android:orientation="vertical">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/tamaraArrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
app:layout_constraintBottom_toBottomOf="@+id/tamaraMessageTv"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/tamaraMessageTv"
app:srcCompat="@drawable/ic_arrow_with_padding" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tamaraMessageTv"
style="@style/Italian_plate_no2_expanded_very_dark_grey_10sp"
android:layout_width="@dimen/dimen_0dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dimen_20dp"
android:layout_marginBottom="@dimen/dimen_10dp"
android:layout_marginEnd="@dimen/dimen_7dp"
android:paddingEnd="@dimen/dimen_2dp"
android:textSize="@dimen/dimen_12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/tamaraArrow"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Pay 4 interest-free payments of AED xx.xx" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="@dimen/dimen_47dp"
android:layout_height="@dimen/dimen_20dp"
android:layout_marginStart="@dimen/dimen_12dp"
android:id="@+id/tamaraImageView"
android:elevation="@dimen/dimen_6dp"
app:srcCompat="@drawable/tamara_icon" />
</FrameLayout>
</androidx.appcompat.widget.LinearLayoutCompat>
`
How can I ensure that the TextView height adjusts dynamically based on the quantity selected, so that only the necessary TextViews are visible without any excess space when the quantity changes?
Thanks in advance for any insights or suggestions!
I’ve attempted various approaches both in the code and XML layouts, but none have resolved the issue.
Could someone offer guidance or suggest a solution to ensure that the TextView height adjusts dynamically based on the quantity selected, displaying only the necessary TextViews without any extra space when the quantity changes?