Chapter 1 Mad
Chapter 1 Mad
Android is a complete set of software for mobile devices such as tablet computers, notebooks,
smartphones, electronic book readers, set-top boxes etc.
It contains a Linux-based Operating System, middleware and key mobile applications.
It can be thought of as a mobile operating system. But it is not limited to mobile only. It is
currently used in various devices such as mobiles, tablets, televisions etc.
What is Android?
Android is a software package and linux based operating system for mobile devices such as
tablet computers and smartphones.
It is developed by Google and later the OHA (Open Handset Alliance). Java language is
mainly used to write the android code even though other languages can be used.
The goal of android project is to create a successful real-world product that improves the
mobile experience for end users.
There are many code names of android such as Lollipop, Kitkat, Jelly Bean, Ice cream
Sandwich, Froyo, Ecliar, Donut etc
Android Ecosystem
If we want to learn about Android and develop Android Applications, it is very important for
us to understand the entire ecosystem of Android and the stakeholders of this ecosystem.
The stakeholders are the consumers that own Android devices. But there are others as well
1.Google: it develops Android.
2. OEMs (Original Equipment Manufacturers):
They Manufacture the hardware, and as well the custom application components.
3. Application Development Companies:
They are the major contributors to the ecosystem and employ Android developers, and also
contract out the product development to services companies.
4. Freelance Android developers:
Developers have the skill-set to contribute to the ecosystem for Android development, they are
who create their own application, and publish them on Google play store.
Freelancers can also generate money by developing applications for product companies.
Why Android?
There are some points:
1. Browser:
The Android browser is one of the best browsers on the mobile market.
It generally loads pages faster than Safari or any other browser, has flash support and simply
does everything a browser should do.
E.g. iPhone has safari browser it is stable, has no flash support, so you cannot watch
YouTube videos or any related contents, it is not flexible.
2. Desktop:
The Android phone adds widgets to the desktop. The purpose for the widget
e.g.
The Facebook widget allows you to update your Facebook desktop.
The people widget allows you to make possible different actions for different contacts right
from your desktop.
The message widget allows you to immediately see your email from the desktop.
3. Connectivity: On one page/desktop you could be able to have four connecting device tool
button.
Turn on/off Bluetooth.
Turn on/off WiFi.
Turn on/off mobile network.
Turn on/off GPS.
These buttons let you switch on/off instantly which will help you to conserve battery life.
4. Multi-Notification: Android Phone have multi-notification system. With android the app
have access to the notification system and call all report.
5. Endless Personalization: The Android cell phone allows client to configuration their mobile
to look and behave exactly like they want.
6. Market: Android has an android market. The Android apps are free and work as well.
7. Google Integration: The Android has inbuilt google support. E.g. Google map, Gmail etc.
8. Open Source: The code of android OS as well as the apps is available.
9. Open to Carrier: If you know java then you are open to Android world.
10. Future: The future mobile phones are basically going to be smart phones.
Features of Android
History of Android
History and versions of android are interesting to know. The code names of android ranges
from A to J currently, such as Aestro, Blender, Cupcake, Donut, Eclair, Froyo, Gingerbread,
Honeycomb, Ice Cream Sandwitch, Jelly Bean, KitKat and Lollipop. Let's understand the
android history in a sequence.
1) Initially, Andy Rubin founded Android Incorporation in Palo Alto, California, United
States in October, 2003.
2) In 17th August 2005, Google acquired android Incorporation. Since then, it is in the
subsidiary of Google Incorporation.
3) The key employees of Android Incorporation are Andy Rubin, Rich Miner, Chris White
and Nick Sears.
4) Originally intended for camera but shifted to smart phones later because of low market for
camera only.
5) Android is the nick name of Andy Rubin given by coworkers because of his love to robots.
6) In 2007, Google announces the development of android OS.
7) In 2008, HTC launched the first android mobile.
1.5 Cupcake 3
1.6 Donut 4
2.1 Eclair 7
2.2 Froyo 8
4.4 KitKat 19
5.0 Lollipop 21
6.0 Marshmallow 23
1. Linux kernel
It is the heart of android architecture that exists at the root of android architecture. Linux
kernel is responsible for device drivers, power management, memory management, device
management and resource access
2) Native Libraries
On the top of linux kernel, there are Native libraries such as WebKit, OpenGL, FreeType,
SQLite, Media, C runtime library (libc) etc.
The WebKit library is responsible for browser support, SQLite is for database, FreeType for
font support, Media for playing and recording audio and video formats.
3. Android Runtime
In android runtime, there are core libraries and DVM (Dalvik Virtual Machine) which is
responsible to run android application. DVM is like JVM but it is optimized for mobile devices.
It consumes less memory and provides fast performance.
4) Android Framework
On the top of Native libraries and android runtime, there is android framework. Android
framework includes Android API's such as UI (User Interface), telephony, resources, locations,
Content Providers (data) and package managers. It provides a lot of classes and interfaces for
android application development.
5) Applications
On the top of android framework, there are applications. All applications such as home, contact,
settings, games, browsers are using android framework that uses android runtime and libraries.
Android runtime and native libraries are using linux kernal
Chapter No: 2
The android SDK (Software development kit) is the most important software of
android which is installed.
The android SDK provides to test android applications, the API libraries, an
emulator, documentation, sample code, developer tools and tutorials which help you
to build, test, and debug apps for android.
Android SDK can be downloaded from http://developer.android.com/sdk/index.html
using android SDK manager which is free.
We find the android SDK in a zip file and have to unzip its contents into a folder.
We can also create android virtual devices (AVD) via the android SDK, which run in
the emulator.
Tools
Packages
When you first install the SDK all you obtain are the base tools.
These are executables and supporting files that help you develop applications.
The packages are the records specific to a particular version of android (called a platform) or
a particular add- on to a platform.
Android development tools is a plugin for the eclipse IDE which provides a suitable
environment to develop an android application where creation, compilation and
debugging are possible.
ADT can able to set up new android projects, create an application UI, insert packages
based on the android framework API, Debug your applications using the android SDK
tools and export signed or unsigned .apk files in order to distribute in the application.
Also ADT provides custom XML editors and debug output pane from which we get
the unbelievable enhance during develop the android applications.
Defines the hardware features of the virtual device. For example, you can describe
whether the device has camera, whether it uses a physical keyboard or a dialing pad,
how much memory it have and so on.
You can define what version of the android platform will run on the virtual machine.
You will be able to choose a version of the standard android platform or the system
image packaged with an sdk add on.
You can specify the emulator skin you want to use with the AVD which lets you to
control the screen dimensions, look, and so on.
A dedicated storage area on your development machine:
The devices user data (installed, applications, settings, and so on) and emulated SD
card are stored in this area.
We can generate as many AVDS as we want to analysis the applications with some
different configurations.
This testing is essential to confirm the performance of the applications when it is run
on different devices with changing capabilities.
Emulators
Android Emulator is used to run, debug and test the android application. If you don't
have the real device, it can be the best way to run, debug and test the application.
It uses an open source processor emulator technology called QEMU.
The emulator tool enables you to start the emulator from the command line. You
need to write: emulator -avd <AVD NAME>
In case of Eclipse IDE, you can create AVD by Window menu > AVD Manager >
New.
In the given image, you can see the android emulator, it displays the output of the
hello android example.
The android SDK comes with a virtual mobile device emulator that runs on your
computer.
The emulator enables us trial product, to develop and test android applications
without using a physical device.
The android emulator mimics all of the hardware and software features of typical
mobile devices, except that it cannot place actual phone calls.
It provides a selection of navigation and control keys, which you can press using your
mouse or keyboard to generate events for our application.
It provides a screen in which our application is displayed, together with any other
active android applications.
The android emulator supports many hardware features likely to be found on mobile
devices including: An ARM v5 CPU and the corresponding memory management
unit(MMU)
A 16 bit LCD display
one or more keyboards
A sound chip among output and input capabilities.
Flash memory partitions.
A GSM modem including a simulated SIM card.
A camera using a webcam connected to our development computer.
Sensors like an accelerometer, using records from a USB connected Android device.
As we know the modern JVM is high performance and provides excellent memory
management. But it needs to be optimized for low-powered handheld devices as well.
The Dalvik Virtual Machine (DVM) is an android virtual machine optimized for
mobile devices. It optimizes the virtual machine for memory, battery life and
performance.
Dalvik is a name of a town in Iceland. The Dalvik VM was written by Dan Bornstein.
The Dex compiler converts the class files into the .dex file that run on the Dalvik
VM. Multiple class files are converted into one dex file.
Let's see the compiling and packaging process from the source file:
The javac tool compiles the java source file into the class file.
The dx tool takes all the class files of your application and generates a single .dex file.
It is a platform-specific tool.
The Android Assets Packaging Tool (aapt) handles the packaging process.
JVM DVM
1 JVM supports multiple operating system. DVM supports only android operating
system.
2 It compiles java bytecode It compiles Dalvik bytecode
3 It forms separate classes in separate It forms multiple class in one .dex byte code
.class byte code file. file.
4 It runs on more memory It runs on less memory.
5 JVM is based on stack based virtual DVM is based on Register based virtual
machine architecture. machine architecture.
6 The executable format of JVM is The executable format of DVM is APK(only
JAR(one or more .class files.) one .dex files.)
7 It has different constant pools It has common constant pool.
8 JVM runs .class byte code directly the .class byte codes are optimized to .dex
format before executing in DVM
In this page, you will know how to create the simple hello android application. We
are creating the simple example of android using the Eclipse IDE. For creating the
simple example:
Create the new android project
Write the message (optional)
Run the android application
You need to follow the 3 steps mentioned above for creating the Hello android
application.
After finishing the Activity configuration, Android Studio auto generates the activity
class and other required configuration files.
Now an android project has been created. You can explore the android project and
see the simple program, it looks like this:
Chapter No: 3
UI COMPONENTS AND LAYOUTS
UI Layouts
The basic building block for user interface is a View object which is created from the
View class and occupies a rectangular area on the screen and is responsible for
drawing and event handling.
View is the base class for widgets, which are used to create interactive UI components
like buttons, text fields, etc.
The ViewGroup is a subclass of View and provides invisible container that hold other
Views or other ViewGroups and define their layout properties.
At third level we have different layouts which are subclasses of ViewGroup class and
a typical layout defines the visual structure for an Android user interface and can be
created either at run time using View/ViewGroup objects or you can declare your
layout using simple XML file main_layout.xml which is located in the res/layout
folder of your project.
1. Linear Layout
LinearLayout is a view group that aligns all children in a single direction, vertically or
horizontally.
Android LinearLayout is a view group that aligns all children in either vertically or
horizontally.
We are having two options
1. fill_parent
2. wrap_content
1. fill_parent_width
change the “layout_width” to “fill_parent” now the buttons width will fill in the
remaining spaces, just as big as its parent “linearlayout”, but buttons height is still
enough to enclose its content only.
2. wrap_content:
A button component set “wrap_content” on both width and height attribute. it tell
android to display the button big enough to enclose its content.
LinearLayout Attributes
Following are the important attributes specific to LinearLayout −
1. android:id
This is the ID which uniquely identifies the layout.
2. android:baselineAligned
This must be a boolean value, either "true" or "false" and prevents 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 top, bottom, left, right, center, center_vertical, center_horizontal etc.
6. android:orientation
This specifies the direction of arrangement and you will use "horizontal" for a row, "vertical"
for a column. The default is horizontal.
7. android:weightSum
Sum up of child weight
Example
<Button android:id="@+id/btnStartService"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:text="start_service"/>
<Button android:id="@+id/btnPauseService"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:text="pause_service"/>
<Button android:id="@+id/btnStopService"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:text="stop_service"/>
</LinearLayout>
2. Relative Layout
RelativeLayout Attributes
Following are the important attributes specific to RelativeLayout −
1. android:id
This is the ID which uniquely identifies the layout.
2. 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.
3. android:ignoreGravity
This indicates what view should not be affected by gravity.
Using RelativeLayout, you can align two elements by right border, or make one below
another, centered in 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 RelativeLayout.LayoutParams 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 another resource, in the form "@[+][package:]type:name"
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 reference to another resource, in the form "@[+][package:]type:name".
3. android:layout_alignLeft
Makes the left edge of this view match the left edge of the given anchor view ID and must be
a reference to another resource, in the form "@[+][package:]type:name".
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, either "true" or "false"
6. android:layout_alignParentLeft
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, either "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, either "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 reference 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 anchor view ID and must
be a reference 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, in the form "@[+][package:]type:name".
13. android:layout_below
Positions the top edge of this view below the given anchor view ID and must be a reference
to another resource, 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 child 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 another 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 another 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 another 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 another resource, in the form "@[+][package:]type:name".
package com.example.demo;
import android.os.Bundle;
import android.app.Activity;
}
Following will be the content of res/layout/activity_main.xml file –
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp" >
<EditText
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/reminder" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentStart="true"
android:layout_below="@+id/name">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button2" />
</LinearLayout>
</RelativeLayout>
o/p
3. Table Layout
TableLayout containers do not display border lines for their rows, columns, or cells.
TableLayout Attributes
Following are the important attributes specific to TableLayout −
1. android:id
This is the ID which uniquely identifies the layout.
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.
4. android:stretchColumns
The zero-based index of the columns to stretch. The column indices must be separated by a
comma: 1, 2, 5.
example
Following is the content of the modified main activity file
src/com.example.demo/MainActivity.java. This file can include each of the fundamental
lifecycle methods.
package com.example.demo;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:text="Time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1" />
<TextClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textClock"
android:layout_column="2" />
</TableRow>
<TableRow>
<TextView
android:text="First Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1" />
<EditText
android:width="200px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<TableRow>
<TextView
android:text="Last Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1" />
<EditText
android:width="100px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ratingBar"
android:layout_column="2" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:id="@+id/button"
android:layout_column="2" />
</TableRow>
</TableLayout>
4. Frame Layout
The FrameLayout is a placeholder on screen that you can use to display a single view.
Frame Layout 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.
You can, however, 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
FrameLayout Attributes
Following are the important attributes specific to FrameLayout −
1. android:id
This is the ID which uniquely identifies the 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
Determines whether to measure all children or just those in the VISIBLE or INVISIBLE state
when measuring. Defaults to false.
Following is the content of the modified main activity file
src/com.example.demo/MainActivity.java. This file can include each of the fundamental
lifecycle methods.
package com.example.demo;
import android.os.Bundle;
import android.app.Activity;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Following will be the content of res/layout/activity_main.xml file −
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<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>