Get Height Of View After Content Is Populated At Runtime

133 Views Asked by At

I have a Cardview with height WRAP_CONTENT . Content of Cardview is to be populated after network call . And based on the height of Cardview I have to set background in my fragment . So I tried using ViewTreeObserver and Post method but I am still getting value as 0.

cardTierInfo.post {
            cardTierInfoHeight = cardTierInfo.height
            setLayoutParams(cardTierInfoHeight)
        }

Call is being made in OncreateView.

1

There are 1 best solutions below

0
Công Hải On

If you using kotlin extends core-ktx it is easy to get actual height by doOnLayout

cardTierInfo.doOnLayout {
            cardTierInfoHeight = cardTierInfo.height
            setLayoutParams(cardTierInfoHeight)
}