Best approach to handling strings.xml in an Android project [closed] - android

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Wanted to ask about the strings.xml in an Android project. I often see that the normal usage is to put all text for the entire App in the strings.xml. And I have also seen that strings.xml holds system strings and the rest is stored in xml files that have the name of the class. like this:
ActivityMain.java (having many text strings for user to read)
activity_main.xml (holding all text strings for ActivityMain.java)
I look trough android-best-practices but finds no mentioning of this.
Maybe this does not matter but what is your view on this, pros and cons?

To manage the strings for your project, the best approachment (From my not quite long experience) is to split the strings related to it's usage.
For example,
if you have 2 Activity: MainActivity.java with activity_main.xml (let's call them MainActivity) and SecondActivity.java with activity_second.xml, you need to split the string based on the activity name. So you need to make:
strings_main.xml
strings_second.xml
in the strings_main.xml, you need to use activityname_text pattern. For example, if in the MainActivity you have a Title TextView then you need to add:
<string name="main_title">My Title</string>
For untranslatable string in MainActivity, you can add something like this:
<string name="main_app_name" translatable="false">MyAppName</string>
<string name="main_developer_name" translatable="false">My Developer Name</string>
This approach is suitable if you have a large project where each distinct Activity (or package) is maintained by another developer. And this approach will make sure you not accidentally alter the text for other Activity than your current edited Activity.
For multiple language, we just need to make a folder based on the language. For example, for Indonesia language we just need to add values-in folder. Then we can copy the strings xml from values folder. More for Supporting Different Languages.
When adding a different language for an app (For example, Indonesia language), Android Studio will complaining about missing translation if there is a string in values but not in values-in. For example, if in values we have:
<string name="main_title">My Title</string>
But not exist in values-in then Android Studio will complaining about the non exist translation when Build-> Release the app. We can fix the error by adding translatable=false:
<string name="main_title" translatable="false">My Title</string>
We also can change the translation because there is Translation Editor in Android Studio.
For project naming convention, you can look at Ribot android-guidelines.

It all depends on your preferences. And there is really many ways of storing Strings. As many as coding styles.
One way: store all in strings.xml . This can be OK for small project,
but when quantity of strings will be over 50, all'll look messed up.
Second way: store by usage purpose. Something like login_error_strings.xml and so on. This idea is described here
Third way: store in static fields. Create some Const.class and put there all what do you want. I don't really know what is drawback of it, but there is diffidently something wrong.
Just to clarify: I think that "second way" is best way of doing this.

Related

gender-dependent locale String resources Android

I'm implementing an app in Hebrew, and I like it to be user-friendly in such way that at the first time the user logs on, there will be a question "Are you a male or a female?". After answering this question, I want most of the strings to be gender-dependent
(E.g. in Hebrew the question "Would you like some coffee?" will be
תרצה לשתות קפה?
for a male, and -
תרצי לשתות קפה?
for a female)
Meanwhile my app supports English and Unisex-Hebrew Locales, so I'm using String resources (like R.string.somevalue) and I know how to handle values-he and values-en.
Let's say I can ask for is_male() and is_locale_hebrew() at anytime, I saw this answer but it won't help my case since there are a hell lot of strings in my working-already app and I want the solution to add only xml files (hopefully) with the less needed change in my "Activity"s code.
I thought maybe overloading the parser that looks for the xml files will do the magic, but I have no clue where to start from.
My question divides into two parts:
A. How can I implement gender-dependnt String-resources?
B. (Opt) Some of the string-resources are good as unisex right now, is there any option to avoid copying those resources to the 2 new gender-dependent files and just add a default behavior of "if you don't find a string resource at values-he-male search for it in values-he"?
Thanks in advance!
Re'em
Same question for me. I plan to have (in addition to the default string.xml in English) iw (for male) and iw_fe (for female).
When the user selects his/her gender, I will change the locale (Set Locale programmatically )
As for using default values in Hebrew, I am still clueless. For now I will simply copy the Hebrew XML and change the required entries, leaving all the rest intact.
HTH
Noam

Android: Organizing Strings and String Arrays in res/values

I am working on a project that includes a lot of strings and string arrays. I would like to put them into created folders inside res/values, but I get errors when I try to do this. Either getRecources() does not recognize the new folder or the xml attributes cannot link together. I know this is a noob-ish question, but thanks for the help!
Unfortunately, you can't create any subfolders in your values folder. But you have two instruments to control the hierarchy.
String arrays are declared in the following way:
<string-array name="arr_name">
<item>Text</item>
<item>Another text</item>
</string-array>
You can access them through R.array.arr_name.
Prefixes are kind of obvious, but since you mentioned that you are a novice, it's worth mentioning. I usually prefix all of my strings depending on how they are used. For example, btn_ for the text used on buttons, dialog_ for strings used in dialogs and so on. This way autocomplete in the IDE also works much better too.
Also you can split your declarations into different files, but this doesn't have any impact at all on the way you access them, so I don't know if this can help you.
You can define array of strings using following way. Later you can access it in code with R.values.langs
<string-array name="langs">
<item>бг</item>
<item>en</item>
<item>ру</item>
</string-array>
To organise my res folder I use defined xml files not sub-folders.
Basic Example:
- if you have Strings for your Login Page put them in login_strings.xml
- if you have Strings for your Options Page put them in options_strings.xml
etc.
Hope this helps.

Android - Change value in strings.xml [duplicate]

This question already has answers here:
Change value of R.string programmatically
(8 answers)
Closed 6 months ago.
So inside strings.xml I have a string called change_bg and I'd like to change it's value according to click events.
I know that in order to get the value you use
changeBG=getString(R.string.change_bg);
But I don't know how to SET the value of
R.string.change_bg
Please let me know how.
Thanks in advance!
Dvir
You can't change resource files during runtime. Strings are hard-coded in the string.xml file and hence can't be changed during runtime. Instead of trying to edit your strings.xml file, just use SharedPreferences to store the user's preferences if that's what you're trying.
You basically must understand that strings we normally hardcode, now we do it in string.xml
the strings that are variable in nature must not be defined in string.xml
You can set its default value in onCreate() of your MainActivity i.e Launcher Activity.
You cannot change the values of strings.xml at run time. I had the same doubt, when I started with android development. Just remember that strings.xml can only be set before running the application manually and after that, you cannot modify it. You will understand the reason for that in the due course.

Android Strings

I wrote a big app with thousands of string in the code.... very bad idea, because now I want to translate each string.... big problem.
Copying all strings to the strings.xml takes a long time.
Eclipse has an option to take all selected strings and put them into messages.properties.
Does this work similiar like strings.xml? When, why all people use strings.xml.
Or should is use eclipse to seperate each string and than I should copy them to string.xml?
All people are using strings.xml because this is the normal way to do it on Android. You don't have to manage the load of the strings, to call any locale function in your script.
You can see the documentation here : http://developer.android.com/guide/topics/resources/index.html
BTW, you can easily transform your eclipse generated file to an strings.xml file after the extraction.
In Eclipse you can use the shortcut keys Alt + Shift A, S to extract an inline string in to the strings.xml file via a popup dialog - might be a bit easier than doing it by hand. And as the others say, yes you should ALWAYS use the strings.xml file so that you only have to look in one place when you want to change a string, instead of having to search through all your code.

Which strings to store in strings.xml?

Are there any suggestions on which strings you should store in strings.xml and which strings can be stored as String objects? For example, do I have to put a string into strings.xml, if I use it only to complete a certain action and then it can be destroyed? And what is the main reason in storing strings in xml? Thanks in advance for your answers.
Any string that will be displayed to the user should be in strings.xml. This is useful in case you ever want to support other languages for your application. If you do, you just create a new strings.xml file that language with translated values. You can learn more about it here.
One reason is multi-language support.
You should store the strings that you use in Activities - TextView, button's caption and so on.
You should put most constants in strings.xml, your app title, button names, textview contents...mostly things that wont change in your application.
Another reason for storing strings in xml is for localization. You can store different files for each different Locale or language, and Android will grab the correct file for the phone's selected Locale or language.
Here is a link to the String resource Android page, it will go more deeply into how the language support is done.
You don't store all the strings in strings.xml, but only strings constants related to user interface, the strings that you want to translate in different languages.
You can have different folder like :
values
values-fr
values-de
in each a strings.xml file with you UI messages translated in many languages.
Regards,
Stéphane

Categories

Resources