Chapter 3
Chapter 3
Chapter 3
1 AndroidManifest.xml
This is the Android application definition file. It contains information about the
Android application such as minimum Android version, permission to access Android
device capabilities such as internet access permission, ability to use phone permission,
etc.
2 Java
The Java folder contains the Java source code files. These files are used as a controller
for controlled UI (Layout file). It gets the data from the Layout file and after
processing that data output will be shown in the UI layout. It works on the backend of
an Android application.
3 res/drawable
Here we store the various density-independent graphic assets used in our application.
4 Layout:
A layout defines the visual structure for a user interface, such as the UI for an
Android application. This folder stores Layout files that are written in XML language.
5 mipmap
Mipmap folder contains the Image Asset file that can be used in Android Studio
application. we can generate icon types like Launcher icons, Action bar and tab icons,
and Notification icons.
6 colors.xml
colors.xml file contains color resources of the Android application. Different color
values are identified by a unique name that can be used in the Android application
program.
7 strings.xml
The strings.xml file contains string resources of the Android application. The different
string value is identified by a unique name that can be used in the Android application
program. This file also stores string array by using XML language.
8 styles.xml
The styles.xml file contains resources of the theme style in the Android application.
This file is written in XML language.
9 build.gradle(Module: app)
This defines the module-specific build configurations. Here you can add dependencies
what you need in your Android application.
Layouts
1. Linear Layout
Linear Layout is a ViewGroup that is responsible for holding views in it. It is a layout
that arranges its children i.e the various views and layouts linearly in a single
column(vertically) or a single row(horizontally).
Whether all the children will be arranged horizontally or vertically depends upon the
value of attribute android:orientation. By default the orientation is horizontal.
LinearLayout Attributes
android:id
This is the ID which uniquely identifies the layout.
android:baselineAligned
This must be a boolean value, either "true" or "false" and prevents the layout from
aligning its children's baselines.
android:baselineAlignedChildIndex
When a linear layout is part of another layout that is baseline aligned, it can specify
which of its children to baseline align.
android:gravity
This specifies how an object should position its content, on both the X and Y axes.
Possible values are top, bottom, left, right, center, center_vertical, center_horizontal
etc.
android:orientation
This specifies the direction of arrangement and you will use "horizontal" for a row,
"vertical" for a column. The default is horizontal.
<Button android:id="@+id/btnStartService"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:text="start_service"/>
<Button android:id="@+id/btnStopService"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:text="stop_service"/>
</LinearLayout>
2. Absolute Layout
An Absolute Layout lets you specify exact locations (x/y coordinates) of its children.
Absolute layouts are less flexible and harder to maintain than other types of layouts
without absolute positioning.
Absolute layout are harder to maintain for different mobile screen sizes than other
types of layouts because we set the exact location of a child view or called
component. The positioning is based on x(top) and y(left) coordinates and that
positioning is not as useful in world of various screen resolutions(sizes) and aspect
ratios.
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="OK"
android:layout_x="50px"
android:layout_y="361px" />
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Cancel"
android:layout_x="225px"
android:layout_y="361px" />
</AbsoluteLayout>
3. Frame Layout
FrameLayout is designed to block out an area on the screen to display a single item.
Generally, FrameLayout should be used to hold a single child view, because it can be
difficult to organize child views in a way that's scalable to different screen sizes
without the children overlapping each other.
We can add multiple children to a FrameLayout and control their position within the
FrameLayout by assigning gravity to each child, using
the android:layout_gravity attribute.
1. android:id
This is the unique id which identifies the layout .
2. android:foreground
Foreground defines the drawable to draw over the content and this may be a color
value. Possible color values can be in the form of “#rgb”, “#argb”, “#rrggbb”, or
“#aarrggbb”.
3. android:foregroundGravity
This defines the gravity to apply to the foreground drawable. Default value of gravity
is fill. We can set values in the form of “top”, ”center_vertical” , ”fill_vertical”,
”center_horizontal”, ”fill_horizontal”, ”center”, ”fill”, ”clip_vertical”,
”clip_horizontal”, ”bottom”, ”left” or ”right”
4. android:visibility
This determine whether to make the view visible, invisible or gone.
5. android:measureAllChildren
This determines whether to measure all children including gone state visibility or just
those which are in the visible or invisible state of measuring visibility. The default
value of measure all children is false. We can set values in the form of Boolean i.e.
“true” OR “false”.
<ImageView
android:src="@drawable/ic_launcher"
android:scaleType="fitCenter"
android:layout_height="250px"
android:layout_width="250px"/>
<TextView
android:text="Frame Demo"
android:textSize="30px"
android:textStyle="bold"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"/>
</FrameLayout>
android:background="@color/design_default_color_secondary"
tools:context=".MainActivity">
<TextView android:text="LeftTop"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="RightTop"
android:layout_gravity="top|right" />
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="CentreTop"
android:layout_gravity="top|center_horizontal" />
<TextView android:text="Left"
android:layout_gravity="left|center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Right"
android:layout_gravity="right|center_vertical" />
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Centre"
android:layout_gravity="center" />
<TextView android:text="LeftBottom"
android:layout_gravity="left|bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="RightBottom"
android:layout_gravity="right|bottom" />
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="CenterBottom"
android:layout_gravity="center|bottom" />
</FrameLayout>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/table"
android:foregroundGravity="center"
android:foreground="#000"
android:background="@color/design_default_color_secondary"
tools:context=".MainActivity">
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginBottom="10dp"
android:src="@mipmap/ic_launcher"
android:scaleType="centerCrop"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity=""
android:text="CENTER"/>
</FrameLayout>
4. Table Layout
In Android, Table Layout is used to arrange the group of views into rows and
columns. Table Layout containers do not display a border line for their columns, rows
or cells.
For building a row in a table we will use the <TableRow> element. Table row
objects are the child views of a table layout.
Each row of the table has zero or more cells and each cell can hold only one view
object like ImageView, TextView or any other view.
Total width of a table is defined by its parent container
Column can be both stretchable and shrinkable. If shrinkable then the width of
column can be shrunk to fit the table into its parent object and if stretchable then
it can expand in width to fit any extra space available.
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000">
<TableRow android:padding="5dip">
<TextView
android:layout_height="wrap_content"
android:text="@string/loginForm"
android:textColor="#0ff"
android:textSize="25sp"
android:textStyle="bold" />
</TableRow>
<TableRow>
<TextView
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_marginLeft="10dp"
android:text="@string/userName"
android:textColor="#fff"
android:textSize="16sp" />
<EditText
android:id="@+id/userName"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_marginLeft="10dp"
android:background="#fff"
android:hint="@string/userName"
android:padding="5dp"
android:textColor="#000" />
</TableRow>
</TableLayout>
Relative Layout
In Relative Layout we need to specify the position of child views relative to each
other or relative to the parent. In case if we didn’t specify the position of child views,
by default all child views are positioned to top-left of the layout.
layout_alignParentBotto If it specified “true”, the bottom edge of view will match the
m bottom edge of parent.
layout_alignParentLeft If it specified “true”, the left edge of view will match the left edge
of parent.
layout_alignParentRight If it specified “true”, the right edge of view will match the right
edge of parent.
layout_above It accepts another sibling view id and places the view above the
specified view id.
layout_below It accepts another sibling view id and places the view below the
specified view id.
layout_toLeftOf It accepts another sibling view id and places the view left of the
specified view id.
layout_toRightOf It accepts another sibling view id and places the view right of the
specified view id.
layout_toStartOf It accepts another sibling view id and places the view to start of the
specified view id.
layout_toEndOf It accepts another sibling view id and places the view to end of the
specified view id.
<Button
android:id="@+id/btn4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Button4" />
<Button
android:id="@+id/btn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/btn2"
android:layout_centerHorizontal="true"
android:text="Button5" />
<Button
android:id="@+id/btn6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/btn4"
android:layout_centerHorizontal="true"
android:text="Button6" />
<Button
android:id="@+id/btn7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@+id/btn1"
android:layout_toRightOf="@+id/btn1"
android:layout_alignParentRight="true"
android:text="Button7" />
</RelativeLayout>