I am using the sample code in this question(getWidth() and getHeight() of View returns 0).
It had the following code,
view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
view.getHeight(); //height is ready
}
});
And I wrote it in Kotlin like this
image_view.viewTreeObserver.addOnGlobalLayoutListener{
image_view.viewTreeObserver.removeOnGlobalLayoutListener { this }
Log.d("Should be called once", "Height = " + image_view.height + ", Width = " + image_view.width)
};
The result was,
11-22 19:18:33.319 4754-4754/loser.qrcode D/Should be called once: Height = 672, Width = 672
11-22 19:18:33.328 4754-4754/loser.qrcode D/Should be called once: Height = 672, Width = 672
So, it was called twice. That is, removeOnGlobalLayoutListener
did not work. What should I use instead of this
?