0% found this document useful (0 votes)
5 views

Android Development

Uploaded by

anjeru
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Android Development

Uploaded by

anjeru
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

Android

Development
By: Ambrose Njeru [BSc, Msc ComputerScience] 1
Introduction
 Android is an open source and Linux-based operating
system for mobile devices such as smartphones and
tablet computers.
 Android was developed by the Open Handset Alliance,
led by Google, and other companies.
 The official language for Android development is Java.
 Large parts of Android are written in Java and its APIs
are designed to be called primarily from Java.
 The first beta version of the Android Software
Development Kit (SDK) was released by Google in 2007
where as the first commercial version, Android 1.0, was
released in September 2008.
By: Ambrose Njeru [BSc, Msc ComputerScience] 2
Introduction
 The source code for Android is available under free
and open source software licenses.

By: Ambrose Njeru [BSc, Msc ComputerScience] 3


Features of Android
1. Beautiful User Interface: Android OS basic screen
provides a beautiful and intuitive user interface.
2. Connectivity: GSM/EDGE, IDEN, CDMA, EV-DO,
UMTS, Bluetooth, Wi-Fi, LTE, NFC and WiMAX.
3. Storage: SQLite, a lightweight relational database, is
used for data storage purposes.
4. Media support: H.263, H.264, MPEG-4 SP, AMR,
AMR-WB, AAC, HE-AAC, AAC 5.1, MP3, MIDI, Ogg
Vorbis, WAV, JPEG, PNG, GIF, and BMP.
5. Web browser: Based on the open-source WebKit
layout engine, coupled with Chrome's V8 JavaScript
engine supporting HTML5 and CSS3.
By: Ambrose Njeru [BSc, Msc ComputerScience] 4
Features of Android
6. Messaging: SMS and MMS
7. Multi-touch: Android has native support for multi-
touch which was initially made available in handsets
such as the HTC Hero.
8. Multi-tasking: User can jump from one task to
another and same time various application can run
simultaneously.
9. Resizable widgets: Widgets are resizable, so users
can expand them to show more content or shrink
them to save space.
10. Multi-Language: Supports single direction and bi-
directional text.
By: Ambrose Njeru [BSc, Msc ComputerScience] 5
Features of Android
11. Google Cloud Messaging (GCM): This is a service
that lets developers send short message data to their
users on Android devices, without needing a
proprietary sync solution.
12. Wi-Fi Direct: A technology that lets apps discover
and pair directly, over a high-bandwidth peer-to-peer
connection.
13. Android Beam: A popular NFC-based technology
that lets users instantly share, just by touching two
NFC-enabled phones together.

By: Ambrose Njeru [BSc, Msc ComputerScience] 6


Android Application
 Android applications are usually developed in the Java
language using the Android Software Development Kit.
 Once developed, Android applications are packaged
easily and uploaded to the various App Stores such as
Google Play, SlideME, Opera Mobile Store, Mobango,
F-droid and the Amazon Appstore.
 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.

By: Ambrose Njeru [BSc, Msc ComputerScience] 7


Android Application
 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), a consortium of 84 companies
such as Google, Samsung.
 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.

By: Ambrose Njeru [BSc, Msc ComputerScience] 8


History of Android
 Initially, Andy Rubin founded Android Incorporation
in Palo Alto, California, United States in October,
2003.
 In 17th August 2005, Google acquired android
Incorporation. Since then, it is in the subsidiary of
Google Incorporation.
 The key employees of Android Incorporation are
Andy Rubin, Rich Miner, Chris White and Nick Sears.
 Originally intended for camera but shifted to smart
phones later because of low market for camera only.
 Android is the nick name of Andy Rubin given by
coworkers because of his love to robots.
By: Ambrose Njeru [BSc, Msc ComputerScience] 9
History of Android
 In 2007, Google announced the development of
android OS.
 In 2008, HTC launched the first android mobile.

By: Ambrose Njeru [BSc, Msc ComputerScience] 10


Android Architecture
 The Android architecture or Android software stack
is categorized into five parts:

By: Ambrose Njeru [BSc, Msc ComputerScience] 11


Android Architecture
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 – Lies on the top of the linux
kernel. The native libraries include 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.
By: Ambrose Njeru [BSc, Msc ComputerScience] 12
Android Architecture
3. Android Runtime – Consists of 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 – Lies on the top of native
libraries and android runtime. 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.
By: Ambrose Njeru [BSc, Msc ComputerScience] 13
Android Architecture
 The Android framework includes the following key
services −
 Activity Manager − Controls all aspects of the
application lifecycle and activity stack.
 Content Providers − Allows applications to
publish and share data with other applications.
 Resource Manager − Provides access to non-
code embedded resources such as strings, color
settings and user interface layouts.
 Notifications Manager − Allows applications to
display alerts and notifications to the user.

By: Ambrose Njeru [BSc, Msc ComputerScience] 14


Android Architecture
 View System − An extensible set of views used to
create application user interfaces.
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.

By: Ambrose Njeru [BSc, Msc ComputerScience] 15


Core Building Blocks
 An android component is a piece of code that has a
well defined life cycle e.g. Activity, Receiver, Service
etc.
 The core building blocks or fundamental
components of android are activities, views, intents,
services, content providers, fragments and
AndroidManifest.xml.
 Activity – This is a class that represents a single
screen with a user interface, i.e performs actions on
the screen. It is like a Frame in AWT. An activity is
implemented as a subclass of Activity class −
public class MainActivity extends Activity {
} By: Ambrose Njeru [BSc, Msc ComputerScience] 16
Core Building Blocks
 Service – This is a component that runs in the
background to perform long-running operations
without needing to interact with the user and it
works even if application is destroyed.. There are two
types of services: Local service is accessed from
within the application whereas remote service is
accessed remotely from other applications running
on the same device. For example, a service might play
music in the background while the user is in a
different application. A service is implemented as a
subclass of Service class
public class MyService extends Service {
} By: Ambrose Njeru [BSc, Msc ComputerScience] 17
Core Building Blocks
 Broadcast Receivers – They respond to broadcast
messages from other applications or from the system.
For example, applications can initiate broadcasts to
let other applications know that some data has been
downloaded to the device and is available for them to
use, so this is broadcast receiver who will intercept
this communication and will initiate appropriate
action. A broadcast receiver is implemented as a
subclass of BroadcastReceiver class and each
message is broadcaster as an Intent object.
public class MyReceiver extends BroadcastReceiver {
public void onReceive(context,intent){}
} By: Ambrose Njeru [BSc, Msc ComputerScience] 18
Core Building Blocks
 Content Providers – Used to supply data from one
application to others on request. Such requests are
handled by the methods of the ContentResolver
class. The data may be stored in the file system, the
database or somewhere else entirely. A content
provider is implemented as a subclass of
ContentProvider class and must implement a
standard set of APIs that enable other applications to
perform transactions.
public class MyContentProvider extends
ContentProvider {
public void onCreate(){}
} By: Ambrose Njeru [BSc, Msc ComputerScience] 19
Core Building Blocks
 There are additional components which are used:
 Intent – This is used to invoke components. It is
mainly used to:
 Start the service
 Launch an activity
 Display a web page
 Display a list of contacts
 Broadcast a message
 Dial a phone call etc.
 Fragments – Represents a portion of user interface in
an Activity. An activity can display one or more
fragments on the screen at the same time.
By: Ambrose Njeru [BSc, Msc ComputerScience] 2
0
Core Building Blocks
 View – This is the UI element such as button, label,
text field etc. Anything that you see is a view.
 Layouts - View hierarchies that control screen format
and appearance of the views.
 AndroidManifest.xml - Contains information about
activities, content providers, permissions etc. It is like
the web.xml file in Java EE.
 Android Virtual Device (AVD) - It is used to test the
android application without the need for mobile or
tablet etc. It can be created in different configurations
to emulate different types of real devices.

By: Ambrose Njeru [BSc, Msc ComputerScience] 21


Anatomy of Android Application

By: Ambrose Njeru [BSc, Msc ComputerScience] 22


Anatomy of Android Application
1. Java - This contains the .java source files for your
project. By default, it includes an MainActivity.java
source file having an activity class that runs when
your app is launched using the app icon.
2. res/drawable-hdpi - This is a directory for drawable
objects that are designed for high-density screens.
3. res/layout - This is a directory for files that define
your app's user interface.
4. res/values - This is a directory for other various XML
files that contain a collection of resources, such as
strings and colours definitions.

By: Ambrose Njeru [BSc, Msc ComputerScience] 23


Anatomy of Android Application
5. AndroidManifest.xml - This is the manifest file
which describes the fundamental characteristics of
the app and defines each of its components.
6. build.gradle - This is an auto generated file which
contains compileSdkVersion, buildToolsVersion,
applicationId, minSdkVersion, targetSdkVersion,
versionCode and versionName

By: Ambrose Njeru [BSc, Msc ComputerScience] 24


Important Application Files
1. The Main Activity File.
 This is a Java file MainActivity.java.
 This is the actual application file which ultimately
gets converted to a Dalvik executable and runs your
application.
 Sample code generated by the application:
package com.example.helloworld;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends
AppCompatActivity {
@Override
By: Ambrose Njeru [BSc, Msc ComputerScience] 25
Important Application Files
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
 R.layout.activity_main refers to the
activity_main.xml file located in the res/layout
folder.
 The onCreate() method is one of many methods that
are figured when an activity is loaded.

By: Ambrose Njeru [BSc, Msc ComputerScience] 2


6
Important Application Files
2. The Manifest File.
 All components that you develop as a part of your
application, are all declared in the manifest.xml file
which resides at the root of the application project
directory.
 The file works as an interface between Android OS
and your application, so if you do not declare your
component in this file, then it will not be considered
by the OS.
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/re
s/android" By: Ambrose Njeru [BSc, Msc ComputerScience] 27
Important Application Files
package="com.example.tutorialspoint7.myapplicatio
n">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
By: Ambrose Njeru [BSc, Msc ComputerScience] 2
8
Important Application Files
<category
android:name="android.intent.category.LAUNCHER
" />
</intent-filter>
</activity>
</application>
</manifest>

By: Ambrose Njeru [BSc, Msc ComputerScience] 2


9
Important Application Files
 The <application>...</application> tags encloses the
components related to the application.
 Attribute android:icon will point to the application
icon available under res/drawable-hdpi.
 The application uses the image named
ic_launcher.png located in the drawable folders
 The <activity> tag is used to specify an activity and
android:name attribute specifies the fully qualified
class name of the Activity subclass and the
android:label attributes specifies a string to use as
the label for the activity.
 You can specify multiple activities using <activity>
tags. By: Ambrose Njeru [BSc, Msc ComputerScience] 30
Important Application Files
 The action for the intent filter is named
android.intent.action.MAIN to indicate that this
activity serves as the entry point for the application.
 The category for the intent-filter is named
android.intent.category.LAUNCHER to indicate that
the application can be launched from the device's
launcher icon.
 The @string refers to the strings.xml file.
 @string/app_name refers to the app_name string
defined in the strings.xml file, which is
"HelloWorld".
 Similar way, other strings get populated in the
application. By: Ambrose Njeru [BSc, Msc ComputerScience] 31
Important Application Files
 The following list of tags can be used in your
manifest file to specify different Android application
components −
 <activity>elements for activities.
 <service> elements for services.
 <receiver> elements for broadcast receivers.
 <provider> elements for content providers.
3. The Strings File
 The strings.xml file is located in the res/values folder
and it contains all the text that your application uses.
For example, the names of buttons, labels, default
text, and similar types of strings go into this file.
By: Ambrose Njeru [BSc, Msc ComputerScience] 32
Important Application Files
 This file is responsible for their textual content.
 A default strings file will look like:
<resources>
<string name="app_name">HelloWorld</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string
name="title_activity_main">MainActivity</string>
</resources>
4. The Layout File
 The activity_main.xml is a layout file available in
res/layout directory, that is referenced by your
application when building its interface.
By: Ambrose Njeru [BSc, Msc ComputerScience] 33
Important Application Files
 The file is modified very frequently to change the
layout of your application.
 For the"Hello World!" application, the file will have
following content related to default layout:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/re
s/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
By: Ambrose Njeru [BSc, Msc ComputerScience] 34
Important Application Files
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="@dimen/padding_medium"
android:text="@string/hello_world"
tools:context=".MainActivity" />
</RelativeLayout>
 This is an example of simple RelativeLayout.
 The TextView is an Android control used to build the
GUI and it have various attributes like
android:layout_width, android:layout_height etc
which are being used to set its width and height etc..
By: Ambrose Njeru [BSc, Msc ComputerScience] 35
Important Application Files
 The @string refers to the strings.xml file located in
the res/values folder.
 @string/hello_world refers to the hello string
defined in the strings.xml file, which is "Hello
World!".

By: Ambrose Njeru [BSc, Msc ComputerScience] 36


Android Resources
 There are many more items which you use to build a
good Android application.
 Apart from coding for the application, you take care
of various other resources like static content that
your code uses, such as bitmaps, colors, layout
definitions, user interface strings, animation
instructions, and more.
 These resources are always maintained separately in
various sub-directories under res/ directory of the
project.

By: Ambrose Njeru [BSc, Msc ComputerScience] 37


Organizing Android Resources
MyProject/
app/
manifest/
AndroidManifest.xml
java/
MyActivity.java
res/
drawable/
icon.png
layout/
activity_main.xml
info.xml
values/
strings.xml
By: Ambrose Njeru [BSc, Msc ComputerScience] 38
Directory & Resource Type
1. anim/ - Contains XML files that define property
animations. They are saved in res/anim/ folder and
accessed from the R.anim class.
2. color/ - Contains XML files that define a state list of
colors. They are saved in res/color/ and accessed from
the R.color class.
3. drawable/ - Contains image files like .png, .jpg, .gif or
XML files that are compiled into bitmaps, state lists,
shapes, animation drawable. They are saved in
res/drawable/ and accessed from the R.drawable class.
4. layout/ - Contains XML files that define a user
interface layout. They are saved in res/layout/ and
accessed from the R.layout class.
By: Ambrose Njeru [BSc, Msc ComputerScience] 39
Directory & Resource Type
5. menu/ - Contains XML files that define application
menus, such as an Options Menu, Context Menu, or
Sub Menu. They are saved in res/menu/ and accessed
from the R.menu class.
6. raw/ - Contains arbitrary files to save in their raw form.
You need to call Resources.openRawResource() with the
resource ID, which is R.raw.filename to open such raw
files.
7. values/ - Contains XML files that contain simple
values, such as strings, integers, and colors. For
example, here are some filename conventions for
resources you can create in this directory −
 arrays.xml for resource arrays, and accessed from the
R.array class.
By: Ambrose Njeru [BSc, Msc ComputerScience] 4
0
Directory & Resource Type
 integers.xml for resource integers, and accessed
from the R.integer class.
 bools.xml for resource boolean, and accessed
from the R.bool class.
 colors.xml for color values, and accessed from the
R.color class.
 dimens.xml for dimension values, and accessed
from the R.dimen class.
 strings.xml for string values, and accessed from
the R.string class.
 styles.xml for styles, and accessed from the R.style
class.
By: Ambrose Njeru [BSc, Msc ComputerScience] 41
Directory & Resource Type
8. xml/ - Contains arbitrary XML files that can be read
at runtime by calling Resources.getXML(). You can
save various configuration files here which will be
used at run time.
 Below is an example which specifies images for a
default screen and alternative images for high
resolution screen.
MyProject/
app/
manifest/
AndroidManifest.xml
java/
MyActivity.java
By: Ambrose Njeru [BSc, Msc ComputerScience] 42
Directory & Resource Type
res/
drawable/
icon.png
background.png
drawable-hdpi/
icon.png
background.png
layout/
activity_main.xml
info.xml
values/
strings.xml
By: Ambrose Njeru [BSc, Msc ComputerScience] 43
Accessing Resources
 During your application development you will need to
access defined resources either in your code, or in your
layout XML files.
 Accessing Resources in Code
 When your Android application is compiled, a R class
gets generated, which contains resource IDs for all the
resources available in your res/ directory.
 You can use R class to access that resource using sub-
directory and resource name or directly resource ID.
 Example: To access res/drawable/myimage.png and set
an ImageView you will use following code −
ImageView imageView = (ImageView)
findViewById(R.id.myimageview);
imageView.setImageResource(R.drawable.myimage);
By: Ambrose Njeru [BSc, Msc ComputerScience] 4
4
Accessing Resources
 First line of the code make use of R.id.myimageview to
get ImageView defined with id myimageview in a Layout
file.
 Second line of code makes use of R.drawable.myimage to
get an image with name myimage available in drawable
sub-directory under /res.
 Accessing Resources in XML
 Consider the following resource XML
res/values/strings.xml file that includes a color resource
and a string resource −
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="opaque_red">#f00</color>
<string name="hello">Hello!</string>
</resources> By: Ambrose Njeru [BSc, Msc ComputerScience] 45
Accessing Resources
 Now you can use these resources in the following layout
file to set the text color and text string as follows −
<?xml version="1.0" encoding="utf-8"?>
<EditText
xmlns:android="http://schemas.android.com/apk/res/an
droid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="@color/opaque_red"
android:text="@string/hello" />

By: Ambrose Njeru [BSc, Msc ComputerScience] 4


6

You might also like