As previous answer stated - your view is not measured yet. Have a look at Android KTX - Kotlin extensions under Jetpack, especially this one:
View.doOnLayout((view: View) -> Unit)?)
This extension function makes sure that the provided action is executed once the VIew is laid out or, if it was already laid out, it will be called immediately.
https://developer.android.com/reference/kotlin/androidx/core/view/package-summary#(android.view.View).doOnLayout(kotlin.Function1)
This article explains in detail how Views are measured, laid and drawn and what you as developer can do to ensure you get correct size of your View. It also goes over usually recommended solutions like View.post()
and registering OnGlobalLayoutListener
and explains what can go wrong when using them.
https://cheesecakelabs.com/blog/understanding-android-views-dimensions-set/