Android - Unit 3.1.5
Android - Unit 3.1.5
Android - Unit 3.1.5
An android project requires, consist of various files and folders/directories that provides the needed suppo
rt, resources require for app development and execution/runtime of app.
During the development of app, we have to use various tools, technique, laypout, control, images, coding
resources, and many more utilities. Source of All such a tools/utilities get from different directories of andr
oid project.
The directories structure acts like a container which holds the necessary logic’s, images, customization in
its real form. During the development(compilation) and at run time, android looks into these folders to loca
te each type of assets, resources it needs.
An Android project contains various directories that are categorized into following three groups/types tha
t are discussed below :
1. AndroidProjects
An Android project is the container for our application’s source code, resource files and files such as the A
nt-build and Android Manifest file. An application project is the main type of project and the contents are e
ventually built into an apk file that you install on a device.
2. TestProjects
These projects contains the code that is needed to test our application projects and are built into applicati
ons that run on a device.
3. LibraryProjects
These libraries/directories contain shareable Android source code and resources that we can reference in
Android projects. Library projects cannot be installed onto a device however, they are pulled into the apk
file at build time.
------------------------------------------------
The main objective of an android project is to build an .apk file from it. for this purpose when we start with
an android project then some directories creates by default and some needs few customization to enhan
ce android applications. So let’s understand the actual directories in android project to develop an android
application :
1. /src : This is the most common folder in any android project. It contains our java source code for androi
d application. Under this folder we can see our java source files in which we write logic for android
applications.
2. /gen : In most IDE (such as Eclipse) this folder is created by IDE for configuration purpose. Under this f
older we can find two java files such as BuildConfig.java and R.java. Both files are auto generated by IDE
which helps android project to work smoothly.
3. /Android (version name) : Under this folder we can see some android .jar file which relates to android s
dk platform. We should not modify this file for smooth functioning of our project.
4. /assets : By default this folder is empty, but we can store raw asset files such as music file, data file etc
in this folder. The files retain its original name and structure when compiled into an .apk file.
5. /bin : This is a very important folder in our directory structure because it contains AndroidManifest.xml fi
le and serve/act as an output directory for our project which further build an .apk file.
7. /res : This folder contains lots of required sub folders for the project. Let’s understand these folders one
by one.
a. /drawable : Under this folder we store images (PNG, JPEG, GIF) for our project. Generally, this folder c
omes with five types of sub folders which ensures rendering of images on mobile devices according to its
screen resolution. There are five sub folders with different names such as: drawable-hdpi, drawable- Idpi,
drawable-mdpi, drawable-xhdpi, drawable-xxhdpi (where dpi refers to dot per inch). Normally Google pref
ers.PNG format for images.
b. /layout : Under this folder we store Ul layouts file for our android project, which is written in xml format.
we can store many activity files under this folder. In other words this folder relates to our application Ul lay
outs.
d. /values: In this folder we place predefined application values in the form of XML files that define the vari
able names and their value that will be later referenced in our java source code, in simple words, it holds
all our constant values for our application in one place. In this way we can customize our applications at o
ne place as per our requirements. Within this folder we can find three xml files such as:
i. dimens.xml: This XML file defines the dimension values such as font sizes and standard heights for our
UI.
ii. strings.xml: In this file we define text strings which is used in the application. for example: if we wish to
change the title of an application, then we can simply change in this file instead of doing any hard coding i
n application.
iii. styles.xml: This XML file defines styles which is used in the application. Generally we apply these style
s to our Ul elements. With the help of style we can separate the look of our application from the layout an
d functionality.
8. /project.properties: This file contains project settings, such as the build target. We can edit project prop
erties in eUl screen components.
A typical user interface of an android application consists of action bar and the application content area.
2. View Control
3. Content Area
The basic unit of android application is the activity. A Ul is defined in an xml file. During compilation, each
element in the XML is compiled into equivalent Android GUI class with attributes represented by methods.
b. View and ViewGroups
An activity is consist of views. A view is just a widget that appears on the screen. It could be button, etc O
ne or more views can be grouped together into one GroupView. Example of ViewGroup includes layouts.
1. Views are the basic User Interface class, used for constructing, viewing the visual interface elements c
ommonly known as UI controls or widgets.
All Ul controls, and the layout classes are derived from Views class.
2. ViewGroups are extensions of the View class that can contain multiple child Views. The View Group cla
ss can create compound controls that are made up of interconnected child Views. It also extended to prov
ide the layout managers, such as Linear Layout, that help you compose User Interfaces.
3. Activities represent the window or screen that are displayed to the user. Activities works as UI form.To
display an UI form to the mobile user, you have to Assign a View or layout to an Activity.
Android provides several common Ul controls (widgets) and layout managers.
---------------------------
Following are five types of layoutes that are need to set in every android app :
1. Linear Layout
2. Absolute Layout
3. Table Layout
4. Frame Layout
5. Relative Layout
Linear layout is further divided into horizontal and vertical layout. It means it can arrange views in a single
column or in a single row.
The sample/example code for linear layout(vertical) is shown below, that includes one text view:
<LinearLayoutxmlns:android="http://schemas.andriod.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="@string/hello"/>
</LinearLayout>
1.android:id
This attribute defines/set an unique ID number for linear layout which is needed to identifies the Linear L
ayout.
2.android:baselineAligned
This attribute has a boolean value, either "true" or "false" this attribute is used to prevent(whether to allow
or does not allow) the layout from aligning its children’s baselines.
3.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.
4.android:divider
This is drawable to use as a vertical divider between buttons. You use a color value, in the form of "#rgb",
"#argb", "#rrggbb", or "#aarrggbb".
5.android:gravity
This specifies how an object should position its content, on both the X and Y axes. Possible values are to
p, bottom, left, right, center, center_vertical, center_horizontal etc.
6.android:orientation
This attribute specifies the direction of arrangement of UI controls.Orientation value is either "horizontal" f
or a row, "vertical" for a column. The default value is horizontal.
7.android:weightSum
------------------------------
The Absolute Layout is used to set the layout for an android app when developer want to view varoius U
I controls(children) at exact locations/positions.
Absolute layout can be declared like this :
<AbsoluteLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android" >
<Button
android:layout_width="188dp"
android:layout_height="wrap_content"
android:text="Button"
android:layout_x="126px"
android:layout_y="361px" />
</AbsoluteLayout>
An Absolute Layout lets you specify exact locations (x/y coordinates) of its children (UI control or child acti
vity). Absolute layouts are less flexible and harder to maintain than other types of layouts without absolute
positioning.
-------------------------------------------------
1. android:id
This attribute defines/set an unique ID number for absolute layout which is needed to identifies the Absol
ute Layout .
2.
android:layout_x
3. android:layout_y
1. AbsoluteLayout(Context context)
The Table Layout is used to set the layout for an android app when developer want to view varoius UI c
ontrols in tabular format, in the from of rows and coloumns.
Table layout can be declared like this :
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent" >
<TableRow>
<TextView
android:text="User Name:"
android:width="120dp"
<EditText
android:id="@+id/txtUserName"
android:width="200dp" />
</TableRow>
</TableLayout>
Android Table Layout going to be arranged groups of views into rows and columns. You will use the <Tabl
eRow> element to build a row in the table. Each row has zero or more cells; each cell can hold one View
object.
1.
android:id
This attribute defines/set an unique ID number for Table layout which is needed to identifies the Table L
ayout .
2. android:collapseColumns
This specifies the zero-based index of the columns to collapse. The column indices must be separated by
a comma: 1, 2, 5.
3.
android:shrinkColumns
The zero-based index of the columns to shrink. The column indices must be separated by a comma: 1, 2,
5
. android:stretchColumns
The zero-based index of the columns to stretch. The column indices must be separated by a
comma 1,2,5.
------------------------------
The Relative Layout enables you to specify how child views are positioned relative to each other. It can b
e declared like this.
<RelativeLayout
android:id="@+id/RLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android" >
</RelativeLayout>
Android Relative Layout enables you to specify how child views are positioned relative to each other. The
position of each view can be specified as relative to sibling elements or relative to the parent.
1.
android:id
This attribute defines/set an unique ID number for Relative layout which is needed to identifies the Relati
ve Layout .
2. android:gravity
This specifies how an object should position its content, on both the X and Y axes. Possible values are to
p, bottom, left, right, center, center_vertical, center_horizontal etc.
3. android:ignoreGravity
Using Relative Layout, you can align two elements by right border, or make one below another, centered i
n the screen, centered left, and so on. By default, all child views are drawn at the top-left of the layout, so
you must define the position of each view using the various layout properties available from Relative Layo
ut. Layout Params and few of the important attributes are given below-
1 android:layout_above
Positions the bottom edge of this view above the given anchor view ID and must be a reference to anothe
r resource, "@[+][package:]type:name" in the form
2 android:layout_alignBottom
Makes the bottom edge of this view match the bottom edge of the given anchor view ID and must be a ref
erence to another resource, ource, in the form "@[+][package:]type:name".
3 android:layout_alignLeft
Makes the left edge of this view match the left orige of the given anchor view ID and must be a reference t
o another resource, "@[+][package:]type:name". in the form
4 android:layout_alignParentBottom
If true, makes the bottom edge of this view match the bottom edge of the parent. Must be a boolean value
, either "true" or "false".
5 android:layout_alignParentEnd
If true, makes the end edge of this view match the end edge of the parent. Must be a boolean value, eithe
r "true" or "false".
6 android:layout_aligr.ParentLeft
If true, makes the left edge of this view match the left edge of the parent. Must be a boolean value, either
"true" or "false".
7 android:layout_alignParentRight
If true, makes the right edge of this view match the right edge of the parent. Must be a boolean value, eith
er "true" or "false".
8 android:layout_alignParentStart
If true, makes the start edge of this view match the start edge of the parent. Must be a boolean value, eith
er "true" or "false".
9 android:layout_alignParentTop
If true, makes the top edge of this view match the top edge of the parent. Must be a boolean value, either
"true" or "false".
10
android:layout_alignRight
Makes the right edge of this view match the right edge of the given anchor view ID and must be a referen
ce to another resource, in the form "@[+][package:]type:name".
11 android:layout_alignStart
Makes the start edge of this view match the start edge of the given anctior view ID and must be a referenc
e to another resource, in the form "@[+][package:]type:name".
12 android:layout_alignTop
Makes the top edge of this view match the top edge of the given anchor view ID and must be a reference
to another resource, "@[+][package:]type:name". in the form
13
android:layout_below
Positions the top edge of this view below the given anchor view ID and must be a reference to another res
ource, in the form "@[+][package:]type:name".
14 android:layout_centerHorizontal
If true, centers this child horizontally within its parent. Must be a boolean value, either "true" or "false".
15
android:layout_centerinParent
If true, centers this child horizontally and vertically within its parent. Must be a boolean value, either "true"
or "false".
16
android:layout_centerVertical
If true, centers this chila vertically within its parent. Must be a boolean value, either "true" or "false".
17
android:layout_toEndOf
Positions the start edge of this view to the end of the given anchor view ID and must be a reference to an
other resource, in the form
"@[+][package:]type:name".
18
android:layout_toLeftOf
Positions the right edge of this view to the left of the given anchor view ID and must be a reference to ano
ther resource, in the form
"@[+][package:]type:name".
19
android:layout_toRightOf
Positions the left edge of this view to the right of the given anchor view ID and must be a reference to ano
ther resource, in the form
"@[+][package:]type:name".
20
android:layout_toStartOf
Positions the end edge of this view to the start of the given anchor view ID and must be a reference to an
other resource, in the form "@[+][package:]type:name".
---------------------------
The Frame Layout is a placeholder on screen that you can use to display a single view. It can be declared
like this :
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/lblComments"
android:layout_below="@+id/lblComments"
android:layout_centerHorizontal="true" >
<ImageView
android:src="@drawable/droid"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
Frame layout is designed to block out an area on the screen to display a single item.
Generally, Frame Layout 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.
You can, however, add multiple children to a Frame Layout and contro, their position within the Frame La
yout by assigning gravity to each child, using the android:layout_gravity attribute.
---------------------------------------------
1 android:id
This attribute defines/set an unique ID number for Frame layout which is needed to identifies the Frame
Layout .
2 android:foreground
This defines the drawable to draw over the content and possible values may be a color value, in the form
of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".
3 android:foregroundGravity
Defines the gravity to apply to the foreground drawable. The gravity defaults to fill. Possible values are top
, bottom, left, right, center, center_vertical, center_horizontal etc.
4 android:measureAllChildren
Determine whether to measure all children or just those in the VISIBLE or INVISIBLE state when measuri
ng.Defaults to false.