I sometimes see this declarations in Android source code:
mContext.getString(2131361954);
Notification n = new Notification(2130837696, "123", System.currentTimeMillis());
// Example code - does not match together
I think the numbers are some resources from the project, right? Why sometimes people work with this numbers instead of using the R class? Is it faster or something else?
And how can I check which resource is assigned to that numbers? Is it possible to get number which is used if I only have the file or is this number random? Maybe with the file name or the MD5 hash of the file or something else?
There is no performance difference, as all members of the R class are static and final, and are directly swapped in during compile time. This is equivalent to any code that uses R.x.y, so the performance is the same.
I would strongly recommend against using the numbers directly in your project as they may change during the addition, removal and modification of resources.
You can check the resource to which that number corresponds by converting it to hex, opening up the R.java file and searching for that hex number and seeing what it is assigned to.
You can also use getResources().getResourceEntryName(int resid); and pass it the ID at runtime to retrieve the file name.
Related
I uploaded local photos in drawable files. The photos are approximately 3 mb in size. Its showing this error. However The problem panel shows analizing for 15 minutes till now and it is still showing it. What could be the possible reason for such errors.
Resource name must start with a small case letter or an underscore('_').
For more rules regarding resource naming convention, you can refer to the below-mentioned medium article.
https://medium.com/#AkhilDad/a-designers-guide-for-naming-android-assets-f790359d11e5
There are a few conventions used in resources:
For resources that exist as separate files, they must be
lower_case_underscore_separated. The appt tool makes sure that your
files are only lower-case, because using mixed case can cause issues
on case-insensitive filesystems.
For resources declared only in values/... (attributes, strings, etc)
the convention is generally mixedCase.
There is a convention used sometimes to tag names with a
"classification" to have simple namespaces. This is for example
where you see things like layout_width and layout_alignLeft. In a
layout file the attributes for both the View and the parent layout
management are mixed together, even though they are different
owners. The "layout_*" convention ensures that there are no
conflicts between these names and it is easy to understand which
entity the name impacts.
For more information here is the complete discussion.
Are there conventions on how to name resources?
Resource name must start with letter. You can't start image name with digits.
The reason you can't have a resource with a numeric name is because variable names cannot start with numbers.
The error is actually because the resource name cannot start with a number. Resource name should start with a letter or underscore(_).
Android will allocate ids for each pics in the res/drawable directory. Where are they?
I want to dynamically choose one pic form the pool and show it. How can I do that?
They are stored in the res/drawable folder.
If the file name is demo.png, then they can be accessed by R.drawable.demo
If you want to access a random drawable, store all the resource identifiers in a Integer ArrayList, and programatically generate a random function using Random(), and get that particular item from the arraylist. Then you'll have a random drawable every time.
Autogenerated ids are in the gen file, but not advisable to use them. It would be better to use the filenames directly through some predefined array of R.drawable.filename and randomly pick them.
There IDs are stored in the R.java file, but you cannot edit it, as your changes are over written each time.
You can also access resources by name, which may be a viable approach to solving your problem if you know the names of the resources or can derive them according to some pre-defined naming scheme. (for example images are named in the sequence image1, image2 and so on.
You have to map the name to the identifier using the getIdentifier() method of the Resources class.
String name = "resource" + rng.nextInt(count);
int resource = getResources().getIdentifier(name, "drawable", "com.package");
The documentation for this method says:
Note: use of this function is
discouraged. It is much more efficient
to retrieve resources by identifier
than by name.
This is true but need not be a problem if you are doing it in code that isn't performance sensitive.
Alternatively, if you don't mind listing the resources in XML, you could create a typed array that you can then randomly select from.
I wonder if there is a way to read out the locale values of all existing values directories.
Let's say I've got the following directories under my res-Directory
[...]
values
values-de
values-nl
[...]
Now I need a method to get back the information that there is a locale of de and nl existing for the directory values.
Is there any way, if yes how?
Any help will be appreciated.
Regards,
Christian
Well, ideally, your app neither knows nor cares what resource sets you have. That is the whole point behind resource sets, after all -- to insulate your app from changes in resources.
That being said, one possibility is to write a script that is part of your build process that generates a file with your requested data (e.g., XML file containing the roster of resource sets) that you then read in at runtime.
Or, arrange to have a "magic value" in each set. For example, in res/values-de/strings.xml, you could have a lang_de string, and in res/values-nl/strings.xml, you could have a lang_nl string. Then, you can use reflection to iterate over your string resources and find those matching the lang_ pattern. This may be significantly slower than the first option, particularly if you have lots of string resources.
I know of no way to interrogate the system to find out what resource sets are defined.
I want to load bitmaps into ImageViews in Android, but I don't want to use the R.drawable syntax because I have a lot of images with a standard naming convention so it's much easier to get the image with some logic. For example, all my images are named:
img1.png
img2.png
img3.png
So if a user selects a value, let's say x, I show "img" + x + ".png" in the ImageView. Looks like Bitmap.decodeFile is what I need, but I need to know the syntax of how to get to my drawables folder since that's where the images are. Or if there's perhaps a better way.
I realize I could do this with a switch statement instead of concatenating the image name but that would be a lot of lines since I have so many images.
One alternative is to use reflection to load images from R. You can looking static variables by string name. Keep in mind that reflection can be slow though. If you are going to be loading the same image multiple times, you might want to keep a cache of name -> R id.
Edit:
You can also access them by URI. See referring to android resources using uris.
The R.drawable value is a number by itself. You just need to use the first image id and add a fixed value to find the right image. For example:
R.drawable.image1 = 1234;
R.drawable.image2 = 1235;
etc.
So to get image 2 I would to R.drawable.image1 + 1.
Every time the R.java file is regenerated the numbers may change but the sequence will be the same. If you don't want to depend in this then you will have to look at the "assets" folder.
Looks like I've found a solution. The real problem is that I generate the resource names at runtime, but I can get the resource id like this:
getResources().getIdentifier("string1" + string2, "id", "com.company.package")
I can then pass that value to setImageResource or any other method that accepts a resource id.
Actually I have 52 images which r basically cards.
images names are from 1 to 52.
when I put all the 52 images in my drawable folder then it is showing an error in R.java file which is:
Syntax error on token "image name(any
between between 1 to 52)", invalid
VariableDeclaratorId
what is the problem?
thanks for replying.
i think u didnt get my problem.
i have given name to my cards from 1 to 52 because i need to randomly select one card from it.
Resource names have to be proper Java identifiers. Call them card1 through card52 instead of just their numbers (if I understand you correctly).
EDITED TO ADD: To map an integer to the correct image, your code should manage the mapping itself. One (not terribly elegant) way is to explicitly create a Bitmap[] cardImages = new Bitmap[52]; array and assigning each resource into the array, as in e.g.
Resources r = context.getResources();
cardImages[0] = loadBitmap(r.getDrawable(R.drawable.card1));
// ...
cardImages[12] = loadBitmap(r.getDrawable(R.drawable.card13));
// ...
cardImages[51] = loadBitmap(r.getDrawable(R.drawable.card52));
The problem is that Android doesn't allow to use spaces in a file identifier
Pontus Gagge is right. Android will take the name of the everything in the drawable folder and will try to generate an R file that contains an int for every image that you are using in your app. The ints are named after the file names of your drawables. You can then later use this ints as ids to load the images from your app.
The problem is that Java does not allow a vairable name to start with a number. Your images start with a number therefore your variables in the R file will start with a number. You have to choose another name for your images.