Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Organizing Strings.xml

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?

like image 401
nebkat Avatar asked Jan 04 '11 18:01

nebkat


People also ask

What are strings xml?

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).

Why do we store strings as resources in xml?

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.

What is strings xml used for?

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.

Why should you use string resources instead of hard coded strings in your apps?

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).


2 Answers

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.

menu_strings.xml

<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> 

dialog_strings.xml

<resources>     <string name="dialog_cancel_yes">Yes, cancel.</string>     <string name="dialog_cancel_no">No, do not cancel.</string> </resources> 
like image 177
Ginger McMurray Avatar answered Oct 14 '22 01:10

Ginger McMurray


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.

like image 41
Kevin Coppock Avatar answered Oct 14 '22 00:10

Kevin Coppock