Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are resource identifier values determined in Android?

I was wondering how the Android resource identification system works.

So, I have (for example) a XML layout file with the name "example". In my java code, this is identified by calling R.layout.example which returns an integer.

What I was wondering was how this integer is determined, and then how an integer value is used to identify and find a corresponding resource.

Thanks!

like image 418
public static void Avatar asked Oct 22 '22 04:10

public static void


1 Answers

The integers are stored in a generated file called R.java in the project's gen folder. A sample from a recent work-in-progress of mine is:

public final class R {
public static final class attr {
}
public static final class drawable {
    public static final int cot_logo=0x7f020000;
    public static final int cot_logo_small=0x7f020001;
    public static final int ic_launcher=0x7f020002;
    public static final int icon=0x7f020003;
}
public static final class id {
    public static final int password=0x7f060001;
    public static final int username=0x7f060000;
}
public static final class layout {
    public static final int login_dlg=0x7f030000;
    public static final int main=0x7f030001;
}
public static final class string {
    public static final int app_name=0x7f050000;
}
public static final class xml {
    public static final int config=0x7f040000;
}

}

As you can see, there is a pattern to how they are generated. Each category of resource is given its own area to work with.

Another thing to note is that they are generated in the order they are encountered in the XML/folders/etc. So, for example, if you move some views around in the XML, you'll need to clean your project to regenerate the full R.java file. Otherwise, the integer mappings will be incorrect and you'll end up with(most likely) a ClassCastException, or unexpected behaviour at the very least.

like image 112
Geobits Avatar answered Oct 23 '22 20:10

Geobits