How to get string resources from a certain file? - android

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.

Related

separate values/strings.xml android

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.

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 one values folder for multiple languages

Is there a way to tag a strings resource folder with more than one language(values-en-es)?
My problem is that for Hebrew on some devices the language code "iw" and on others it is "he".
My current solution is to make two folders with the same content and only change their name
respectively.
I wonder if there is a more accurate way to do it?
Resource folder names can have multiple qualifiers but only one qualifier per type:
For example
values-en-rGB //Language + Region
is valid but
values-en-fr//Language + Language
is not valid, since it has multiple values for a single qualifier. So
values-iw-he
is not possible.
Source: Android Developers, Qualifier Name Rules.
However this doesn't mean you have to duplicate the files. Instead, you can create an Alias Resource.
Android Developers explains Alias Resouces like this:
Creating Alias Resources: When you have a resource that you'd like to use for more than one device configuration (but do not want to provide as a default resource), you do not need to put the same resource in more than one alternative resource directory. Instead, you can (in some cases) create an alternative resource that acts as an alias for a resource saved in your default resource directory.
For example, a String resource in one folder
<string name="app_name">My Awesome App</string>
can be referenced in another String resource in another folder as:
<string name="application_name">#string/app_name</string>
More about alias-resources on Android Developers.
You can make a File Link in eclipse, as described here.
So you have your values-iw/strings.xml with the real values and you make a File Link to that file in your values-he folder. This has the benefit that you do not have to edit 2 files, the linked 'file' gets updated automatically.

Can localization resources be downloaded in runtime?

I have application with internet access and don't want to store many string.xml files for different languages.
What I want:
Application contains only one string.xml with english strings.
When user launches it - I see user's phone locale, and ask my own server for necessary language file. (resource keys will be the same)
When new resource file comes, all interface should work with new file.
The question is how to change existing or add new string.xml file in runtime?
You obviously cannot change, download or remove strings.xml at runtime
If you want to store locations, you will have to use SQLite storage to store translations.
similar:
How to modify strings.xml file at runtime
run time modification of strings.xml
Now I can see only solution:
Create some "localization proxy" that will return me necessary resource (from string.xml or downloaded resource)
Replace all getString() and getText() to your own method getStringFromLocalization
Override TextView, Button and some other views with custom one and change there init and setText methods.
Overriding the standered resource/language using resource files which are complied time then your scarifying performance over customization. Do it only if u need this.

Change string.xml at runtime

I have a special request to implement dynamic language packs and themes in Android. This basically means:
to download a zip file containing a file named strings.xml (containing the translation)
replace in the application the file /res/values/strings.xml with the one downloaded
Is this possible? Thank you for your help.
No. Things inside the Res folder are static and you can't recreate the R class in runtime.
Why don't you try using sqlite for your situation?

Categories

Resources