I am new here.
How do we go about separating the string values for different categories(fragments,activity, etc.)
For now I put everything in one file,that is the values/strings.xml. When the program gets too big, they are all jumbled together and hard to differentiate.
any advice?
thanks,
techfang
The filename is arbitrary. You can name your strings files as you want strings_my_activity for example
I'll try to give each string a meaningful name, haha. Then, have them listed in section, use newlines to separate them.
You can have multiple string resource files, so it is perfectly allowable for you to have (for example):
res/
strings.xml
main_activity_strings.xml
main_fragment_strings.xml
sub_fragment_strings.xml
The files themselves can have any allowable name really. What is important is what is in the file. So any entry ends up resolving to R.string.xxxxxxx
How desirable this approach is, is of course another question. You may find you want to keep 'global' strings (such as OK, Cancel, etc.) in the top-level strings.xml file.
Related
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.
I'm new to Android programming. I spent a long time trying to look for a 'phantom line breakpoint' error in my code and I couldn't figure it out till I changed a string resource name.
I have an options.xml menu that contains
<item android:id="#+id/menu_about"
android:title="#string/menu_about"
android:icon="#drawable/ic_menu_about"></item>
My question - I have #+id/menu_about and #string/menu_about. is this possible?
Thank you very much
Yes,its possible,and you can retrieve R.string.menu_about and R.id.menu_about.
and in autogenerated R.java have diffrent class for that.
Yes, this is possible.
R.java itself is organized into some subclasses for strings, ids, drawables etc. It is possible to have two different resources with the same name as they will be members of different classes. However, you cannot have two of the same resources with the same name, like two strings named "foo" for example.
#+id/menu_about creates the item element with id "menu_about".... #string/menu_about in your code sets the title to whatever is stored in strings.xml file in your values folder (i.e. R.string.menu_about)
Yes you can give the same name of #+id& #string.
This both were store in different class in R.java. Like if You declare #+id/menu_about this will store in id class. etc..
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.
What's the meaning of attribute 'msgid' in strings.xml ?
How to get its value?
Attribute "msgid" is present in strings.xml if you are using string localization. For example, if you have alternate application strings for Spanish in the folder values-es, values.xml will contain "msgid".
When I look at the strings.xml for Spanish I see some long values like
8340973892742019101
What is interesting is that strings.xml for Italian and other languages contains the same msgid for the same string.
The only thing that comes to my mind is that it are some unique resource IDs, produced internally by the application. So, I do not think it makes sense to search for additional meaning in them. They are unique within the application, and that is only important.
These are operating system's strings and come from the com.android.support:appcompat-v7 package. You can see them defined here
I have two resources file in res/values directory: string.xml and names.xml
how can I retreive all resources from names.xml only
the method
Field[] x=R.string.class.getFields();
retrieves resources from both files.
how can this be acheived
thanks
I am sorry, but that is not possible. It does not matter that you have your strings split between multiple named resource files -- Android combines them all when it compiles your project.
You are welcome to use prefixes or something to identify one set of strings from another. I do that with the support code for the Android Parcel Project, to allow reusable components to each define strings without one overwriting the strings of another.