I was wondering if it's possible to load resources from strings.xml in raw xml resource. Example:
<root>
<element>#string/res1</element>
<element>#string/res2</element>
<element>#string/res3</element>
</root>
Is this possible?
If it's not possible, if I create a raw xml resource, do I have to provide multiple files for localization purposes?
Are you trying to use values declared in strings.xml in a file inside /raw/? Then no, that's not possible. If you want to internationalize your "raw" XMLs you can provide one for each of your supported languages in the appropriate folder (/raw-fr/, /raw-en/, etc). At least that's out of the box.
Alternatively, you can establish a template format and parse the raw XMLs in Java and fetch the appropriate string resource from there instead.
Related
I have made a new value resource file in the android studio project as name dimens
but while referring to it from XML I have to use the attribute #dimen
I am really curious to know the referring tag must have to be the same as the file name but it is not.
I have seen the same pattern in strings and colors.
But it is like that? any clue?
name of your files doesn't matter in fact, you can rename dimens.xml to anything.xml. Or you can have dimen_activity.xml and dimen_fragment.xml files, which helps you manage them. Also, you can keep in this file <dimen tags, but also <integers and any other (e.g. you can have one sizes.xml file). Resources are built upon content inside all XML files placed in values, a kind-of map is created then and all <dimens from all XML files are available under #dimen/ or R.dimen.
While developing a library module I decided to extern all UI attributes in a metadata file using XML - which will be latter validated againts a XSD schema. Currently I have both, XML and XSD, on assets folder of my project. I want to pass the XML reference in a custom attribute for my Custom View - doing something like app:cestyle="#assets/my_xml_file". Is that possible?
Assets are not mapped in R file so you can't access them like #string or #dimen, etc.
I would suggest either using a string resource that equals asset file name or res/raw folder to hold your xml files.
I would like to use multiple xml style sheets in Android project.
I have:
res/
values/
styles.xml
some_other_styles_file.xml
and_some_other_styles_file.xml
...
What is the proper way to:
1 - reference those files
2 - reference individual styles from within different files
And:
3 - if using multiple xml style sheets, do style names need to be unique across files? In other words could two files, each being used by the app, contain styles with the same name?
File names are just for your organization, they are all automatically compiled and referenced by R.style.<style_name> when in java, or by style = "#style/<style_name>" when in xml (such as layout files). You can even mix types (e.g. have string resources in your styles.xml)
To create a set of styles, save an XML file in the res/values/
directory of your project. The name of the XML file is arbitrary, but
it must use the .xml extension and be saved in the res/values/ folder.
The root node of the XML file must be <resources>.
source
You reference them all the same way R.style.<style_name>
It does not matter which file the styles are defined (files are used for organization)
Yes the names must be unique.
I need to make an app internationalization, so I create the folder "values-zh-rCN", and copy all the files in the folder "values" to the folder "values-zh-rCN".
I translate the files strings.xml and array.xml in the folder "values-zh-rCN" to chinese, but I find I need not translate anything in the files styles.xml and dimens.xml in the folder "values-zh-rCN".
Can I delete the two files styles.xml and dimens.xml in the folder "values-zh-rCN"? Thanks!
Yes. Android will use the files in the default folder (values instead of values-xxx) if they are not present in a more specific folder.
For example I have an app with 6 files in values but only two in values-fr (strings and string-arrays), one in values-sw600dp and values-sw720dp (dimens), one in values-v11 (styles) and two in values-v17 (styles and dimens).
In each I only define the elements that are different from the default value and Android use those when appropriate and use the default when not.
I would recommend to copy strings.xml only. You shouldn't write strings directly in arrays.xml but instead reference strings contained in strings.xml :
<string-array>
<item>#string/some_string</item>
...
</string-array>
It's easier to maintain if you decide to change your array later. You won't have to edit all of your array files (it's easy to make a mistake and put a string in the wrong item).
Yes you can.
Read this article it will help you.
Best wishes.
I have a large XML file which is arranged like so:
<item><title>...</title><link>...</link></item>
How can I use parse(new InputSource()); to point to this XML file if it's stored within my project directory and where do I put the XML file?
The best answer ignores your parse(new InputSource()) request. Put it in res/xml/ and use getResources().getXml(). This gives you an XmlPullParser on your data. The big advantage here is that your XML is somewhat pre-parsed during the compile step, and so parsing is about ten times faster at runtime than with normal XML parsers.
If it absolutely positively has to use DOM or SAX, put it in res/raw/, then use getResources().openRawResource() to get an InputStream, which you can wrap in an InputSource.