How to pass XML file to layout custom attribute? - android

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.

Related

why we refer the dimens.xml file in android with #dimen not #dimens?

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.

How do I use multiple xml "style sheets" in Android?

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.

Using strings.xml in raw xml resource

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.

Access from xml to other xml?

How to access from xml file to values in other xml. I need to access to xml which was created by SharedPreferences and is in folder shared_prefs.
You can do an "inner" join with XSLT

Is it possible reuse android resource file parser to read custom resource file?

Imagine the following scenario: some xml file is downloaded from server and stored on device. Inside this file is the proper android resource xml markup, for example:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="string_array_name">
<item>text_string</item>
</string-array>
</resources>
I wonder is it possible to somehow create a custom Resources object and make it read this custom xml file - so I can just reuse existing resource xml parsing mechanism?
Or is this a no-go and I must use XmlPullParser in this case?
You can't reuse resource parser because it parses binary xml files, not text xml files.
You probably could've converted your xml into Android binary format and then somehow trick resource parser (assuming this is possible). But converting text xml to binray xml on server-side is relatively harder task then just write your custom XML parser.
By the way, the aapt tool is responsible for text to binary xml conversion (as part of android build process).

Categories

Resources