Has anyone encountered issues with text width calc...
# compose
m
Has anyone encountered issues with text width calculations like this before? I'm using a custom button wrapper (it's a material 3 button under the hood with styling). This button is (unfortunately) used in a ComposeView that is in a Fragment that gets rendered in an xml based nav graph. On first viewing of the screen it's perfectly fine. However, on the second viewing, it's unexpectedly wrapping the text, and this continues until i completely quit the app and start over, even if i exit the activity. I've confirmed through the layout inspector that the width of the Text composable is the same (44dp) in both instances, and that all of the text style properties are also identical. The only difference is it seems like it doesn't think it can fit the last character in the text on subsequent viewings. I would have expected that if it needed more room for the text, it would have recomputed the proper intrinsic width when the new composition happened and made the Text wider. This seems to happen on a Pixel 9 image running API 35, targeting API 35 under compose 1.7.8
z
I’d file a bug to have the text team look at this. Might be an issue with measurement caching or something, I think i saw a fix re: caching and intrinsically land a while ago? Cc @Grant Toepfer
m
It definitely feels like a sizing issue on the Text element. I measured the text manually in code, and it's 44.57143dp on both cases. Both cases also report the width of the Text as 44dp via the layout inspector.
Copy code
private fun measureTextWidth(text: String, style: TextStyle): Dp {
    val textMeasurer = rememberTextMeasurer()
    val widthInPixels = textMeasurer.measure(text = text, style = style).size.width
    return with(LocalDensity.current) { widthInPixels.toDp() }
}
If i manually set the size of the Text element to 1 dp larger than this, the button is fine in all viewings.
Copy code
val measured = measureTextWidth(
                text = stringResource(id = R.string.apply_button_label),
                style = LocalTextStyle.current
            )

            Text(
                modifier= Modifier.width(measured.plus(1.dp)),
If i don't add that extra dp, it's always wrapping.
g
API 35 only? This may be fixed via https://issuetracker.google.com/391378120
You can check if that fixes it by updating your sample to the most up to date compose release (including on-stable) which as of now is
1.8-rc01
m
Thanks @Grant Toepfer I will check on this.
Been reading through that and it definitely seems like this applies to this situation. It seems to mostly happen for a very specific custom font we're using, but not the other font (a sans based variant). We're getting rid of the problematic font soon anyway, but in the mean time, we can just force the button to be full width, along with the contained text and it will avoid the issue.
👍 1