Search code examples
androidanimationandroid-animationlayoutparams

Android get height of a wrap content view inflated as 0dp


I'm currently working on animation that will grow up the view if the user clicks it. Basically, its a card that, when clicked, it will reveal the bottom content. For that, I'm extending Animation like this:

Val collapseAnimation = object : Animation() {
            override fun applyTransformation(interpolatedTime: Float, t: Transformation?) {
                val interpolatedInverted = 1 - interpolatedTime

                val headerLp = headerImage.layoutParams
                headerLp.width = ...
                headerImage.layoutParams = headerLp
            }
        }

The problem is that i need to get the height of a view (wrap_content) that is defined in XML as 0dp. Basically, I want to grow up a view from 0dp to wrap_content and for that i need to know what is the wrap_content size.

How can I accomplish that in the most efficient way, without hard coding the view size?


Solution

  • In order to measure a view with different layout params and get its height, we can do the following:

    contentContainer.measure(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)
    val contentContainerFinalHeight = contentContainer.measuredHeight