Jetpack Compose: Whats the difference between Modifier.heightIn(...) vs Modifier.prefferedHeigh(...)

Viewed 813

Exactly what the title says. Whats the difference between Modifier.heightIn(...) and Modifier.prefferedHeigh(). They seem to work the same way in my trials.. Does anyone knows when to use what and on what scenarios?

1 Answers

Modifier.preferredHeight() sets the height which can be overridden by constraints, instead of Modifier.height, that sets the height regardless of the incoming constraints.

Modifier.heightIn() forces the content to comply with this constraints and centers it in the case that the content is larger than maximum or smaller than minimum.

On a side note, these two functions serve different purposes since the first one take two arguments and the second takes just one, because of that, they would hardly do the same thing.

Notice there are other functions for width and height, as well as "size", which includes both of them. By looking in the source code (Ctrl + click | Cmd + click), it's possible to see all possibilities and documentation explaining the functionality.

P.S.: you misspelled preferredHeight, it took me a couple of minutes to find it in Android Studio.

Related