I would like to understand how is better to use dp and sp values in the Compose project. I checked several open-source Compose projects, and most of them are hardcoding the dimensions. But it's definitely not a way to support flexibility and different screen sizes. I see a few ways for now:
- Using the old way with
dimens.xml
and get the values directly in the compose functions callingdimensionResource()
. - Having references to the values of
dimens.xml
in a Kotlin class. For example:
class AppDimensions {
val paddingSmall: Dp
@Composable
get() = dimensionResource(R.dimen.padding_small)
...
}
- Not to use the
dimens.xml
and implement your own logic for different screen sizes. For example:
class AppDimensions {
val paddingSmall = when(screenSize) {
Compact -> 10.dp
Medium -> 16.dp
Expanded -> 20.dp
else -> 10.dp
}
...
}
I like the 3rd variant because it seems more flexible and allows us to avoid returning to XML. But it will require efforts to support it.
But maybe, can we use it in some more convenient way?