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.
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 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 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.
I'm doing Tutorials and I'm on section about images. It says to put them into the folder res/drawable. But I don't have that folder, I have three instead: res/drawable-hdpi, res/drawable-ldpi and res/drawable-mdpi. So whats the difference between them?
Im using this tutorial.
One of the steps is:
Create a strings.xml file in
res/values/ and edit the file to look
like
There already is strings.xml, combined with the above, telling me to use res/drawable, are these tutorials out of date?
This tutorial has code like:
R.id.spinner
R.array.planets_array
R.layout is just simple enum. Uses the main.xml in the layout folder. But where are R.id and R.array to come from. Because it is coming up in eclipse saying it doesn't know what they are. R.java gets updated automatically, so can someone tell me from reading that tutorial where id gets added to R? It says that
The R.array.planets_array ID
references the string-array defined
above
Only it doesn't work. I doubt it makes a difference that i didn't make strings.xml since it's the same filename in the same location. But since R.java is meant to be updated automatically I don't know how to fix this.
Those are for the different screen resolutions for the range of devices that are out there. Read about supporting multiple screens on the Android dev site.
Just so you know where the R stuff comes from.
The R.java file is a generated file which contains some kind of pointers to a resource in your application. It is a simple integer actually which uniquely identifies the resource in the internal resource management system of Android.
R.string identifiers are generated from resources XML files like this one for example.
<resources>
<string name="test">This is a test string.</string>
</resources>
R.array identifiers from string array XML files.
<resources>
<string-array name="days_of_week">
<item>Monday</item>
<item>Tuesday</item>
<item>Wednesday</item>
<item>Thursday</item>
<item>Friday</item>
<item>Saturday</item>
<item>Sunday</item>
</string-array>
</resources>
You can access that array using its identifier R.id.days_of_week now.
R.id identifiers are a bit special.
They are generated in two ways. The first one is when you define a View in your XML layout file using the #+id/... syntax. Note the + sign.
The other way is to define them in resource XML files like strings for example.
<resources>
<item type="id" name="first" />
<item type="id" name="second" />
</resources>
You'd then just use them in a layout XML file like this #id/first. Note that there is no + sign anymore as you reference it, before you were declaring it.
In code you then use it like this, R.id.first.
There are a lot of other resources. I'd like to point you to the Application Resources article and also make sure to checkout the Resource Types sub-article.
If you don't have the folder, just create it. It is basically the fallback for the case that you don't have a resource in a more specific folder like res/drawable-hdpi
The *-xx folders allow you to provide more specific drawables (images) for various screen resolutions.
The same principle applies to values/ and values-xx/ where xx is a country code ; the xx versions allow you to have translations for UI messages.