Professional Documents
Culture Documents
Android Layout: by Zana Azeez Kakarash Email: 2021 - 2022
Android Layout: by Zana Azeez Kakarash Email: 2021 - 2022
Android layout
by
Zana Azeez Kakarash
Email: zana.azeez@kti.edu.krd
2021 – 2022
Four
Layout
1. Declare UI elements in XML : Android provides a straightforward XML vocabulary that corresponds to
the View classes and subclasses, such as those for view and layouts.
2. Instantiate layout elements at runtime : Your application can create View and ViewGroup objects
• The most common way to define your layout and express the view is with an XML layout file.
• View objects are leaves in the tree, ViewGroup objects are branches in the tree.
6
Linear Layout
• In the Linear layout all the elements are displayed in linear.
• all the elements of a linear layout are displayed according to its orientation.
• Android Linear Layout is a view group that aligns all children in either Vertically or Horizontally
9
Linear Layout Attributes
<LinearLayout
android:layout_width=“match_parent"
android:layout_height="wrap_content"
android:orientation="vertical“ > <!-- Vertical Orientation set -->
</LinearLayout>
<LinearLayout
android:layout_width=“match_parent"
android:layout_height="wrap_content"
android:orientation=“horizontal“ > <!-- Horizontal Orientation set -->
</LinearLayout>
• wrap_content: which means that the view wants to be just big enough to enclose its content.
• Padding is the space inside the border, between the border and the actual view’s content
• Margins are the spaces outside the border, between the border and the other elements next to this
view.
• layout_weight specifies how much of the extra space in the layout to be allocated to the View.
Project name
Nested layout
Nested layout
20
Relative Layout
• Android Relative Layout enables you to specify how child views are positioned relative to each other.
• It means that child controls, such as ImageView, TextView ,and Button controls, can be placed above,
below, to the left or right, of one another
22
1- Define a Relative Layout
• Your XML resource file should now look something like this:
2) In this case, we need seven TextView controls (one for each color).
<TextView TextView
android:text="RED" android:text="ORANGE"
android:id="@+id/TextView01" android:layout_height="wrap_content"
android:layout_height="wrap_content" android:background="#ffa500"
android:background="#f00" android:gravity="center"
android:gravity="center" android:textColor="#000"
android:textColor="#000" android:id="@+id/TextView02"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:padding="25dp"></TextView> android:layout_centerHorizontal="true"
android:padding="25dp"></TextView>
<TextView <TextView
android:text="YELLOW" android:text="GREEN"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="#ffff00" android:background="#0f0"
android:gravity="center" android:gravity="center"
android:textColor="#000" android:textColor="#000"
android:id="@+id/TextView03" android:id="@+id/TextView04"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_alignParentRight="true" android:layout_toLeftOf="@+id/TextView05"
android:padding="25dp"></TextView> android:padding="25dp"
android:layout_centerVertical="true"></TextView>
<TextView <TextView
android:text="BLUE" android:text="INDIGO"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="#00f" android:gravity="center"
android:gravity="center" android:textColor="#fff"
android:textColor="#fff" android:id="@+id/TextView06"
android:id="@+id/TextView05" android:layout_width="wrap_content"
android:layout_width="wrap_content" android:layout_toRightOf="@+id/TextView05"
android:layout_centerInParent="true" android:background="#4b0082"
android:layout_margin="10dp" android:padding="25dp"
android:padding="25dp"></TextView> android:layout_centerVertical="true"></TextView>
28
1) Center relative to Parent View
1. android:layout_centerHorizontal="true"
This places the view horizontally in the center of the parent.
2. android:layout_centerVertical="true“
This places the view vertically in the center of the parent.
3. android:layout_centerInParent="true“
This attribute will place the view in the center of the parent.
1. android:layout_alignParentTop="true“
If you write this attribute for a View, then that view will stick to the top of its parent.
2. android:layout_alignParentBottom="true“
If you write this attribute for a View, then that view will stick to the bottom of its parent.
3. android:layout_alignParentLeft="true“
If you write this attribute for a View, then that view will stick to the left of its parent.
4. android:layout_alignParentRight="true“
If you write this attribute for a View, then that view will stick to the right of its parent.
37
Example
41