https://kotlinlang.org logo
#compose
Title
# compose
d

Daniele B

10/22/2020, 9:47 AM
The
Text
composable seems to only accept
sp
font-sizes, and not
dp
. There are some situations, where using
sp
(i.e. allowing the system to adjust the text size according to user general settings) is going to break a layout. So you want to couple the text to the layout (e.g. when you want to display a text on just one line, which has a limited width). On the traditional Android view system, it was possible to use
dp
instead of
sp
for text. How can I deal with this in JetpackCompose ?
c

cheapmon

10/22/2020, 9:55 AM
I do not know if this would be sufficient, but this is possible:
Copy code
someDp.value.sp
t

tieskedh

10/22/2020, 10:04 AM
The Density-interface can convert from Sp to Dp. You can get one using the DensityAmbient:
Copy code
val sp = with(DensityAmbient.current){
    100.dp.toSp()
}
👍 1
d

Daniele B

10/22/2020, 11:33 AM
thanks! this is working for me:
Copy code
someDp.value.sp
t

tieskedh

10/22/2020, 12:35 PM
@Daniele B do note that 5.sp will be 5.dp in your code, and not the amount that it should actually be
d

Daniele B

10/22/2020, 12:50 PM
@tieskedh what do you mean? I am actually looking for a way to apply 5dp instead of 5sp
z

Zach Klippenstein (he/him) [MOD]

10/22/2020, 1:48 PM
There's a reason this is difficult - this would be frustrating for users with low vision who can't read text at the normal size, who probably won't be super impressed that your layout works correctly because they won't be able to read the screen. This also might not be the workaround you think anyway - recent versions of Android let you change the density for everything, not just the font scale. Will your layout be broken in that case as well?
d

Daniele B

10/22/2020, 1:59 PM
If the width and height are constraint, then a bigger font would definitely break the layout. I mean, not the layout, but the readibility anyway.
z

Zach Klippenstein (he/him) [MOD]

10/22/2020, 2:08 PM
I mean how does your layout look if you change the Display Size setting and leave Font Size as the default?
d

Daniele B

10/22/2020, 2:31 PM
Oh, well. You are actually right. If I set the font larger, even those texts with dp.value.sp will get larger :(
So, isn't there any chance to make the size of a text fixed, despite changing the phone's display size settings?
z

Zach Klippenstein (he/him) [MOD]

10/22/2020, 2:36 PM
I think there's an even hackier way, but again, there's a reason all this friction exists. Layouts should adapt to support these conditions, instead of trying to disable accessibility features.
d

Daniele B

10/23/2020, 3:45 PM
@cheapmon actually
someDp.value.sp
still adjusts the size when you change Font size or Display size in the phone settings
@tieskedh actually your suggestion is working, but only if you apply directly to a composable. It doesn’t work if you want to define it within a Typhograpy textStyle. In such case it’s not able to retrieve the
current
value, which is understandable. I found a solution by creating a composable like this:
Copy code
@Composable
fun fixedFont(dp : Dp) : TextUnit {
    return with(DensityAmbient.current) { dp.toSp() }
}
and using it like this:
Copy code
Text(text = "mytext", fontSize = fixedFont(12.dp))
@Zach Klippenstein (he/him) [MOD] I agree with you in principle. But I believe this is something that should be controlled by the developer. Same as it’s happening in the traditional Android system. There are some text elements that are better to be displayed as fixed font-size, rather than risking to be displayed only partly.
One example is the text in
BottomNavigationItem
s you don’t want it to vary based on the device textsize settings.
z

Zach Klippenstein (he/him) [MOD]

10/23/2020, 4:46 PM
In that case, if the density is so low that a bottom bar can’t reasonably be displayed, it should probably fall back to a different navigation idiom, or maybe hide text, show just icons instead? Again, if your user has low vision, it won’t matter if the text is displayed at all if they can’t read it anyway. Granted, this is a design question, but I think that Compose is absolutely right to make it very difficult to not answer accessibility questions like this.