Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.Start your free trial
Is it a good practice to have multiple string resources?
I was wondering whether or not it is a good practice to have multiple string resources?
In the video, Ben created a class named ParseConstants, instead of just putting the constant strings (defined in the Parse 'database') into the strings.xml file. I understand the reason for this, but is it not a 'better' idea to categorize those kind of strings in a new strings resource .xml file e.g. named strings_parse.xml?
I am working on an app that hold A LOT of different types of strings that should be categorized depending on the string category they belong to. An example could be strings for toast messages. In order to keep it organized, would it not be a good practice to hold a string resources only for strings appearing in toast messages? Or should I hold all toast strings in an string-array in the original strings.xml file?
Sorry for the amount of questions in this post! I just want to know what the best practice is regarding this matter.
A best practice is to not have any hardcoded values. What Ben did with
ParseConstants is one way to do that.
Regarding strings in Android, a best practice is to put all the user-facing strings into the XML resources. That way internationalization (and plurals and other things) are easier for you as a developer when working in the Java code.
String values that will never be seen by the user don't need to go to the XML resources, but there is one case I can think of when it should go there (if the "key" is being used in the XML). Except for that use case I mentioned, putting the "key" strings in the XML resource file doesn't offer anymore benefits than encapsulating it in a Java file with
I don't know your exact use case for the many
Toasts, but if it's not used in an array or list, then I'd recommend just descriptive names as regular string resource items.
For further reading, one can visit Android strings.xml Best Practices? post at stackoverflow.
In the linked stackoverflow post, one user links to an open source project on GitHub, named Astrid. In this project, the developer(s) has/have multiple string resources which looks good because they are organized into different parts, making it easier to maintain.