Pr.Pg Next Pg

Analysis of main.xml tutorials

What is main.xml?

  • This XML file describes the UI of the application. An activity can reference to this UI. This UI, however, does not bind itself to any activity at all. A UI is built using layouts and widgets. The main.xml that Eclipse builds by default consists of a single LinearLayout, which means that all the elements are arranged in a single column. It then defines a textView, which is used to display a static non-editable text. In this case, the "hello" string defined in the strings.xml file (the '@' sign refers to a file defined in the res folder). Each view element further has attributes, such as layout_height and layout_width, etc.

 

  • When you click on the main.xml file from the package explorer, you will see the following screen. You can go to the code from the graphical interface from the main.xml tab at the bottom.

 

main.xml

 

<?xml version="1.0" encoding="utf-8"?>

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

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/hello"/>

</LinearLayout>

 

 

Analysis of the Code

  • Line 1 shows the version of the xml and shows the character encoding of the document.

  • Line 1 declares and defines a Linear Layout

  • Line 3 indicates the orientation of the layout. The orientation can be vertical or horizontal

  • Line 4 and 5 set the Layout width and height. If the attribute is set to “fill_parent” ( same as match_parent in API 8 or higher) then it will occupy the entire available space and if the attribute is set to wrap content then it wil occupy on the space it requires to wrap the contents.

  • The next line inserts a text view in the layout. The other attributes added are similar to those of the layout. However the “ android:text ” will add a text in the text view. ‘@’ is used for a reference. So “@string/hello” meanns that it is referencing a string in the strings.xml file with the reference “ hello ”. It adds the value of the string to the attribute which we have set for this tutorial as “ Hello Android!!! ”

  • Finally the Linear Layout tag is closed.

  • The open and closing tags of xml are analogous to HTML tags.

 

Analysis of main.xml

  • The Above code would result in an Layout which could be seen graphically by clicking on the graphical tab at the bottom of the window.

 

 

Pr.Pg border                                              Next Pg