For the old XML layouts I always use the dimen.xml file to define almost all dimensions, paddings, margins, etc.
While learning Jetpack Compose I find that all repositories, examples, etc., hardcode all dimensions related values directly into the compose code, even though I am aware that there is the possibility to use the dimensionResource method to read it from an xml file.
What is the recommended way for Compose regarding this? to keep using dimen XML files, or to hardcode it into the actual compose code?
Google's official documentation mentions that resource dimensions can be used with compose using dimensionResource()
like below:
Box(
modifier = Modifier
.padding(
horizontal = dimensionResource(id = R.dimen.padding_horizontal),
vertical = dimensionResource(id = R.dimen.padding_vertical),
),
)
I also prefer using them. I've seen a post on Medium where the writer suggested using kotlin classes and objects to store dimens and then using some formulas to calculate which class must be used based on the screen dimension. Still, personally, I don't suggest going that road. The dimens are already sufficient enough, both in stability and ease of use.