Where exactly does placement markup take place?
Hi these questions seem to be pretty essential, but I haven't found an answer to this.
When we do setContentView(R.layout.somelayout)
, then our XML layout file is bloated by the service LayoutInflater
. But I would like to know exactly where this is happening. I haven't found any code in the activity source code that bloats the XML file.
And also where exactly are identifiers for views generated in R?
Thanks in advance.
Less documentation and written for the base procedure and is called to call setContentView()
other methods as well.
Actually when you call setContentView(R.layout.<layout_file_name>);
. Android nutshell which will inflate the given mock file with its id and prepare the View for your current activity and place it on the top level.
What happen when you call setContentView(R.layout.<layout_name>) ?
The Android system receives this link and uses it to undo, measure and draw the tree. The root node of the hierarchy requests that its child nodes draw themselves - in turn, each node view group is responsible for accessing each of its own child views to draw themselves. Children can query for size and location in the parent, but the parent makes the final decision on how big each child is. Android parses the elements of your layout in order (from the top of the hierarchy tree) by instantiating views and adding them to their parents. Since they are drawn in order, if there are elements that overlap the positions, the last one to be drawn will lie on top of others previously facing that space.
Update:
Ok after i found,
When you call setContentView () of the base activity class, it calls the abstract method of the Window class setContentView () , which is an abstract method, and the only exception to this method is the 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, inflation viewing happens in this class.
Update: 2 About generating the R.java .. file
Aapt(Android Asset Packaging Tool)
- This tool compiles all XML layout files and a AndroidManifest.xml
. Besides the compiled version, this also generates a file R.java
containing all the links of the compiled resources.
Resources stored in the res subdirectory include objects such as icons, layouts, and strings. They are compiled with the aapt tool into a named file R.java
that is stored in a subdirectory gen/
.
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 Android HelloWorld app)
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.
source to share