Is there a way to not resize the button content wh...
# compose
p
Is there a way to not resize the button content when we simply place a CirculerProgress inside ?
s
Can you share a video?
c
Button by default sizes to content, so you’ll have to set a fixed width/height with Modifiers
p
Then if we make the fixed width the text can be trimmed. What I want is that after the button gets its content i.e. so text and icon. I can simply add a loading indicator without the button resizing when I set Loading true. I already have fixed height and that’s ok but I don’t want buttons to have fixed width since they are part of our design system they need to be flexible to be use in many places.
s
So you say you want the button not to resize and then you say you want the button to adapt its width. That's why I said giving us a video is probably a good idea, of what is happening and what you'd expect to happen.
p
like this
s
Yeah so this looks like there’s no resizing. So you’ve managed to do what you’re trying to do or am I misunderstanding something?
p
yes I did it using
Copy code
val widthButtonCached = remember { mutableStateOf(width) }
        val density = LocalDensity.current

.defaultMinSize(minHeight = height, minWidth = widthButtonCached.value)
.onGloballyPositioned {
    if (widthButtonCached.value == Dp.Unspecified)
        widthButtonCached.value = with(density) { it.size.width.toDp() }
}
now the question is this ok in terms or performance?
and this only works if the initial state of the button isn’t loading
I DID IT 🙂
actually without litneing but it will have one layer
so I have a Box where I place the button
s
There’s various alternatives here • Make the button centered to the screen and animate the width so that this feels more fluid at least • Still keep the text under it, but with alpha 0f to keep the exact size that you’re looking for. • Cache the old size as you were doing, which I don’t love tbh • Actually put an absolutely fixed size on the button. We typically do this in our design system buttons, where it’s fillMaxWidth always anyway so it’s no problem
p
I removed cache now
I added a layer on top of the button
so in reality the text is there
so the structured looks like. BOX { BUTTON { ICON TEXT } BOX { CircularProgress } }
s
Good job! As long as you know what the text should be and it’s not gonna increase in size, I think that’s what I would’ve done too.
p
even it changes the box with progress has the following
Copy code
Box(
    modifier = Modifier
        .matchParentSize()
so it should rezise
or should I also test that 🙂
🧐
yes it works 🙂
thanks guys 🙂 some how explain the problem made me see the solution
s
The classic rubber duck debugging. Super high success rate in my experience 😄
c
That solution is what I would’ve done. That is to use a Box and positioning the elements over each over versus trying to put the spinner in the Button
Or just toggle alpha of the text in the button between 0 and 1, to keep the button content width set
p
@Chris Sinco [G] @Stylianos Gakis which one is more performant ? Layer or just colouring alpha?
my guess is Color is better for performance but I want to know your opinion
s
I'd just toggle alpha for clarity code reasons. I really doubt performance would be an issue here, and I wouldn't prematurely optimize for it unless you benchmark and notice an actual issue.