Pablo
02/04/2024, 1:11 PMModifier
.fillMaxSize()
.wrapContentSize()
They do it in a compose sample of a Android development guide from official android guides. ( https://developer.android.com/codelabs/basic-android-kotlin-compose-build-a-dice-roller[…]abs%2Fbasic-android-kotlin-compose-build-a-dice-roller-app )
I mean... fillMaxSize does that the view fills the entire screen, and wrapContentSize does that the view uses only it's size.. so I don't understand why use both at same time. They do it to center the view, passing an argument to wrapContentSize, but I just can't understand this way of achieving it. I mean.. if you wanna center a view, you supposedly put that view in a bigger container and center it, but passing a "fillMaxSize" and a "wrapContentSize" to center it is very strange.Chrimaeon
02/04/2024, 3:06 PMPablo
02/04/2024, 3:40 PMPablo
02/04/2024, 3:41 PMPablo
02/04/2024, 3:41 PMPablo
02/04/2024, 3:41 PMChrimaeon
02/04/2024, 3:44 PMfillMaxSize
modifier tells the measurement phase that this view wants to consume the max available space. that’s then mesured and passed to the next modifier, the wrapContentSize
, which then calculates with the fillMaxSize
contraints to shrink back down to wrap the content and with the alignment parameter used in the codelab, center the view.Chrimaeon
02/04/2024, 3:45 PMromainguy
02/04/2024, 6:02 PMPablo
02/05/2024, 10:58 AMPablo
02/05/2024, 10:59 AMPablo
02/05/2024, 11:05 AMPablo
02/05/2024, 11:08 AMPablo
02/05/2024, 11:08 AMZach Klippenstein (he/him) [MOD]
02/05/2024, 5:31 PMwrapContentSize
. It will center its content in the minimum constraints if smaller, but most of the time it will have a minimum size of 0, so it won’t need to center. It’s up to the layout in which that’s placed to decide whether to pass a minimum size or not, and then where to place that minimally-sized element within the available space. Many components will default to placing it in the top-start corner, i.e. IntOffset.Zero
. To override that behavior, that’s why you need fillMaxSize
, so the parent can place it at Zero
and wrapContentSize
can then use all that space.Pablo
02/06/2024, 9:17 AMPablo
02/06/2024, 9:18 AMPablo
02/06/2024, 9:18 AMPablo
02/06/2024, 9:23 AMChrimaeon
02/06/2024, 10:24 AMModifier.background(Red).padding(8.dp).background(Green)
So you set the background twice on one composable but it will look like having a border. 😉Zach Klippenstein (he/him) [MOD]
02/06/2024, 6:48 PMZach Klippenstein (he/him) [MOD]
02/06/2024, 6:50 PMBox(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
YourComposable()
}
In this case, Box by default doesn’t propagate min constraints, so wrapContentSize
is not needed. The outer box is still required to fill max size, otherwise it would shrink to the the size of YourComposable
.romainguy
02/06/2024, 6:50 PMChrimaeon
02/06/2024, 6:53 PMZach Klippenstein (he/him) [MOD]
02/06/2024, 7:27 PM