MAD _Unit_1_Notes Introduction to Android
MAD _Unit_1_Notes Introduction to Android
Mobile Programming is the process of making software for smartphones and digital
assistants, most commonly for Android and iOS.
The software can be preinstalled on the device, downloaded from a mobile app store or
accessed through a mobile web browser.
The programming and markup languages used for this kind of software development include
Java, Kotlin, Swift, C# and HTML5, CSS, XML and JavaScript etc.
Before going into details let us discuss why mobile programming and development is
important now a day.
According to recent data and survey an average smartphone user spent 3 hours and 15
minutes on a smartphone every day. This is longer than the time spent watching TV, or
engaging with any other media.
About 90% of this time was spent in apps. Clearly, mobile apps are the biggest medium your
customers are engaging with digitally.
Smartphone users have an average of 80 apps installed on their phone and use at least 40 of
them every month.
A mobile app is the best way to either build a business from scratch, or increase your existing
business and give it a digital edge
The evolution of mobile application development technology with new devices made our
lives much easier.
Introduction to Android
Android is an OS for mobile devices such as smart phones, tablet computers etc.
Google released most of the Android code under the Apache License, a free software license.
Android, Inc. was founded in Palo Alto, California, United States in October, 2003 by Andy
Rubin, Rich Miner, Nick Sears and Chris White.
Google acquired Android Inc. on August 17, 2005, making Android Inc. a wholly owned
subsidiary of Google Inc.
Following Tables shows the Android and other operating system Market Shares.
Android 70.89
iOS 28.36
Samsung 0.38
Kai OS 0.18
Other 0.02
With 70.89 Market Share in 2023 Android is becoming the dominating operating system in
smart phone world.
Android Features
Media support for common audio, video, and still image formats (MPEG4, H.264, MP3,
AAC, AMR, JPG, PNG, GIF)
GSM(Global System for Mobile Communication) Telephony (hardware dependent)
Bluetooth, EDGE(Enhance Data Rate for GSM Evolution), 3G, and WiFi (hardware
dependent)
Camera, GPS, compass, and accelerometer (hardware dependent)
Rich development environment including a device emulator, tools for debugging.
ANDROID ARCHITECTURE
The Android Architecture generally consists of a Linux kernel and a collection of C/C++
libraries that is an application framework that provides services, and management of the
applications and run time.
Linux Kernel
Android was created on the open source kernel of Linux. One main reason for choosing
this kernel was that it provided core features on which to develop the Android
operating system. The features of Linux kernel are:
1. Security:
The Linux kernel handles the security between the application and the system.
2. Memory Management:
It efficiently handles the memory management
3. Process Management:
It manages the process well, allocates resources to processes whenever they need
them.
4. Network Stack:
It effectively handles the network communication.
5. Driver Model:
It ensures that the application works. Hardware manufacturers can build their drivers
into the Linux build.
Libraries:
Running on the top of the kernel, the Android framework was developed with various
features. It consists of various C/C++ core libraries with numerous of open source tools. Some
of these are:
Media frameworks:
These libraries allow you to play and record audio and video.
Secure Socket Layer (SSL):
These libraries are there for Internet security.
Among all the components Linux Kernel provides main functionality of operating system
functions to smartphones and Dalvik Virtual Machine (DVM) provide platform for running an
android application.
Application framework –
Application Framework provides several important classes which are used to create an
Android application. It provides a generic abstraction for hardware access and also helps in
managing the user interface with application resources. Generally, it provides the services
with the help of which we can create a particular class and make that class helpful for the
Applications creation.
It includes different types of services activity manager, notification manager, view system,
package manager etc. which are helpful for the development of our application according to
the prerequisite.
Application runtime –
Android Runtime environment is one of the most important part of Android. It contains
components like core libraries and the Dalvik virtual machine (DVM). Mainly, it provides the
base for the application framework and powers our application with the help of the core
libraries.
Platform libraries –
The Platform Libraries includes various C/C++ core libraries and Java based libraries such as
Media, Graphics, Surface Manager, OpenGL etc. to provide a support for android
development.
Media library provides support to play and record an audio and video formats.
Surface manager responsible for managing access to the display subsystem.
SGL and OpenGL both cross-language, cross-platform application program
interface (API) are used for 2D and 3D computer graphics.
SQLite provides database support and Free Type provides font
support.
Web-Kit This open source web browser engine provides all the functionality to
display web content and to simplify page loading.
SSL (Secure Sockets Layer) is security technology to establish an encrypted link
between a web server and a web browser.
An android component is simply 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.
1. Activity
activities set the layout and display the output and also respond to the user’s actions. An
activity is implemented as a subclass of class Activity.
public class MainActivity extends Activity {
}
2. Services
3. View/Widgets
A view is the UI element such as button, label, text field etc. Anything that you see is a view.
4. Intents
6. Fragments
Fragments are like parts of activity. An activity can display one or more fragments on the
screen at the same time.
7. Broadcast Receivers
8. Notifications
Notifications are the application alerts that are used to draw the user’s attention to some
particular app event without stealing focus or interrupting the current activity of the user.
They are generally used to grab user’s attention when the application is not visible or
active,
Examples: E-mail popups, Messenger popups, etc.
ACTIVITY LIFE CYCLE:
An activity represents a single screen with a user interface just like window or frame.
Activity is the subclass of AppCompactActivity class.
If you have worked with C, C++ or Java programming language then you must have seen
that your program starts from main() function.
Very similarway, Android system initiates its programwith
in an Activity starting with a call on onCreate() method.
Activity class is one of the very important parts of the
Android component.
Any app, don’t matter how much small it is (in terms of code and scalability), it has at least
one Activity class.
So it can be said that an activity is the entry point for interacting with the user.
Every activity contains the layout, which has a user interface to interact with the user.
As we know that every activity contains a layout associated with it, so it can be said that
activity class is the gateway, through which a user can interact programmatically with UI.
Layout for a particular activity is set with the help of setContentView().
setContentView() is a function that takes View as a parameter.
As a user navigates through, out of, and back to your app, the Activity
instances in your app transition through different states in their lifecycle.
The Activity class provides a number of functions that allow the activity to know that a
state has changed: that the system is creating, stopping, or resuming an activity, or
destroying the process in which the activity resides.
Method Description
@Override
super.onCreate(savedInstanceState);
}
onStart called when activity is becoming visible to the user.
@Override
super.onStart();
onResume called when activity will start interacting with the user.
@Override
super.onResume();
@Override
super.onPause();
@Override
super.onStop();
@Override
@Override
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.
DVM features:
Register-based architecture. The data structure with methods placed there is based on
processor registers. Instructions in register VM are executed faster than similar ones in stack VM
Executes the bytecode of its format. Android dexer converts .class files into .dex format
optimized for DVM execution. Unlike a .class file, a .dex file contains several classes at once.
ART Features.
Ahead-of-time (AOT) compilation
ART introduces ahead-of-time (AOT) compilation, which can improve app performance.
ART also has tighter install-time verification than Dalvik.
Improved garbage collection
Garbage collection (GC) is very resource intensive, which can impair an app's
performance, resulting in choppy display, poor UI responsiveness, and other problems.
ART improves garbage collection in several ways
Development and debugging improvements
ART offers a number of features to improve app development and debugging.
Prepared By: Mr. D. R. Somwanshi (M.Sc.CS, M.Phil, CS) COCSIT Latur. 11
Notes: Mobile Application Development Class: BCA TY, Unit I: Introduction to Android
ART supports a number of new debugging options, particularly in monitor- and garbage
collection-related functionality
1.4 ANDROID APPLICATION STRUCTURE, DEVICE SCREEN SIZE COMPATIBILITY
ANDROID APPLICATION STRUCTURE
Android Application structure is made up of several important files and folder that are
required for creating android applications.
All of the files marked in the above image are explained below in brief:
is an important part of our app because it defines the structure and metadata of our
application, itscomponents, and its requirements.
This file includes nodes for each of the Activities, Services, Content Providers and
Broadcast Receiver that make the application and using Intent Filters and Permissions,
determines how they co-ordinate with each other and other applications.
A typical AndroidManifest.xml file looks like:
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 inthe UI layout. It works on the backend of an
Android application.
3. drawable: A Drawable folder contains resource type file (something that can be
drawn). Drawables may take a variety of file like Bitmap (PNG, JPEG), Nine Patch, Vector
(XML), Shape, Layers, States, Levels, and Scale.
4. layout: A layout defines the visual structure for a user interface, suchas the UI for
an Android application. This folder stores Layout files that are written in XML language.
You can add additional layout objects or widgets as child elements to gradually build a View
hierarchy that defines your layout file.
Below is a sample layout file:
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a TextView" />
</LinearLayout>
5. mipmap: Mipmap folder contains the Image Asset file that can be used
in Android Studio application. You can generate the following 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.
Below is a sample colors.xml file:
<resources>
<string name="app_name">MyApp</string>
</resources>
8. styles.xml: The styles.xml file contains resources of the theme style in
the Android application. This file is written in XML language.Below is a sample
styles.xml file:
<resources>
apply plugin:
'com.android.application'android {
compileSdkVersion
26defaultConfig {
applicationId "com.example.geeksforgeeks.geeksforgeeks"minSdkVersion
16
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner
"android.support.test.runner.AndroidJUnitRunner"
}
buildTy
pes {
release
{
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar']) implementation
'com.android.support:appcompat-v7:26.1.0'
implementation
'com.android.support.constraint:cons
traint-layout:1.0.2'
}
Android devices come in a variety of screen sizes and resolutions. That’s why
handling the multiple screen size in android is most important.
Prepared By: Mr. D. R. Somwanshi (M.Sc.CS, M.Phil, CS) COCSIT Latur Page 15
Notes: Mobile Application Development, Class: BCATY, Unit I: Introduction to Android
Actual physical size, measured as the screen’s diagonal. For simplicity, Android
groups has four generalized sizes: small, normal, large, and extra large. To set up
support for multiple device sizes in Android, add the element into the
AndroidManifest.xml file.
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens= "true"
android:anyDensity="true"
/>
<application... >
Support for different screen sizes enables the greatest number of users and widest
variety of devices to access your app.
To support as many screen sizes as possible, design your app layouts to be responsive
and adaptive
Window size classes in android categorize the display area available to your app
as compact, medium, or expanded. Available width and height are classified separately
Prepared By: Mr. D. R. Somwanshi (M.Sc.CS, M.Phil, CS) COCSIT Latur Page 16
Notes: Mobile Application Development, Class: BCATY, Unit I: Introduction to Android
wrap_content — Enables the view to set its size to whatever is necessary to fit the
content contained in the view.
match_parent — Enables the view to expand as much as possible within the parent
view.
0dp (match constraint) — In a ConstraintLayout, similar to match_parent. Enables
the view to take all the available space within the view's constraints.
ANDROID EMULATOR
The Android emulator is an Android Virtual Device (AVD), which represents a specific
Android device. We can use the Android emulator as a target device to execute and test our
Android application on our PC.
The Android emulator provides almost all the functionality of a real device. We can get the
incoming phone calls and text messages. It also gives the location of the device and
simulates different network speeds.
Android emulator simulates rotation and other hardware sensors. It accesses the Google
Play store, and much more
Prepared By: Mr. D. R. Somwanshi (M.Sc.CS, M.Phil, CS) COCSIT Latur Page 17
Notes: Mobile Application Development, Class: BCATY, Unit I: Introduction to Android
Testing Android applications on emulator are sometimes faster and easier than doing on a
real device. For example, we can transfer data faster to the emulator than to a real device
connected through USB.
The Android emulator comes with predefined configurations for several Android phones,
Wear OS, tablet, Android TV devices.
The Android emulator takes additional requirements beyond the basic system
requirement for Android Studio. These requirements are given below:
The Android emulator is installed while installing the Android Studio. However some
components of emulator may or may not be installed while installing Android Studio. To
install the emulator component, select the Android Emulator component in the SDK
Tools tab of the SDK Manager.
We can run an Android app form the Android Studio project, or we can run an app which
is installed on the Android Emulator as we run any app on a device.
1. In Android Studio, we need to create an Android Virtual Device (AVD) that the emulator
can use to install and run your app. To create a new AVD:-
3. Select the system image for the particular API level and click Next.
Prepared By: Mr. D. R. Somwanshi (M.Sc.CS, M.Phil, CS) COCSIT Latur Page 18
Notes: Mobile Application Development, Class: BCATY, Unit I: Introduction to Android
5. In the toolbar, choose the AVD, which we want to run our app from the target device
from the drop-down menu.
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.paad.myapp"
android:versionCode="1"
android:versionName="0.9 Beta"
android:installLocation="preferExtern
al">[....manifest nodes ]
</manifest>
Prepared By: Mr. D. R. Somwanshi (M.Sc.CS, M.Phil, CS) COCSIT Latur Page 19
Notes: Mobile Application Development, Class: BCATY, Unit I: Introduction to Android
A manifest file includes the nodes that define the application components,
security settings, test classes and requirements that make up the application.
Some of the manifest sub-node tags that are mainly used are:
uses-sdk– It is used to define a minimum and maximum SDK version that must
be available on a device so that our application function properly, and target SDK
for which it has been designed using a combination of minSdkVersion,
maxSdkVersion, and targetSdkVersion attributes, respectively.
<uses-sdk android:minSdkVersion=”6”
android:targetSdkVersion=”15”/>
<uses-configuration android:reqTouchScreen=”finger”
android:reqNavigation=”trackball”
android:reqHardKeyboard=”true”
android:reqKeyboardType=”qwerty”/>
<uses-configuration android:reqTouchScreen=”finger”
android:reqNavigation=”trackball”
android:reqHardKeyboard=”true”
android:reqKeyboardType=”twelvekey”/>
<uses-feature android:name=”android.hardware.nfc”/>
<supports-screens android:smallScreens=”false”
android:normalScreens=”true”
android:largeScreens=”true”
android:xlargeScreens=”true”/>
Prepared By: Mr. D. R. Somwanshi (M.Sc.CS, M.Phil, CS) COCSIT Latur Page 20
Notes: Mobile Application Development, Class: BCATY, Unit I: Introduction to Android
<uses-permission
android:name="android.permission.INTERNET"></uses-
permission>
application– A manifest can contain only one application node. It uses attributes
to specify the metadata for your application (including its title, icon and theme).
During development we should include a debuggable attribute set to true to
enable debugging, then be sure to disable it for your release builds.
The application node also acts as a container for the Activity, Service, Content
Provider, and Broadcast Receiver nodes that specify theapplication components.
The name of our custom application class can be specified using the
android:name attribute.
<application android:icon="@drawable/icon"
android:logo-"@drawable/logo" android:theme="@android:style/Theme.Light"
android:name=".MyApplicationClass" android:debuggable="true">
[....application nodes. ............................................. ]
</application>
Prepared By: Mr. D. R. Somwanshi (M.Sc.CS, M.Phil, CS) COCSIT Latur Page 21