Prevent Android from generating new ID on resources - android

Does anyone know if it is possible to make Android not generate new ID's for resources that is added to raw, drawable and other resource-folders?
The problem is that if I for example add an image with the name 3.jpg, and later on adds another image called 2.jpg the ID for image 3.jpg gets changed.
I need to keep the ID's static as the ID's are defined in XML files and used in the app to get the resources based on the ID.
I may have done something wrong when I tested it so it may be that the ID does not get changed, but as far as I can remember ID got changed when adding new resources.
Thanks for any help.

As far as i know, R.java is autogenerated and all resource ID's are internally allocated by eclipse. Technically your code should not depend on the ID's generated. So even if ID's keep changing, it is absolutely fine. If you are depending on the generated ID's in your code, you will keep running into problems.

Related

Is this a good way of detecting the current active fragment?

I'm currently trying to determine which fragment is the current active one within the onResume() method.
I'm currently basing the detection off the destination ID. Would the ID be the same for everyone who downloads the app or is it a randomly generated number?
How I'm getting the ID:
Navigation.findNavController(getView()).getCurrentDestination().getId()
How I'm using it:
if (Navigation.findNavController(getView()).getCurrentDestination().getId() == 2131296382){
Log.w("Navigation", "The current fragment is the chat fragment");
}
This is very bad approach to operate just with numbers - integer ids in your case, called 'magic numbers'.
Would the ID be the same for everyone who downloads the app
Resource ids will be same on all devices, which have the same version of the app, different versions may have different resource ids, because they are code generated during the build.
If you want to operate with resources ids you need to get them from R.java class by reference. All resources ids are available in R.java class. In your case R.id.<your_fgagment_id> from your navigation.xml. More about resources ids here https://developer.android.com/guide/topics/resources/providing-resources

what R.java file actually does and how

I have been working on a simple android tutorial and while browsing through the project folders I found this R.java file in gen folder...
When I opened it seemed to me as a mess...
first R itself is a class.
it had multiple Inner classes defined within eg drawable,id,layout,etc.
and that inner classes had lots of variables declared as below which were assigned with hex values
public static final int addr=0x7f080003;
...
...
and much more
R is auto generated and acts as some pointer for other files
Questions for R.java
what it is basically for
how it works
why
values are in hex
what role did it performs while the actual application is running
"Acts as some pointer to other files" is actually absolutely correct, now the question is which files it points to how it is done.
What does it contain?
R file contains IDs for all the resources in the res folder of your project and also some additional IDs that you define on your own (in the layouts, for example). The IDs are needed for the Android resource management system to retrieve the files from the APK. Each ID is basically a number which corresponds to some resource in the resource management system.
The file itself is needed so you can access or reference the resource from code by giving the ID of the resource to the resource manager. Say, if you want to set the view in the activity, you call
setContentView(R.layout.main);
main in the R file contains the number which is understood by the Android resource management system as the layout file which is called main.
Why is it better than just plain file names?
It's harder to make a mistake with the generated fields. If you write the field name incorrectly, your program won't compile and you will know that there's an error immediately. If you write an incorrect string, however, the application won't fail until it is launched.
If you want to read more on this topic, you should check the Android documentation, especially the Accessing Resources part.
This holds your resource ids. So when you do something like
TextView tv = (TextView) findViewById(R.id.mytextview);
it looks up your id here for that View, layout, etc... This way the app has an easy way to look up your ids while you can use easy to remember names. Anytime you create a resource it automatically creates an id for it and stores it here. That's why you never want to try and edit this file yourself.
One way to think about how valuable R.java is, imagine a world without it. Its amazing how android brings the xml and java world together to help avoid coding the UI manually completely. With legacy java building UI using the java language was a pain. Invaluable.
With Android you can not only build your UI using only xml, but also see it while you build it. Invaluable.
Every element in the xml can be referenced in the java code WITHOUT writing a single line of code to parse the xml :). Just R.id.nameOfElement. Invaluable.
Rapid development is beautifully done in android. Imagine if iPhone would have 5000 screens to fit that one piece of code, they would crumble on their XCode. Google has done a wonderful job with just R.java. Invaluable.

How to prevent some values in R.java from changing when adding/updating resources

