2

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?

Gowtham Subramaniam
  • 3,358
  • 2
  • 19
  • 31
johnny_crq
  • 4,291
  • 5
  • 39
  • 64
  • try using a ViewTreeObserver to get the size at runtime. https://developer.android.com/reference/android/view/ViewTreeObserver.html – Ahmed Abidi Feb 08 '17 at 17:42
  • the view is initially inflated as 0dp, so it won't work with only this solution i think – johnny_crq Feb 08 '17 at 17:52
  • Once the view is laid out, calling `getWidth` on the view will return the actual width. So just don't call this in `onCreateView`. You can use the `ViewTreeObserver` as suggested to make sure the view has been laid out. – kris larson Feb 08 '17 at 18:05
  • if the view is defined in xml with height 0dp, how can viewtree observable return an actual height? the view is defined as having 0dp in height, i want to know its size with wrap_content without actually setting it – johnny_crq Feb 08 '17 at 20:42

1 Answers1

5

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
johnny_crq
  • 4,291
  • 5
  • 39
  • 64