I have some code like this in my Android settings xml file (res/xml/settings.xml):
<EditTextPreference
android:title="#string/settings_date_format"
android:summary="#string/settings_date_format_summary"
android:key="settingsDateFormat"
android:defaultValue="#string/config_settings_date_format_default_value" />
And a string like this:
<string name="config_settings_date_format_default_value">dd MMM yyyy, HH:mm</string>
As you can see, I'm setting the default value to a string. However, I'd like to have these default values stored somewhere else as it doesn't make sense to put them in res/values/strings.xml
Is it possible for me to put them in a new file called "config.xml" for example?
You can just add another XML file in the values folder and name it however you want.
On compilation time, the compiler will scan all your resources folders, regardless of the files and their names, and will create a lookup table for all your resources.
You can have multiple files with string constants in them. You just have to follow the file naming rules and make sure not to duplicate any of the string or file names.
Related
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.
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.
I have two xml files in values folder for strings:
new_strings.xml
strings.xml
From strings.xml I can access string as follows:
String str = getString(R.string.app_name);
How can I directly access from new_strings.xml?
By the same way you're accessing the values in strings.xml file.
Example :
strings.xml :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name_1">First app name</string>
</resources>
new_strings.xml :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name_2">Second app_name</string>
</resources>
In your java code you can do :
R.string.app_name_1
R.string.app_name_2
and you can access both values which are in two different xml files.
As the doc said:
file location:
res/values/filename.xml
The filename is arbitrary.
The <string> element's name will be used as the resource ID.
compiled resource datatype:
Resource pointer to a String.
To give you a really short answer:
Yes, you can access to all strings in both files regardless of the filename. Although it's generally a good practice to keep all strings in one file, I do personally find it useful to separate strings into multiple files, especially for localization purposes, which brings me to the next point:
Yes you can access to the localized strings in other locale folders, e.g. values-es, as long as it contains a translated copy of all string files in the values folder.
For example, if you have strings.xml and new_strings.xml in your values folder, make sure you also have both of those files in your values-es folder in order for your app to display the localized strings in Spanish.
Hope this clarifies things up for you =)
When we add some entries in the strings.xml file or layout.xml file, then the R.java file gets modified automatically. Again, if we want to refer something from the layout file such as reading the EditText value entered by the user, then again we refer the R.java file at our java code to read the values.
What is this R.java file all about? The values of each entry of this R.java file seems to be in HEXADECIMAL format but what is its use?
I have read the doc but i get fairly confused for this R.java :(
Please someone step forward and explain what is this R.java file all about :(
Regards,
http://developer.android.com/guide/topics/ui/declaring-layout.html says:
android:id="#+id/my_button"
The at-symbol (#) at the beginning of the string indicates that the
XML parser should parse and expand the rest of the ID string and
identify it as an ID resource. The plus-symbol (+) means that this is
a new resource name that must be created and added to our resources
(in the R.java file).
The R.java file is generated by the Android Resource Manager (aapt.exe) and contains references to all resources of your app. Each reference is a unique id (public static final int). These constants are written to the R.java file in hexadecimal format. The logic of assigning specific integer to each resource is private to the Android Resource Manager. You can look at the source code of aapt.exe on the internet, e.g. at http://gitorious.org/rowboat/frameworks-base/trees/d58fb97ddf052b3ceac921ac7e936af990392b2c/tools/aapt
It turns resource objects into Java recognizable names for you to refer to in your code.
The R class is basically the way Android provide resource access from within your code. As you wrote when you alters strings.xml etc on save of that resource file Android SDK will recompile the R class to make your changes accessible for within your code. What the values in R class are is more or less not important since its used by Android internally to map, for example a string to an ID.
To reference a string from within your code you use R like this:
R.string.MyString
If you string in string.xml is called MyString. Same for layouts etc.
I guess this is what you read, but otherwise it's pretty good explained here.
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.