Why should we use Strings from XML with Jetpack Compose?

110 Views Asked by At

If the idea of Compose is to get rid of XML, why is it still recommended to use XML based Strings and String Resources?

object Strings {...}
object Sp {...}
object Dp {...}

In a .kt file sounds like a much better way to handle strings in compose. And sp/dp values

1

There are 1 best solutions below

0
Rakib Hasan On

Yes, Jetpack Compose is to move away from XML-based UI definitions, but there are situations where using XML for strings and string resources is still recommended. This is mainly for the sake of maintaining compatibility and leveraging existing localization tools. See below for greater clarity -

  • Localization: XML resources are well-established for localization workflows. String resource files can easily be translated into different languages and managed with tools like Android Studio's built-in translation editor. Defining strings in Kotlin files would make localization more complex and require custom workflows.
  • Resource sharing: String resources can be shared across different parts of your app, including both Compose and traditional view-based components. Using them in XML ensures consistency and avoids duplication of strings. If you defined them in Kotlin files, you'd have to handle sharing and management manually.
  • Platform features: XML resources can leverage platform features like string formatting and pluralization directly. While it's possible to achieve some of these in Kotlin, the process becomes more complex and less standardized.

However, your suggestion of defining Sp and Dp constants within Kotlin files has merit. These values typically relate to specific themes or UI elements and are often not meant to be translated or shared beyond your project. Keeping them within your codebase can simplify management and improve readability.

Hope you understand!