I'm making an android app and since I've just started I want to try get the most organised code/resources. In my strings.xml file so far I have this:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">GameController</string> <string name="stop">Stop</string> <string name="start">Start</string> <string name="preferences">Preferences</string> <string name="back">Back</string> </resources>
All of the strings except app_name are used in an options menu. But since I will be adding much more strings I was thinking that it might be better to do something like this:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">GameController</string> <string name="menu_stop">Stop</string> <string name="menu_start">Start</string> <string name="menu_preferences">Preferences</string> <string name="menu_back">Back</string> </resources>
Is it the best way or should I use another system?
A single string that can be referenced from the application or from other resource files (such as an XML layout). Note: A string is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file).
The purpose of strings. xml (and other *. xml resource files) is to regroup similar values in one place. This facilitates finding values that would be otherwise buried in the code.
String. xml file contains all the strings which will be used frequently in Android project. String. xml file present in the values folder which is sub folder of res folder in project structure.In Android Studio, we have many Views such as TextView,Button,EditText,CheckBox,RadioButton etc.
It allows you to easily locate text in your app and later have it translated. Strings can be internationalized easily, allowing your application to support multiple languages with a single application package file (APK).
It depends on where the strings will be used. If "stop" will never be used anywhere but in a menu, calling it "menu_stop" is a good idea. If it'll be used all over the place then it should just be called "stop".
Also, XML comments are very useful for organizing resources.
<resources> <string name="app_name">GameController</string> <!-- Menu Strings --> <string name="menu_stop">Stop</string> <string name="menu_start">Start</string> <string name="menu_preferences">Preferences</string> <string name="menu_back">Back</string> </resources>
Finally, if you find you have tons and tons of string resources you may want to go so far as to separate them into different xml files: menu_strings.xml, dialog_strings.xml, etc.
<resources> <!-- Menu Strings --> <string name="menu_stop">Stop</string> <string name="menu_start">Start</string> <string name="menu_preferences">Preferences</string> <string name="menu_back">Back</string> </resources>
<resources> <string name="dialog_cancel_yes">Yes, cancel.</string> <string name="dialog_cancel_no">No, do not cancel.</string> </resources>
This is kind of a subjective question, really. You should use whatever you find easier to handle. I certainly do the second type of naming when I'm using layouts and drawables (e.g. button_x, ninepatch_x, icon_x, etc.), just because it keeps them next to each other, and is easier to narrow down quickly with Content Assist. In XML, you can use comments to group them together, and add white space, just anything that makes it easier for you to find what you need, and quickly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With