Can you reference a drawable if the file name is simply a number?
Example: If I have an image name "1.jpg" in res/drawable it will give me an error if I try to reference it using R.drawable.1
If I have an image name "one.jpg" in res/drawable it will work fine if I try to reference it using R.drawable.one
Short answer: No.
Longer answer:
The filename of every included resource is used in packaging - it will be assigned as the field name for a public static final int
in the R.java file. This means that resource names, among other restrictions, must also satisfy the conditions for Java variable names.
Note what one of the Java tutorials reminds us about Java variable naming:
A variable's name can be any legal identifier — an unlimited-length sequence of Unicode letters and digits, beginning with a letter, the dollar sign "$", or the underscore character "_".
So the following are valid Java variable names and will compile:
public static final int _1=0x7f020000;
public static final int one=0x7f020001;
While the following variable name would cause a compilation error:
public static final int 1=0x7f020000;
The long answer is that this is existing behavior in java that Android has built upon, retaining the same restrictions.
Additionally, it goes further: R.java is not specific to drawables. You will notice that you cannot have a 1.xml
layout or even a <string name="1">
in your strings.xml
file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With