I would like to use the viewTreeObserver so I can listen to when the layout is finished loading and then get coordinates of some views in that layout. I followed the advice here: How to get the absolute coordinates of a view
I translated the code to Kotlin, however the function in the listener is never being called. My code is very simple:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val layoutInflater: LayoutInflater = LayoutInflater.from(applicationContext)
val view: View = layoutInflater.inflate(R.layout.activity_main, null)
view.viewTreeObserver.addOnGlobalLayoutListener(object: ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
Log.d("TAG", "Called!!!")
}
})
setContentView(R.layout.activity_main)