Android Check if View Size Changes Rendered (Kotlin)

Jun 18, 2018
Callback when layout changed

When you make changes to view size or text size programatically, the UI changes are not rendered immediately. How do you know when the new size has actually rendered?

Use android-ktx.doOnLayout.

Performs the given action when this view is laid out. If the view has been laid out and it has not requested a layout, the action will be performed straight away, otherwise the action will be performed after the view is next laid out.

NOTE: if you are not using Kotlin or android-ktx, you can look into the source code which uses addOnLayoutChangeListener.

// change layout widthpreviewLayout.updateLayoutParams {    width = 300}// still showing old widthLog.d(TAG, width=${previewLayout.width}")previewLayout.doOnLayout {    // new width    Log.d(TAG, "doOnLayout.width=${previewLayout.width}")}

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.