0

How is the compile-time generated R.id.view_id for view_id attribute that is defined in layout.xml assigned during that particular view's runtime inflation ?

Obviously attribute names are available during runtime layout.xml parsing, my question is how is this attribute name view_id matched at runtime with integer R.id.view_id in R.java ? (or R.class for that matter)

NathanTempelman
  • 1,301
  • 9
  • 34
kiruwka
  • 9,250
  • 4
  • 30
  • 41
  • http://developer.android.com/tools/building/index.html and this http://developer.android.com/guide/topics/ui/declaring-layout.html – Raghunandan Nov 15 '13 at 19:28
  • What specific programming problem are you trying to solve? – 323go Nov 15 '13 at 19:37
  • @323go just curios about the mechanism. I could't figure out from quick glance at sources how the string `view_id` is matched with integer declared as `R.id.view_id` during parsing. Is it done via reflection ? – kiruwka Nov 15 '13 at 19:39
  • 1
    Ah gotcha, I was just curious if there's a problem you ran into. It was my assumption (based on no actual knowledge of the LayoutInflater source) that the ids are generated and assigned at *compile time* and are included as integers in the binary xml. The `R.id.xxxx` is just for the convenience of the programmer. – 323go Nov 15 '13 at 19:42
  • it's not at run time. it's done at compile time – njzk2 Nov 15 '13 at 19:46
  • @323 Well, the binary xml is generated for "simple" resources, such as string, color, etc., but since I can see `layout.xml` is preserved in `.apk` I assume it IS actualy parsed as plain text during view inflation. In that case your expalantion does not hold. Or am I missing something ? – kiruwka Nov 15 '13 at 19:46
  • @njzk2 could you please elaborate ? View inflation and assigning the id is apparently done runtime. – kiruwka Nov 15 '13 at 19:47
  • did you try to actually open `layout.xml` from your apk? you'll be surprised – njzk2 Nov 15 '13 at 19:47
  • @njzk2 well, you are right! Totally missed that. So it is binary... – kiruwka Nov 15 '13 at 19:48
  • 1
    as 323go mentioned, resources are parsed and processed during compilation. that result in the .arsc file that contains information such as the matching between the ids and the resources. during compilation the ids are assigned, then pushed into generated R.java file. – njzk2 Nov 15 '13 at 19:49
  • It would make sense, as this would immediately come to mind as the most efficient way to inflate layouts... and layout inflation happens very frequently. – 323go Nov 15 '13 at 19:50
  • @njzk2 I can see what 323go means. But still a bit confused by the fact that layout is not in .arsc file. So your comment is not totally correct then ? The mapping is preserved in layout.xml binary stored in .apk ? – kiruwka Nov 15 '13 at 19:51
  • @kiruwka see View(Context, AttributeSet, int) constructor – pskink Nov 15 '13 at 19:55
  • the arsc contains indices and informations for retrieving resources based on their identifiers. – njzk2 Nov 15 '13 at 19:56
  • 1
    would anyone care to summarize and post an answer which I would be happily accepting, in case it would be interesting for someone else in future ? – kiruwka Nov 15 '13 at 20:01

0 Answers0