Android – activity_main.xml

Activity_main.xml

Activity_main.xml – way MainActivity.java the name of the Controller File Automatically Create the exact same way activity_main.xml name View File also Automatically Create occur and the File by MainActivity.java Controller SetContentView () through Method View is displayed on the Android device as the First Screen Layout.

activity_main.xml

This file is stored on the ” / res / layout / ” path of our Android App and all the activities we create in our Android App are related to the Controller Java File, ” / java / com ” of our Android App Architecture . domain.packagename / “Create on Path, while Associated View File with each Activity is Create on” / res / layout / “Path.

Also, in the Naming of the Create Activity File and its associated View File, there is also a Convention Follow, under which the name of the Activity File is also the same name as the Associated View File but each Capital of the name of the Activity File. The name of the View File is replaced with Underscore Replace just before the letter, along with the name of each View File by default beginning with the word ” activity_ main.xml”.

For example if we create another Activity File named SecondActivity , then the Associated View File to be created by default will be named activity_second.xml while the Controller Java File will be named SecondActivity.java . Similarly if we create another Activity File named SecondScreenActivity , then Associated View File to be created by default will be named activity_second_screen.xml while Controller Java File will be named SecondScreenActivity.java

The Layout Defining XML Codes that are created by default in the activity_main.xml file are as follows:
The following is the first Declaration in this file in the form of a statement, which gives the Android system the instruction that the Current File is an XML File, with UTF-8 Font Supported XML Codes based on XML Version 1.0 Standards went:

An Android App supports a number of Layouts, which we will describe in detail in subsequent Chapters, but through the <RelativeLayout… /> Element in the Current View File, the Android System has been given the instruction that this View File RelativeLayout is supported. That is, all the Visual Elements of this View File are Arranged from each other in Relative Mode.
Because any Layout Mode used in a View File related to any Android Activity, that Layout Mode is basically the Root Node of that Layout XML File and all other Elements related to the View are nested in the same Root Node. Live. That is why <TextView… /> Element is nested under <RelativeLayout… /> Element in the above code.

Each element of this Layout File is associated with some visual control such as Button, TextBox, Label etc. and various Aspects of these Visual Controls are specified through the Attributes of the Element. Therefore, to determine how the MainActivity of our Android App will look, the task of specifying the <RelativeLayout… /> element in the activity_main.xml file is done by different Attributes and which Element and which Attribute is working in any way. All the Declarations and Definitions related to how the Specific Visual Element is supposed to appear are identified in the XML Schema through the XML Namespace and represented by the xmlns Attribute as follows in the Current XML File is:

xmlns:android="http://schemas.android.com/apk/res/android"

Namespaces are similar to Package of Java or Header Files of C / C ++, which contain Declaration / Definitions of various Functions used in Current Program File. So through the above code, we give the Android system the instruction that whatever names are being used in the current XML file as Element and Attributes, their definitions under XML Schema http://schemas.android.com / apk / res / android URI to be accessed.

That is, how a TextView Element should look like on the screen of the Android Device, to uniquely represent the TextView Element of the Schema containing the XML Namespace that is defined in the XML Namespace, we use an xmlns Attribute Create Alias, so that we do not have to write the entire URI to refer to that element every time.

In this code, we have given the XML Parser of the Android System through xmlns: android Attribute that the specification of all the XML Attribute Codes written with the ” android: ” prefix is http://schemas.android. com / apk / res / android to be accessed from URI. As a result, the XML Attribute android : id specified in the next line is actually referring to the

http://schemas.android.com/apk/res/ android : id itself, and if we have <RelativeLayout… /> element in the xmlns: If android does not specify Attribute, then we have to specify the complete XML URI for each Attribute in the entire XML file. That is, the above code would be as follows:

However these codes are not practically correct and if you replace the Codes of your Android App’s Layout File with these codes, you will get Rendering Error only, because there are many other functionalities related to Rendering, which need XML View of Android System The parser is born. So it is always necessary to specify xmlns: android Attribute as the first Attribute in the View File of Android App Compulsory so that other Attributes can be specified with the ” android: ” Prefix, which the Android System’s XML Parser understands is.

What actually happens is that the XML Parser of the Android system installed on the Android Device already knows how to visually render the <RelativeLayout… /> or <Button… /> Element, so all in the Layout File It is necessary to specify XML Codes Exactly through the same Element and Attribute Names, just as they are defined in the Schema File of the XML Namespace that is in the XML Parser of the Android System.

This is the same as clrscr () to clear the screen in a C / C ++ program ; The function name itself has to be used and the complete definition of how this function will clear the Output Screen is already specified in the conio.h Header File. As a result, when the compiler compiles our program, we get the Instructions related to clearing the screen from the conio.h Header File, but the Compiler can only do this when we have Include this Header File in our Program File. Be it and call the clrscr () function name somewhere.

Many Attributes have been used to Specify Screen View in this XML File, but as we can Visually Design our View using Drag and Drop technology in Android Studio , so understanding all these Attributes in detail And it is not necessary to remember. However, wherever necessary, we will discuss in detail about Special Attributes.