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.
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.
Background
Lint has a relatively new feature, so that it will warn us about missing translation only for languages that we choose, but i don't get how to use it.
The problem
for some reason, Lint still warns me about languages that i don't intend on translating yet.
What i've tried
for example, currently i want to only have 2 languages : english ("en") and hebrew (which is sadly both "iw" and "he" ) .
so i have strings files in the folders :
values (for english)
"values-he" and "values-iw" (for hebrew) .
i've tried putting the new attribute in the english file as such :
<resources xmlns:tools="http://schemas.android.com/tools" tools:locale="en,iw,he">
...
The question
what is the right way to do it?
Looking here it seems that it's to be used into resource files to indicate the default language. So you can specify only one locale code.
should correspond to a language
Moreover it seems to be used only to disable spell-checker
If you read the article:
This lets you tell the tools which language you're using in your
base values folder. For strings in for example values-de or values-en it's obvious, but not in the base "values" folder
It need only to know what is the language in the default "values" folder (the folder without any attribute).
<resources xmlns:tools="http://schemas.android.com/tools" tools:locale="en">
You are already in right direction. Just need some modification. Like this manner:
<resources xmlns:tools="http://schemas.android.com/tools" tools:locale="es">
Now we know that the language used for strings in the default values folder is Spanish rather than English.
Used by: Lint, Studio (to disable spell checking in non-English resource files)
Reference Link: Go to here tools:locale
Thanks.
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.
Similar to how you can have two folders res/values and res/values-xlarge, and in each you can have a dimens.xml file with different dimensions...
I want to be able to store some integers (not sizes in dp or anything like that, just plain integers) like that. Is this possible to do in .xml? Or if not, how could I do that programmatically?
If you must know why: I am displaying a list where the user can choose to show the number of results at a time. For smaller screens, I want the default list length to be smaller than the default list length for larger screens.
Yes, there's a mechanism exactly like that. Just create an XML file in res/values and related folders and instead of something like:
<resources>
<dimen ...>
</resource>
You can provide an integer resource:
<resources>
<integer name="i">234</integer>
</resource>
However, in your specific scenario I'd also consider if simply changing the related layout file makes more sense.
Sure, you can do that. Just like storing a string Ressource you can store Integers.
Taken from the documentation:
XML file saved at res/values/integers.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="big_screen_width">12000</integer>
<integer name="small_screen_width">320</integer>
</resources>
And the code would look like that
Resources res = getResources();
int bigScreen = res.getInteger(R.integer.big_screen_width);
You can store things programmatically by using SharedPreferences.
I am teaching myself Android using Eclipse, the Android plug-in, and Sams "Teach Yourself Android Development" book. I have this weird little problem. I've been able to create xml files in the res/values directory that hold strings and color values (colors.xml and strings.xml). I have been able to reference these values in the properties of my Android screens (the xml in res/layout), for example setting the "Text" and "Text color" properties with references like "#string/topTitle" and "#color/titleColor," where topTitle and titleColor are defined in the xml files.
BUT: when I create a file called "dimens.xml" and have font sizes in it, Eclipse correctly puts this file in res/values, but when I try to reference these values, e.g. "#dimension/titleFont" I get an error "No resource found that matches the given name." I've tried lots of different names, I've tried "#dimens" instead of the type, still nothing. If I go into the layout xml file and set it explicitly to a font size, e.g. 22pt, it works.
So Eclipse recognized my "dimens.xml" file when I made it well enough to put it in res/values, and lets me edit it, and shows it full of (dimension) values. It just doesn't recognize my referring to it in other xml files.
The book I'm using doesn't actually show a dimension example so I must be doing something wrong. I checked the Android docs but couldn't see any problem.
Any help appreciated. Thanks.
The correct way to refer to a dimension variable (stored in your dimens.xml (don't think the name here really matters though, it's what's inside that does)) from another xml file is like this:
"#dimen/nameOfVariable"
Notice that it is neither dimension, dimensions or dimens, but dimen!
If you look inside your xml file where you have your values, this will make sense as dimen is the name of the xml elements storing dimension values:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<dimen name="someDimension">5dp</dimen>
<dimen name="anotherDimension">10dp</dimen>
</resources>