Ashok
Ashok

Reputation: 2706

Jetpack Compose Resources best practices

I beginner in Jetpack Compose. I have been working with Android-XML till now and there are clear best practices, like code goes in Java/Kotlin folder and all the resources goes into Resource folder. Also, resource have configuration (screen-size, locale, etc.) specific folders to override the resources data accordingly.

But there is no clear documentation or guideline in case of Jetpack compose. I can understand that the drawables needs to be kept in the resources folders, but for other resources there is no clear guidelines.

Like: Dimentions can be directly placed with composable or could be provided via singleton object or via LocalComposition ( Ref: Supporting different screen sizes on Android with Jetpack Compose ) or they could be placed in dimens.xml and resolved via dimensionResource.

Same is also true for Strings and Color.

Since there are so many choices, I am not sure which is a best practice to be followed while implementing a medium to large scale project.

Upvotes: 2

Views: 833

Answers (1)

tyg
tyg

Reputation: 15673

With compose, a lot of the resources you need can/should/must be moved to code. That applies to, among other things, all ui layouts and components, themes and colors, animations, a lot of the drawables (including most icons from https://fonts.google.com/icons).

Device dimensions are handled differently now in compose. The size of your app's viewport is not only dependent on the device's hardware, it can also change any time when a user resized your app window, moves your app from one display to another (some devices have multiple displays), uses a foldable display, and so on. This cannot be adequately addressed by a static configuration in your xml files. Instead, your activity can calculate a WindowSizeClass during runtime that your composables can then use to decide how to display the data. Some composables already automatically choose an appropriate layout for you, like NavigationSuiteScaffold (bottom navigation or navigation rail) or ListDetailPaneScaffold (list only or list and details), depending on the available space. Also see https://developer.android.com/jetpack/compose/layouts/adaptive for more on the topic of adaptive layouts.

String resources, on the other hand, are not touched by compose and therefore stay where they have always been.

For all the configuration that is now in code you can choose a location of your liking, there are no dependencies on fixed file paths. After all, it's just code and can be imported like everything else.

You can use Android Studio's Empty Activity template when you create a new project to compare how some of it is handled there, especially themes and colors.

Upvotes: 1

Related Questions