Android Check if View Size Changes Rendered (Kotlin)

June 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 width
previewLayout.updateLayoutParams {
    width = 300
}

// still showing old width
Log.d(TAG, width=${previewLayout.width}")

previewLayout.doOnLayout {
    // new width
    Log.d(TAG, "doOnLayout.width=${previewLayout.width}")
}
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.