0

NOT a life or death emergency, just a curiosity question. I have two layout files layout/some_xml_file layout-large/some_xml_file

when I call setContentView(R.layout.some_xml_file), the "R.layout.some_xml_file" is passed to setContentView as an int.

My question is.... when is R.layout.some_xml_file converted to the int. a - As the program starts ? so it's a constant as long as the program is running - or - b - Every time the setContentView() statement is called?

Joe Cullity
  • 518
  • 2
  • 8
  • 24

2 Answers2

1

Look into R.java file in your gen folder there you can see your layout converted to int

Nitin Misra
  • 4,472
  • 3
  • 34
  • 52
  • AHh so this gets more confusing. It's converted to an int during the generation, but BOTH the - layout/some_xml_file - and the - layout-large/some_xml_file - are referenced by the same int value, and the decision on which folder (layout or layout-large) to chose from is made during runtime? – Joe Cullity Mar 05 '14 at 02:31
  • yah it's android hierarchy that if you write same layout in `layout`, `layout-large`, `layout-land`, `layout-large-land` they will be generated by same IDs in `R.java` file, It's the functionality that android given to use different layouts in different screen sizes and position for more info you can read this http://developer.android.com/guide/practices/screens_support.html#ConfigurationExamples – Nitin Misra Mar 05 '14 at 02:36
  • Thanks Especially for the pointer to the web documentation - Joe – Joe Cullity Mar 05 '14 at 02:47
1

When you call setContentView(int), you're not actually passing it a layout in an integer form. You're actually telling it where to look inside the project's local resources to decode the View that you've specified.

As for when it's built: when using a program like Eclipse, the resource IDs in your R file are generated whenever you build the project. Outside of that environment, you can generate the file from your resources using aapt:

aapt package --non-constant-id -f -m -M <abs_path_to_AndroidManifest.xml> 
  -S <abs_path_to_res_dir> -I <abs_path_to_platforms_android.jar> 
  -J <abs_path_to_dir_that_should_contain_gen_R_java) 
  --generate-dependencies
Community
  • 1
  • 1
Cruceo
  • 6,763
  • 2
  • 31
  • 52