Home > Net >  How to get real height layout with layout_height="0dp"?
How to get real height layout with layout_height="0dp"?

Time:02-14

I need to set the height of the FrameLayout android:id="@ id/heart_strength according to the height of the FrameLayout android:id="@ id/heart_strength_background, whose height is set like this:

            <FrameLayout
                android:id="@ id/cardiogram_background_light"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_marginStart="@dimen/default_screen_margin"
                android:layout_marginTop="@dimen/chart_widget_margin_top"
                android:layout_marginEnd="@dimen/default_screen_margin"
                android:background="@drawable/chart_widget_background_light_gray"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHeight_percent="0.184"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@ id/heart_rate_diagram_background_light" />

            <FrameLayout
                android:id="@ id/heart_strength_background"
                android:layout_width="@dimen/cardiogram_status_bar_width"
                android:layout_height="0dp"
                android:layout_marginEnd="@dimen/default_screen_margin"
                android:background="@drawable/chart_widget_background_dark_gray"
                app:layout_constraintBottom_toBottomOf="@ id/cardiogram_background_light"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="@ id/cardiogram_background_light" >

                <FrameLayout
                    android:id="@ id/heart_strength"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    app:heartStrength="@{viewmodel.heartStrengthLiveData}"
                    android:background="@drawable/chart_widget_background_light_gray"
                    android:backgroundTint="@color/turquoise"
                    android:layout_gravity="bottom"/>
            </FrameLayout>

When I try to get real height of heart_strength.parent layout with:

    @JvmStatic
    @BindingAdapter("app:heartStrength")
    fun setHeartStrengthViewHeight(bar: FrameLayout, level: Int) {
        val barParent = bar.parent as FrameLayout
        println("bar parent height: ${barParent.layoutParams.height}")
    }

I get 0. How can I know the actual height?

I have a card (cardiogram_background_light). Its height changes dynamically in %. Because of this, the maximum height of the bar located in this card also changes dynamically. Previously, I set its height as maxHeight * Value /100. But now maxHeight dynamically changes on different screen sizes and I want to know its value.

CodePudding user response:

It is because the views are not drawn yet when setHeartStrengthViewHeight is called. To solve this, try the following:

@JvmStatic
@BindingAdapter("app:heartStrength")
fun setHeartStrengthViewHeight(bar: FrameLayout, level: Int) {
    val barParent = bar.parent as FrameLayout

    val observer : ViewTreeObserver = barParent.viewTreeObserver
    observer.addOnGlobalLayoutListener(object: ViewTreeObserver.OnGlobalLayoutListener {
        override fun onGlobalLayout() {
            println("bar parent height: ${barParent.height}")
            barParent.viewTreeObserver.removeOnGlobalLayoutListener(this)
        }
    })

}
  • Related