Each layout file must contain exactly one root element, which must be a View or ViewGroup object.
After you‘ve declared your layout in XML, save the file with the
.xml extension, in your Android project‘s
res/layout/ directory, so it will properly compile.
When you compile your application, each XML layout file is compiled into a
View resource. You should load the layout resource from your application code, in your
Activity.onCreate()callback implementation. Do so by calling
, passing it the reference to your layout resource in the form of:
R.layout.layout_file_name. For example, if your XML layout is saved as
main_layout.xml, you would load it for your Activity like so:
The at-symbol (@) at the beginning of the string indicates that the XML parser should parse and expand the rest of the ID string and identify it as an ID resource.
The plus-symbol (+) means that this is a new resource name that must be created and added to our resources (in the
There are a number of other ID resources that are offered by the Android framework. When referencing an Android resource ID, you do not need the plus-symbol, but must add the
android package namespace, like so: