Viktor
Viktor

Reputation: 656

When do I need to use layoutParams(setLayoutParams) or updateLayoutParams?

I've read answers of this question, but I still don't get it. For example, I do this:

val params = textOptions.layoutParams as ConstraintLayout.LayoutParams
params.bottomToTop = ConstraintLayout.LayoutParams.UNSET
params.bottomToBottom = 0

and my changes apply immediately, without setLayoutParams. But if I use it in some listener, then It's not always applied immediately without layoutParams method. So, when exactly should I use layoutParams and Is it better(for kotlin) to use updateLayoutParams?

Upvotes: 0

Views: 3937

Answers (1)

laalto
laalto

Reputation: 152817

After changing layout parameters, the framework needs to recompute the layout for the changes to take effect. This is called a layout pass.

In some situations there, there's a layout pass requested by some other thing your app did, and the layout pass is actually performed later when the UI thread Handler is free to process other messages.

In some other situations, the re-layout triggers much later and the layout changes only take effect at that time.

You can use requestLayout() or setLayoutParams() on the parent view, or the kotlin extension updateLayoutParams() wrapper for setLayoutParams() to explicitly request a layout pass as soon as possible.

Upvotes: 3

Related Questions