3

Hi these questions seem to be pretty much basic, but I haven't found any answer regarding this.

When we setContentView(R.layout.somelayout) then our layout XML file is inflatted by LayoutInflater service. But I would like to know where exactly it happens. I haven't found any code in the activity source code that inflate the xml file.

And also where exactly the are ids being generated for the views in the R?

Thanks in advance.

Tomik
  • 23,857
  • 8
  • 121
  • 100
  • 1
    You can find little info on http://stackoverflow.com/questions/7838921/android-listview-addheaderview-nullpointerexception-for-predefined-views/7839013#7839013 and http://stackoverflow.com/questions/13878053/android-xml-vs-java-layouts-performance/13878145#13878145 these two questions. – user370305 Feb 18 '13 at 09:03
  • the ids are generated at compile time by aapt or one of those. most of the inflate happens in LayoutInflater.inflate and rInflate – njzk2 Feb 18 '13 at 09:04
  • @njzk2 I mean where the xml file(layout) for the activity invoke inflatter when we call setContentView() method –  Feb 18 '13 at 09:12

1 Answers1

5

Less Documentation and written for basic procedure and invoked call for setContentView() and other methods.

Actually, when you call setContentView(R.layout.<layout_file_name>);. Android nutshell come into picture, Which will inflate the given layout file from its id and prepare View for your current Activity and put into top-level.

What happen when you call setContentView(R.layout.<layout_name>) ?

The Android system receives this reference and uses it to invalidate, measure, and draw the tree. The root node of the hierarchy requests that its child nodes draw themselves — in turn, each view group node is responsible for calling upon each of its own child views to draw themselves. The children may request a size and location within the parent, but the parent object has the final decision on where how big each child can be. Android parses the elements of your layout in-order (from the top of the hierarchy tree), instantiating the Views and adding them to their parent(s). Because these are drawn in-order, if there are elements that overlap positions, the last one to be drawn will lie on top of others previously drawn to that space.

Update:

Ok, After Looking I found,

When you call setContentView() of Activity base class, it calls Abstract method of Window Class's setContentView() which is Abstract method, and the only one overriding of this method is in PhoneWindow Class. Which looks like,

 @Override
    public void setContentView(int layoutResID) {
        if (mContentParent == null) {
            installDecor();
        } else {
            mContentParent.removeAllViews();
        }
        mLayoutInflater.inflate(layoutResID, mContentParent);
        final Callback cb = getCallback();
        if (cb != null) {
            cb.onContentChanged();
        }
    }

So View inflation is take place in this class.

Update: 2 About R.java file generating..

Aapt(Android Asset Packaging Tool) – This tool compiles all XML layout files and AndroidManifest.xml file. Apart from compiled version this also generates an R.java file containing all the references of the compiled resources.

The resources, stored in the res subdirectory, include such things as icons, layouts and strings. These are compiled using the aapt tool into a file named R.java, stored in the gen/ subdirectory.

From aapt help

aapt p[ackage] [-d][-f][-m][-u][-v][-x][-z][-M AndroidManifest.xml] \
        [-0 extension [-0 extension ...]] [-g tolerance] [-j jarfile] \
        [--min-sdk-version VAL] [--target-sdk-version VAL] \
        [--max-sdk-version VAL] [--app-version VAL] \
        [--app-version-name TEXT] [--custom-package VAL] \
        [-I base-package [-I base-package ...]] \
        [-A asset-source-dir]  [-G class-list-file] [-P public-definitions-file] \
        [-S resource-sources [-S resource-sources ...]]         [-F apk-file] [-J R-file-dir] \
        [raw-files-dir [raw-files-dir] ...]

   Package the android resources.  It will read assets and resources that are
   supplied with the -M -A -S or raw-files-dir arguments.  The -J -P -F and -R
   options control which files are output.

Command for aapt : (For HelloWorld Android Application)

aapt \package -v -f -m -M AndroidManifest.xml -S .\bin\res -S .\res \
   -I C:\local\android-sdk-windows\platforms\android-15\android.jar \
   -J .\gen --generate-dependencies

And the result Looks like,

Configurations:
 (default)
 hdpi
 ldpi
 mdpi

Files:
  drawable\ic_launcher.png
    Src: (hdpi) .\res\drawable-hdpi\ic_launcher.png
    Src: (ldpi) .\res\drawable-ldpi\ic_launcher.png
    Src: (mdpi) .\res\drawable-mdpi\ic_launcher.png
  layout\main.xml
    Src: () .\res\layout\main.xml
  values\strings.xml
    Src: () .\res\values\strings.xml
  AndroidManifest.xml
    Src: () AndroidManifest.xml

Resource Dirs:
  Type drawable
    drawable\ic_launcher.png
      Src: (hdpi) .\res\drawable-hdpi\ic_launcher.png
      Src: (ldpi) .\res\drawable-ldpi\ic_launcher.png
      Src: (mdpi) .\res\drawable-mdpi\ic_launcher.png
  Type layout
    layout\main.xml
      Src: () .\res\layout\main.xml
  Type values
    values\strings.xml
      Src: () .\res\values\strings.xml
Including resources from package: \
C:\local\android-sdk-windows\platforms\android-15\android.jar
applyFileOverlay for drawable
trying overlaySet Key=ic_launcher.png
baseFile 0 has flavor ,,,,,,,,,,,hdpi,,,,,,,
baseFile 1 has flavor ,,,,,,,,,,,ldpi,,,,,,,
baseFile 2 has flavor ,,,,,,,,,,,mdpi,,,,,,,
overlayFile 0 has flavor ,,,,,,,,,,,hdpi,,,,,,,
overlayFile 1 has flavor ,,,,,,,,,,,ldpi,,,,,,,
overlayFile 2 has flavor ,,,,,,,,,,,mdpi,,,,,,,
found a match (0) for overlay file ic_launcher.png, \
for flavor ,,,,,,,,,,,hdpi,,,,,,,
found a match (1) for overlay file ic_launcher.png, \
for flavor ,,,,,,,,,,,ldpi,,,,,,,
found a match (2) for overlay file ic_launcher.png, \
for flavor ,,,,,,,,,,,mdpi,,,,,,,
applyFileOverlay for layout
applyFileOverlay for anim
applyFileOverlay for animator
applyFileOverlay for interpolator
applyFileOverlay for xml
applyFileOverlay for raw
applyFileOverlay for color
applyFileOverlay for menu
applyFileOverlay for mipmap
    (new resource id ic_launcher from \
    .\bin\res\drawable-hdpi\ic_launcher.png)
    (new resource id ic_launcher from \
    .\bin\res\drawable-ldpi\ic_launcher.png)
    (new resource id ic_launcher from \
    .\bin\res\drawable-mdpi\ic_launcher.png)
    (new resource id main from .\res\layout\main.xml)
  Writing symbols for class R.
user370305
  • 108,599
  • 23
  • 164
  • 151
  • 2
    I think the OP is wondering where (in source code) layout inflator kicks-in. can you point him to the android source code where inflation exactly happens? – waqaslam Feb 18 '13 at 09:39
  • 1
    @user370305 that's it. +1 on your further efforts :) – waqaslam Feb 18 '13 at 10:05