Hi everyone, Good morning/afternoon, <#CJLTWPH7S...
# android
b
Hi everyone, Good morning/afternoon, #compose #jetpack-compose #android I have a dimen file where I have text sizes. I need to access in my Composable function. I tried with dimensionResource() but receiving error as "Type mismatch" Is there a way we can achieve in Jetpack compose. Please find below code with error <?xml version="1.0" encoding="utf-8"?> <resources> <dimen name="text_size_small">12sp</dimen> </resources> I tried to access as below: val Typography.cguxTextFieldH1: TextStyle @Composable get() { return TextStyle( fontFamily = FontFamily(Font(R.font.roboto_regular)), fontWeight = FontWeight(400), fontStyle = FontStyle.Normal, fontSize = dimensionResource(R.dimen.text_size_small) ) } Error Received: Type mismatch. Required: TextUnit Found: Dp
g
fontSize uses SP, not DP, so font could be scaled without scaling UI
b
Yes . But is there a way can we access SP from dimen.xml similar to dPs?
g
I think the only way is to get value and convert it to SP
it’s actually a good question
b
I can try as below , but not sure whether its correct approach:
Copy code
dimensionResource(id = R.dimen.cgux_text_size_big).value.sp
g
should be fine, because dp resolved value in pixels from resources using current configuration, converted to dp and you just convert it back to sp
when you resolve it from resources you always get value in pixels, it doesn’t distinguish dimension points, though I agree that semantically it doesn’t look right, but result should be correct
b
ok sure. Thanks Andrey . I will follow this approach and check how it renders on UI. Thanks for your suggestions 🙂
g
Just realised that never had this case in our code, specifically with SP
b
Then how are you handling Textsizes in composables ? Are you directly providing as 16.sp or 5.sp like that instead of referring from dimen.xml ?