I need to store the link to the image, which user has chosen. The sources of the images are files, contact icons and resources' drawables (I need drawables since I provide different images for different resolutions/densities).
For the purpose of unification I store it in URI, and consequently this URI may be pointing to a file (file://), to content provider or to resource (android.resource://...). Here is how the URI pointing to drawable with id=2130837534 looks like:
android.resource://my.packagename/2130837534
It works fine unless the ids in R.java are regenerated. So, is there any way to prevent SDK from changing some of the R.java ids values?
No you can't, and shouldn't. If you can describe your problem clearly, there might be some other solution.
Save your resource names in a file, in this way.
icon_profile_1
icon_profile_2
And then when you need the resource ID, do this.
int resID = getResources().getIdentifier("icon_profile_1", "drawable","your_package_name");
Check here(java.lang.String, java.lang.String, java.lang.String)
As R.java is auto-generated it can't be restricted from it's operation, because it will agin be auto-generated when your project is cleaned or compiled.
This is impossible In android because R.java is Autogenrated and you cant do anything for that.

what does the "#+android:id/title" mean?

In normal, we should use #+id/ to define an id and use #id to reference an id. Today I found #+android:id/title in apps/settings/res/layout/preferenc_progress.xml.
How to understand it and how to use it?
It is used for resources that are shipped with the SDK.
You can take a look at them by browsing to
[PATH TO ANDROID SDK]/platforms/android-[VERSION]/data/res
By using the android in android.R.whatever you just specify the R file to look up. For more information you should read Accessing Platform Resources.
That belongs to the app preferences activity screen definition.
title and summary are standard Android fields of a TextView preference item.
I think it does the same thing. It's just a more formal way of saying it by specifying where the namespace is.
I've never met this way of giving id, but in theory this means adding new id title to android package. So you'll be able to use it in your code like android.R.id.title. But I'm not sure resource compiler will really create any id in android package. I think it can be used only with predefined ids. But I'll give you more precise answer later, when I'll be able to check it.
EDIT: I've checked it and found some differences. Firstly, if you define Android's id using #+android:id/some_id, which is already present in SDK, this id will not be defined in your R.java file. If it's not present in SDK, it will be defined in R.java, but with different kind of value. Secondly, if you'll try to convert id from its string representation to int value, Resources.getIdentifier() method will return 0 in case of #+android:id format.
This means it will create an id in your resource file.

Android: Loading multiple images

I have recently started developing a game in android, never used it before and was wondering if there is a simple way of getting a set of images loaded into the application. As my current implementation involves basically
Creating an int[] array,
Storing the values of each drawable into this array, (now this has to be hand coded, so if I add any more images it has to be added programmitically)
Then itterating through each item in the array and calling BitmapFactory to get the resource.
(Unfortunately I don't have the code with me as it is at home and I am at work, but that is the jist)
So 2 questions, is there a way of getting the drawables without having to put in each item manually to the int[] - ie looking for perhaps a file name prefix and then only loading the resource with the prefix?
Which leads me to my second question because I more than just these images in my drawable resource directory, is there a way to add extra organisation (folders) to manage the files better. As currently I have loads of images within the drawable file and how would I reference these sub folders/images.
You cannot have sub folders within the resources structure. Android depends on the folder layout to determine which resource to use in what condition (localization, different screen resolutions, etc).
I'm not sure why exactly you are trying to load up a whole bunch of images, but there are a couple of (slower) methods that allow you to look up a resource by string name. If you used a naming convention for your images you could look them up that way via [Resources.getIdentifier()][1]. However, in a game performance likely matters, so you are probably better off with a more manual approach using the int IDs directly since it is much more efficient.
[1]: http://developer.android.com/reference/android/content/res/Resources.html#getIdentifier(java.lang.String, java.lang.String, java.lang.String)
I am uploading a load of images as they will be shown to user for different items. Its not a system where responsiveness is critical so its okay in terms of what I want. Though...
public int getIdentifier (String name, String defType, String defPackage)
Since: API Level 1
Return a resource identifier for the given resource name. A fully qualified resource name is of the form "package:type/entry". The first two components (package and type) are optional if defType and defPackage, respectively, are specified here.
Note: use of this function is discouraged. It is much more efficient to retrieve resources by identifier than by name.
They suggest using the resource id but if I want to add a file later on then I have to re-compile the app to include the extra file, this is where it bugs me, as the pic gets associated to an item that I have in a string array. So I can add items to the array but not the images without a change of the code.
Surely there is a function to fix this?

Categories

Resources