Android: Two Horizontal Side by Side Textview not managing the same height

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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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}")
}
}
</code>
<code>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}") } } </code>
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

`

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><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>
</code>
<code><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> </code>
<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?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật