Download as pdf or txt
Download as pdf or txt
You are on page 1of 73

HAWASSA UNIVERSITY

Institute of Technology (IOT)


Faculty of Informatics
Department of Computer Science (BSc – Regular)

Mobile Application Development (CoScXXXX)

Compiled By: Areg. T. 1


Chapter One

Introduction

Compiled By: Areg. T. 2


Outlines
Introduction to Android

Android Application Structure

Android UI Architecture [Application Context, Intent, Activity


Lifecycle]

UI Components [Text control…]


Compiled By: Areg. T. 3
Introduction to Android
Android is an open-source and Linux-based operating system for mobile devices
such as smartphones and tablet computers.

Android is the world’s most popular mobile platform. At the last count, there were
over 2.5 billion active Android devices worldwide, and that number is growing
rapidly.

It’s a powerful development framework that includes everything you need to


build great apps using various languages (like a mix of Java/Kotlin and XML).

Android is developed by a consortium of developers known as the OHA (Open


Handset Alliance) and commercially sponsored by Google.

Compiled By: Areg. T. 4


Android Features
Android is a powerful OS that supports great features. Few of them
are listed below:
Feature Description
Beautiful UI Android OS basic screen provides a beautiful and intuitive user interface.
Connectivity GSM/EDGE, IDEN, CDMA, EV-DO, UMTS, Bluetooth, Wi-Fi, LTE, NFC and WiMAX
Storage SQLite, a lightweight relational database, is used for data storage purposes.
Multi-Language Supports single direction and bi-directional text.
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
Messaging SMS and MMS
Resizable widgets Widgets are resizable, so users can expand them to show more content or shrink
them to save space.

Compiled By: Areg. T. 5


Android History
In 2003, Android Inc. started the development of the Android
operating system.

Later, in 2005, Google purchased it.

On November 5, 2007, Google released the beta version of Android


OS.

Later, on November 12, 2007, the Android software development kit


(SDK) was released.
Compiled By: Areg. T. 6
Cont…
Again in October 2008, the first Android mobile was publicly
released. This mobile was released with Android 1.0.

Compiled By: Areg. T. 7


Android versions, Code names, and API level
Version Numbers Code Name Release Date API Level
1.0 No code name September 23, 2008 1
1.1 >> February 9, 2009 2
1.5 Cupcake April 27, 2009 3
1.6 Donut September 15, 2009 4
2.0 – 2.1 Éclair October 26, 2009 5–7
2.2 – 2.2.3 Froyo May 20, 2010 8
2.3 – 2.3.7 Gingerbread December 6, 2010 9 – 10
3.0 – 3.2.6 Honeycomb February 22, 2011 11 – 13
4.0 – 4.0.4 Ice Cream Sandwich October 18, 2011 14 – 15
4.1 – 4.3.1 Jelly Bean July 9, 2012 16 – 18
4.4 – 4.4.4 KitKat October 31, 2013 19 – 20
Compiled By: Areg. T. 8
Cont…
Version Numbers Code Name Release Date API Level
5.0 – 5.1.1 Lollipop November 12, 2014 21 – 22
6.0 – 6.0.1 Marshmallow October 5, 2015 23
7 Nougat August 22, 2016 24
7.1.0 – 7.1.2 >> October 4, 2016 25
8 Oreo August 21, 2017 26
8.1 >> December 5, 2017 27
9 Pie August 6, 2018 28
10 Quince Tart September 3, 2019 29
11 Red Velvet Cake 2020 30
12 – 12L Snow Cone 2021 31 – 32
13 Tiramisu - 33
Compiled By: Areg. T. 9
Setup the Environment
In order to Setup the Environment that we are working on, we need to
install the followings:

1. Java Development Kit (JDK) : Download and install JDK from


oracle’s official website:
https://www.oracle.com/java/technologies/downloads/

2. Download and install Android studio from the android developers


official website: https://developer.android.com/studio. This software
suite includes the Android SDK.

Compiled By: Areg. T. 10


Android Application Structure
Android uses packages not only to arrange the code in the application
but also to manage the application itself.

Android application in android studio development tool have a pre-


defined structure with code and resource organized into a number of
folders.

Compiled By: Areg. T. 11


Cont…
An Android app is really just a bunch of valid files in a particular
folder structure, and Android Studio sets all of this up for you when
you create a new app as shown below.

The diagram below shows the basic building blocks of an android


application structure.

Compiled By: Areg. T. 12


Compiled By: Areg. T. 13
Cont…
Android Studio projects use the Gradle build system to compile and
deploy apps.

Gradle projects have a standard structure. Here are some of the key
files and folders we will be working on, as illustrated below:

Compiled By: Areg. T. 14


Compiled By: Areg. T. 15
Android App – Different XML Files
Android Manifest XML file: Every Android app project must have an
AndroidManifest.xml file (with precisely that name) at the root of the
project source set.

The manifest file describes essential information about your app to


the Android build tools, Android operating system, and Google Play.

Compiled By: Areg. T. 16


Cont…
Layout XML file: A layout resource defines the architecture for the
UI in an Activity or a component of UI.

File location: res/layout/filename.xml, where the filename will be


used as the resource ID.

Resource reference:
In Java: R.layout.filename
In XML: @[package:]layout/filename

Compiled By: Areg. T. 17


Cont…
Strings XML file: A single string that can be referenced from the
application or from other resource files ( such as XML layout).

This xml file is used to replace the Hard-coded strings with a single
string.

This file enhances the reusability of the code.

Compiled By: Areg. T. 18


Cont…
Note: A string is a simple resource that is referenced using the value
provided in the name attribute. So, we can combine string resources
with other simple resources in one XML file, under one <resources>
element.

File location: res/values/filename.xml, where the filename is arbitrary.


The <string> element’s name will be used as the resource ID.

Resource reference:
In Java: R.string.string_name
In XML: @string/string_name

Compiled By: Areg. T. 19


Cont…
Styles XML file: A style resource defines the format and look for a
UI.

A style can be applied to an individual View (from within a layout


file) or to an entire Activity or application (from within the manifest
file).

A style is a simple resource that is referenced using the value


provided in the name attribute. As such, you can combine style
resources with other simple resources in one XML file, under one
<resources> element.
Compiled By: Areg. T. 20
Cont…
File location: res/values/filename.xml, where the filename is arbitrary.
The element’s name will be used as the resource ID.

Resource reference:
In XML: @[package:]style/style_name

Compiled By: Areg. T. 21


Cont…
Color XML file: The color resource is used to define the color value
that we used in our application.

We can use a color resource any place that accepts a hexadecimal


color value.

Note: A color is a simple resource that is referenced using the value


provided in the name attribute. As such, you can combine color
resources with other simple resources in one XML file, under one
<resources> element.
Compiled By: Areg. T. 22
Cont…
File location: res/values/colors.xml, where the filename is arbitrary.
The <color> element’s name will be used as the resource ID.

Resource reference:
In Java: R.color.color_name
In XML: @[package:]color/color_name

Compiled By: Areg. T. 23


Android System Architecture

Figure 1.1: Android System Architecture

Compiled By: Areg. T. 24


Cont…

Compiled By: Areg. T. 25


Cont…
Application Layer: is the very top layer of the above figure are – [web
browser, contact list, games, dialer, camera app, and others].

Developers write code to run on this layer, and users see the screens
created by apps in this layer.

Compiled By: Areg. T. 26


Cont…
Application Programming Interface (API) Layer: Your android
programs make requests to pieces of code in this API layer.

Compiled By: Areg. T. 27


Cont…
Android’s middle Layer: has two parts – a bunch of code written in
the C and C++ programming languages; and the Android Runtime
(ART).

The Android Runtime is a workhorse that runs all your Java/Kotlin


codes.

Compiled By: Areg. T. 28


Cont…
Hardware Abstraction Layer (HAL): is a kind of universal translator.

The ART runs on many different kinds of hardware, but ART isn’t
tailored for any particular kind of hardware. Instead, HAL does.

Compiled By: Areg. T. 29


Cont…
Linux Kernel Layer: the kernel includes a driver, which handles all
communication among running processes.

It manages all the drivers needed during the runtime of an Android device,
such as camera drivers, display drivers, audio drivers, Wi-Fi drivers,
Bluetooth drivers, and memory drivers, among others.

Among the main features of the Linux kernel are as follows:


Security – the Linux kernel maintains the security b/n an app and the host system.
Memory management
Process management etc.

Compiled By: Areg. T. 30


Android App Architecture
As Android apps grow in size, it’s important to define an architecture
that allows the app to scale, increases the app’s robustness, and makes
the app easier to test.

Considering the common architectural principles, each application


should have at least two layers:
The UI layer that displays application data to the screen.
The data layer that contains the business logic of your app and exposes
application data.

Compiled By: Areg. T. 31


Cont…
You can add additional layer called the domain layer to simplify and
reuse the interactions between the UI and data layers.

Figure 1.2: Android Application Architecture

Compiled By: Areg. T. 32


Cont…
The domain layer is optional because not all apps will have these
requirements.

You can use it only when needed – for example, to handle complexity
or favor reusability.

Compiled By: Areg. T. 33


Important terms of Android App
Classical computer science classes often define a program in terms of
functionality and data, and Android applications are NO difference.

They perform tasks, display information to the screen, and act upon
data from a variety of sources.

Compiled By: Areg. T. 34


Cont…
Developing Android applications for mobile devices with limited
resources requires a detailed understanding of the application
lifecycle, and how Android applications function and interact with one
another.

Android uses its own terminology for these application building


blocks – terms such as context, activity, and intent.

Compiled By: Areg. T. 35


Context
Context is an interface to global information about an application
environment.

This is an abstract class whose implementation is provided by the


Android system.

It allows access to application-specific resources and classes, as well


as up-calls for application level operations such as launching activities,
broadcasting and receiving intents, etc.

Compiled By: Areg. T. 36


Cont…
Context is a layer (interface) which stands behind its component
(Activity, Application) and component’s lifecycle, which provides
access to various functionalities which are supported by application
environment and Android framework.

There are mainly two types of context: which are Application context
and Activity context.

Compiled By: Areg. T. 37


Cont…
Application Context:
The function for retrieving an application context is: getApplicationContext().
Returns the context which is linked to application which holds all activities
running inside it.
In application context, our context is tied to application and its lifecycle.
We can think of it as a layer behind the whole application. As long as user
doesn’t kill the application, it’s alive.

Compiled By: Areg. T. 38


Cont…
Activity Context:
The function for retrieving an activity context is: getContext().
Returns the context which is linked to the Activity from which it is called.
In Activity context, the context is tied to an Activity and its lifecycle.
We can imagine context as a layer which stands behind activity and it will live
as long as activity lives. The moment the Activity dies, Context will too.

The “this” keyword:


The this keyword in general sense refers to current instance of a class.
So, when we use this keyword inside an Activity, it refers to that Activity
instance, and helps to get context of that Activity.

Compiled By: Areg. T. 39


Cont…
Context can…
Loading resource.

Launching a new activity.

Creating views.

Obtaining system service.

Compiled By: Areg. T. 40


Intent
An intent is a message sent to the system or another application to
request that an action be performed.

Basically an intent can be used to start an activity, start a service or


send a broadcast.

Starting an activity: An activity represents a single screen in an


application.

Compiled By: Areg. T. 41


Cont…
You can start a new instance of an Activity by passing an Intent to
startActivity(). The Intent describes the activity to start and carries any
necessary data.

Types of Intent:
Explicit Intents:
Specify which application will satisfy the intent, by supplying either the target app’s
package name or a fully-qualified component class name.
Implicit Intents:
Don’t name a specific component, but instead declare a general action to perform, which
allows a component from another app to handle it.

Compiled By: Areg. T. 42


Activity
An activity is a window containing user interface (UI) components
that the user can interact with.

An activity provides the window in which the application draws its UI


(user interface).

Starting an activity often means displaying a window.

An activity is implemented as a subclass of the Activity Java class.


Compiled By: Areg. T. 43
Cont…
The first window that the application creates is called the main
activity and serves as the entry point to the application.

An Android application may contain multiple activities and you


specify the main activity by declaring it in the application manifest
file.

Compiled By: Areg. T. 44


Activity Lifecycle
Over a course of its lifetime, an activity goes through a number of
states.

To navigate transitions between stages of the activity lifecycle, the


Activity class provides a core set of 7 callback methods like
onCreate(), onStart(), onPause(), onRestart(), onResume(), onStop(),
and onDestroy() to handle transitions between states.

The system invokes each of these callbacks as an activity enters a new


state.

Compiled By: Areg. T. 45


Cont…

Figure 1.3: Activity Lifecycle

Compiled By: Areg. T. 46


Cont…
Depending on the complexity of your activity, you probably don’t
need to implement all the lifecycle methods.

However, it’s important that you understand each one and implement
these that ensure your app behaves the way users expect.

In general, the movement through an activity’s lifecycle looks like in


the table below:

Compiled By: Areg. T. 47


Cont…
Method Description Next
onCreate() o Called when the activity if first created. onStart()
o This is where you should do all of your normal static set up: create views,
bind data to lists, etc.
o This method also provides you with a Bundle containing the activity’s
previously frozen state, if there was one.
o Always followed by onStart().
onStart() o Called when the activity is becoming visible to the user. onResume()
o Followed by onResume() if the activity comes to the foreground, or onStop()
onStop() if it becomes hidden.
onResume() o Called when the activity will start interacting with the user. onPause()
o At this point your activity is at the top of its activity stack, with user input
going to it.
o Always followed by onPause().

Compiled By: Areg. T. 48


Cont…
Method Description Next
onPause() o Called when the activity loses foreground state, is no longer focusable or onResume()
before transition to stopped/hidden or destroyed state. onStope()
o The activity is still visible to user, so it’s recommended to keep it visually
active and continue updating the UI.
o Implementations of this method must be very quick because the next
activity will not be resumed until this method returns.
o Followed by either onResume() if the activity returns back to the front, or
onStop() if it becomes invisible to the user.
onStop() o Called when the activity is no longer visible to the user. onRestart()
o This may happen either because a new activity is being started on top, an onDestroy()
existing one is being brought in front of this one, or this one is being
destroyed.
o This is typically used to stop animations and refreshing the UI, etc.

Compiled By: Areg. T. 49


Cont…
Method Description Next
o Followed by either onRestart() if this activity is coming back to interact
with the user, or onDestroy() if this activity is going away.
onRestart() o Called after your activity has been stopped, prior to it being started again. onStart()
o Always followed by onStart().
onDestroy() o The final call you receive before your activity is destroyed. Nothing
o This can happen either because the activity is finishing, or because the
system is temporarily destroying this instance of the activity to save space.
o You can distinguish between these two scenarios with the isFinishing()
method.

Compiled By: Areg. T. 50


UI Components
Android offers a sophisticated and powerful componentized model for
building your UI, based on the fundamental layout classes: View and
ViewGroup.

To start with, the platform includes a variety of prebuilt View and


ViewGroup subclasses – called widgets and layouts, respectively –
that you can use to construct your UI.

In android UI components are the interactive or View components


that are used to design the user interface of an application.

Compiled By: Areg. T. 51


Cont…
The figure below shows the pictorial representation of some popular
ones of UI components in android application.

Compiled By: Areg. T. 52


Cont…
1. EditText:
A user interface element for entering and modifying text.
When you define an edit text widget, you must specify the input type
attribute.
For example, for plain text input set inputType to “text”.
2. SeekBar:
A SeekBar is an extension of ProgressBar that adds a draggable thumb.
The user can touch the thumb and drag left to right to set the current progress
level or use the arrow keys.

Compiled By: Areg. T. 53


Cont…
3. CheckBox:
A checkbox is a specific type of two-states button that can be either checked
or unchecked.
Checkboxes allow the user to select one or more options from a set.
4. RadioGroup of RadioButton:
A radio button is a two-states button that can be either checked or unchecked.
When the radio button is unchecked, the user can press or click it to check it.
However, contrary to a check box, a radio button cannot be unchecked by the
user once checked.
Radio buttons are normally used together in a radio group.

Compiled By: Areg. T. 54


Cont…
5. Switch:
A switch is a two-state toggle widget.
Users can drag the switch “thumb” back and forth to select either of two
options or simply tap the switch to toggle between options.
6. Spinner:
A view that displays one child at a time and lets the user pick among them.
In the default state, a spinner shows its currently selected value. Touching the
spinner displays a dropdown menu with all other available values, from which
the user can select a new one.

Compiled By: Areg. T. 55


Cont…
7. Button:
A button is a user interface element that the user can tap or click to perform an
action.
To specify an action when the button is pressed, set a click listener on the
button object in the corresponding activity code:

8. TextView:
A text view is a user interface element that displays text to the user.

Compiled By: Areg. T. 56


Cont…
Generally, in android the user interface of an app is made with a
collection of View and ViewGroup objects.

The View is a base class for all UI components in android and it is


used to create interactive UI components such as TextView, EditText,
Checkbox, RadioButton, etc. and it is responsible for event handling
and drawing.

The ViewGroup is a subclass of View and it will act as a base class


for layouts and layout parameters.

Compiled By: Areg. T. 57


Cont…
The ViewGroup will provide invisible containers to hold other Views
or ViewGroups and to define the layout properties.

In android, we can define a UI components in two ways, those are:


Declare UI elements in XML
Create UI elements at runtime

The android framework will allow us to use either or both of these


methods to define our application’s UI.

Compiled By: Areg. T. 58


Layouts
Layouts are important because they directly affect the look and feel of
your application.

Technically, a layout is a view that arranges child views added to it.

Android comes with a number of build-in layouts, ranging from


Linear layout, which is the easiest to use, to Constraint layout, which
is the most powerful.

Compiled By: Areg. T. 59


Cont…
An important Android component, a layout defines the visual
structure of your UI components.

A layout is a subclass of android.view.ViewGroup, which in turn


derives from android.view.View.

A ViewGroup is a special view that can contain other views.

A layout can be declared in a layout file or added programmatically at


runtime.
Compiled By: Areg. T. 60
Cont…
The following are some of the layouts in Android.
Linear layout
Relative layout
Constraint layout

Compiled By: Areg. T. 61


Cont…
Linear Layout: is a layout that arranges its children either horizontally
or vertically, depending on the value of its orientation property.

The linear layout is the easiest layout to use.

The layout in the figure below, is an examples of linear layout with


horizontal orientation and vertical orientation respectively. It contains
three children, an ImageButton, a TextView and a Button.

Compiled By: Areg. T. 62


Cont…

Compiled By: Areg. T. 63


Cont…

Compiled By: Areg. T. 64


Cont…
Note that each view in a layout can have a layout_gravity attribute to
determine its position within its axis. For instance, setting the
layout_gravity attribute to center will center it.

A linear layout can also have a gravity attribute that affects its gravity.

Compiled By: Areg. T. 65


Cont…
Relative Layout: A layout where the positions of the children can be
described in relation to each other or to the parent.

Relative layout is a view group that displays child views in relative


positions.

The position of each view can be specified as relative to sibling


elements (such as to the left-of or below another view) or in positions
relative to the parent relative layout area (such as aligned to the
bottom, left or center).

Compiled By: Areg. T. 66


Cont…
Relative layout lets child views specify their position relative to the
parent view or to each other (specified by ID).

So 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.

Compiled By: Areg. T. 67


Cont…
Some of the many layout properties available to views in a relative
layout include:
android:layout_alignParentTop
If “true”, makes the top edge of this view match the top edge of the parent.

android:layout_centerVertical
If “true”, centers this child vertically within its parent.

Compiled By: Areg. T. 68


Cont…
android:layout_below
Positions the top edge of this view below the view specified with a resource
ID.

android:layout_toRightOf
Positions the left edge of this view to the right of the view specified with a
resource ID.

Compiled By: Areg. T. 69


Cont…
Constraint Layout: allows you to create large and complex layouts
with a flat view hierarchy (no nested view groups).

It’s similar to Relative Layout in that all views are laid out according
to relationships between sibling views and the parent layout, but it’s
more flexible than Relative Layout and easier to use with Android
studio’s layout editor.

Compiled By: Areg. T. 70


Cont…
All the power of Constraint Layout is available directly from the
layout editor’s visual tools, because the layout API and the layout
editor were specially built for each other.

So you can build your layout with Constraint layout entirely by drag-
and-dropping instead of editing the XML.

Compiled By: Areg. T. 71


Cont…
To define a view’s position in constraint layout, you must add at least
one horizontal and one vertical constraint for the view.

Each constraint represents a connection or alignment to another view,


the parent layout, or an invisible guideline.

Each constraint defines the view’s position along either the vertical or
horizontal axis; so each view must have a minimum of one constraint
for each axis, but often more are necessary.

Compiled By: Areg. T. 72


R.java File
R.java file is an auto-generated file by aapt (Android Asset Packaging
Tool) that contains resource IDs for all the resources of res/ directory.

When you create any component in the xml file, id for the
corresponding component is automatically created in this file.

The id can be used in the activity source file to perform any action on
the component.

Compiled By: Areg. T. 73

You might also like