06 - It-407 - Mobile Application Develeopment
06 - It-407 - Mobile Application Develeopment
University Of Gujrat
Faculty of Computing & IT
Learning Students will be familiar with the process by which new applications are created for
Outcomes the Android operating system. Applications are usually developed in Java programming language using
the Android software development kit (SDK), but other development environments are also available.
Upon successful completion of this class, the learner will be able to:
Use the development tools in the Android development environment
Use the major components of Android API set to develop their own apps
Describe the life cycles of Activities, Applications and Fragments
Use the Java programming language to build Android apps
Make UI-rich apps using all the major UI components
Know UI best-practices
Be familiar with new UI components like Fragments and the Action Bar
Store and manipulate data using Content Providers, Shared Preferences and Notifications
Do background processing with Services and Async Tasks
Link mobile app with server data using web services
Publish the application on google play store
Reference
Material 1) Wei-Menge Lee, Beginning Android 4 Application Development, John Wiley & Sons, 2012
2) Grant Allen, Beginning Android 4,Apress, 2011, ISBN: 1430239840. 3.
3) Neil Smyth, Android Studio Development Essentials, 2015
Prerequisites
Android programming is based on Java programming language so if you have
basic understanding on Java programming then it will be a fun to learn
Android application development.
The smaller screen sizes, decreased storage space, reduced memory, lower
computing power and unreliable network connections aren't the only things that
make mobile application development different than developing traditional Web
applications. The differences are largely driven by users experiencing variations
in the conditions in which they interact with the app. While most enterprise
applications are accessed from a desk in the office, mobile enterprise
applications are more frequently accessed out in the field, where workers are
focused on completing one particular task and moving on.
Xamarin
Corona SDK
appcelerator titanium
PhoneGap
The Android SDK includes an Android device emulator — a virtual device that runs
on your computer. The Android Emulator lets you develop and test Android apps without using a
physical device.
Linux Kernel
At the bottom of the layers is Linux - Linux 3.6 with approximately 115
patches. This provides a level of abstraction between the device hardware and
it contains all the essential hardware drivers like camera, keypad, display etc.
Also, the kernel handles all the things that Linux is really good at such as
networking and a vast array of device drivers, which take the pain out of
interfacing to peripheral hardware.
1. Security:
The Linux kernel handles the security between the application and the system.
2.Memory Management:
It manages the process well, allocates resources to processes whenever they need
them.
4.Network Stack:
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:
The Android runtime consist of core libraries of Java and ART(the Android RunTime).
Older versions of Android (4.x and earlier) had Dalvik runtime.
This open source web browser engine provides all the functionality to display web
content and to simplify page loading.
4. Media frameworks:
These libraries allow you to play and record audio and video.
ART and Dalvik are compatible runtimes running Dex bytecode, so apps developed for
Dalvik should work when running with ART. However, some techniques that work on Dalvik
do not work on ART.
ART Features
Application Framework
An application framework is a software library that provides a fundamental structure to
support the development of applications for a specific environment. An application
framework acts as the skeletal support to build an application.
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.
Package Manager – The system by which applications are able to find out
information about other applications currently installed on the device.
Telephony Manager – Provides information to the application about the telephony
services available on the device such as status and subscriber information.
Applications
Located at the top of the Android software stack are the applications. These comprise
both the native applications provided with the particular Android implementation (for
example web browser and email applications) and the third party applications installed
by the user after purchasing the device.
← prev
The Dalvik Virtual Machine (DVM) is an android virtual machine optimized for mobile devices.
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 clas
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 pla
The Android Assets Packaging Tool (aapt) handles the packaging process.
Although JIT and AOT use the same compiler with a similar set of optimizations, the
generated code might not be identical. JIT makes use of runtime type information, can do
better inlining, and makes on stack replacement (OSR) compilation possible, all of which
generates slightly different code.
The user runs the app, which then triggers ART to load the .dex file.
If the .oat file (the AOT binary for the .dex file) is available, ART uses it directly.
Although .oatfiles are generated regularly, they don't always contain compiled code (AOT
binary).
If no .oat file is available, ART runs through JIT or an interpreter to execute the .dex file.
ART always uses the .oat files if available. Otherwise, it uses the APK and extracts it in
memory to get to the .dex; this incurs a big memory overhead that is equal to the size of
the dex files.
Advertisements
Previous Page
Next Page
An activity represents a single screen with a user interface just like window or frame of
Java.Android activity is the subclass of ContextThemeWrapper 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 similar
way, Android system initiates its program with in an Activity starting with a
call on onCreate() callback method. There is a sequence of callback methods
that start up an activity and a sequence of callback methods that tear down an
activity as shown in the below Activity life cycle diagram: (image courtesy :
android.com )
The Activity class defines the following call backs i.e. events. You don't need to
implement all the callbacks methods. However, it's important that you
understand each one and implement those that ensure your app behaves the
way users expect.
onCreate()
1
This is the first callback and called
when the activity is first created.
onStart()
onResume()
onPause()
onStop()
onDestroy()
7 onRestart()
APK File
Resources
Manifest file
Example
This example will take you through simple steps to show Android application
activity life cycle. Follow the following steps to modify the Android application
we created in Hello World Example chapter −
Step Description
1 You will use Android studio to create an Android application and name it
as HelloWorld under a package com.example.helloworld as explained in
the Hello World Example chapter.
3 Run the application to launch Android emulator and verify the result of the
changes done in the application.
Following is the content of the modified main activity
file src/com.example.helloworld/MainActivity.java. This file includes
each of the fundamental life cycle methods. The Log.d() method has been
used to generate log messages −
package com.example.helloworld;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
@Override
super.onStart();
@Override
protected void onResume() {
super.onResume();
@Override
super.onPause();
@Override
super.onStop();
@Override
super.onDestroy();
An activity class loads all the UI component using the XML file available
in res/layout folder of the project. Following statement loads UI components
from res/layout/activity_main.xml file:
setContentView(R.layout.activity_main);
An application can have one or more activities without any restrictions. Every
activity you define for your application must be declared in
your AndroidManifest.xml file and the main activity for your app must be
declared in the manifest with an <intent-filter> that includes the MAIN action
and LAUNCHER category as follows:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tutorialspoint7.myapplication">
<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>
</intent-filter>
</activity>
</application>
</manifest>
If either the MAIN action or LAUNCHER category are not declared for one of
your activities, then your app icon will not appear in the Home screen's list of
apps.
Let's try to run our modified Hello World! application we just modified. I
assume you had created your AVD while doing environment setup. To run the
app from Android studio, open one of your project's activity files and click
Run icon from the toolbar. Android studio installs the app on your AVD and
starts it and if everything is fine with your setup and application, it will display
Emulator window and you should see following log messages
in LogCatwindow in Android studio −
08-23 10:32:07.682 4480-4480/com.example.helloworld D/Android :: The onCreate() event
08-23 10:32:07.683 4480-4480/com.example.helloworld D/Android :: The onStart() event
08-23 10:32:07.685 4480-4480/com.example.helloworld D/Android :: The onResume() event
Let us try to click lock screen button on the Android emulator and it will
generate following events messages in LogCat window in android studio:
08-23 10:32:53.230 4480-4480/com.example.helloworld D/Android :: The onPause() event
08-23 10:32:53.294 4480-4480/com.example.helloworld D/Android :: The onStop() event
Let us again try to unlock your screen on the Android emulator and it will
generate following events messages in LogCat window in Android studio:
08-23 10:34:41.390 4480-4480/com.example.helloworld D/Android :: The onStart() event
08-23 10:34:41.392 4480-4480/com.example.helloworld D/Android :: The onResume() event
Next, let us again try to click Back button on the Android emulator and it
will generate following events messages in LogCat window in Android studio
and this completes the Activity Life Cycle for an Android Application.
08-23 10:37:24.806 4480-4480/com.example.helloworld D/Android :: The onPause() event
08-23 10:37:25.668 4480-4480/com.example.helloworld D/Android :: The onStop() event
08-23 10:37:25.669 4480-4480/com.example.helloworld D/Android :: The onDestroy() event
Android - UI Layouts
Advertisements
Previous Page
Next Page
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.
LAYOUT PARAMS
This tutorial is more about creating your GUI based on layouts defined in XML
file. A layout may contain any type of widgets such as buttons, labels,
textboxes, and so on. Following is a simple example of XML file having
LinearLayout −
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
</LinearLayout>
Once your layout has created, you can load the layout resource from your
application code, in your Activity.onCreate() callback implementation as shown
below −
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
1 Linear Layout
2 Relative Layout
RelativeLayout is a view group that displays child views in relative
positions.
3 Table Layout
TableLayout is a view that groups views into rows and columns.
4 Absolute Layout
AbsoluteLayout enables you to specify the exact location of its children.
5 Frame Layout
The FrameLayout is a placeholder on screen that you can use to display a
single view.
6 List View
ListView is a view group that displays a list of scrollable items.
7 Grid View
GridView is a ViewGroup that displays items in a two-dimensional,
scrollable grid.
Layout Attributes
Each layout has a set of attributes which define the visual properties of that
layout. There are few common attributes among all the layouts and their are
other attributes which are specific to that layout. Following are common
attributes and will be applied to all the layouts:
1 android:id
2 android:layout_width
3 android:layout_height
5 android:layout_marginBottom
6
android:layout_marginLeft
7 android:layout_marginRight
8 android:layout_gravity
9 android:layout_weight
This specifies how much of the extra space in the layout should be
allocated to the View.
10
android:layout_x
11 android:layout_y
12 android:layout_width
13 android:layout_width
14 android:paddingLeft
This is the left padding filled for the layout.
15 android:paddingRight
16 android:paddingTop
17 android:paddingBottom
Here width and height are the dimension of the layout/view which can be
specified in terms of dp (Density-independent Pixels), sp ( Scale-independent
Pixels), pt ( Points which is 1/72 of an inch), px( Pixels), mm ( Millimeters)
and finally in (inches).
You can specify width and height with exact measurements but more often,
you will use one of these constants to set the width or height −
Gravity attribute plays important role in positioning the view object and it can
take one or more (separated by '|') of the following constant values.
clip_vertical 0x80 Additional option that can be set to have the top
and/or bottom edges of the child clipped to its
container's bounds. The clip will be based on the
vertical gravity: a top gravity will clip the
bottom edge, a bottom gravity will clip the top
edge, and neither will clip both edges.
clip_horizontal 0x08 Additional option that can be set to have the left
and/or right edges of the child clipped to its
container's bounds. The clip will be based on the
horizontal gravity: a left gravity will clip the
right edge, a right gravity will clip the left edge,
and neither will clip both edges.
View Identification
A view object may have a unique ID assigned to it which will identify the View
uniquely within the tree. The syntax for an ID, inside an XML tag is −
android:id="@+id/my_button"
The at-symbol (@) at the beginning of the string indicates that the XML parser
should parse and expand the rest of the ID string and identify it as an ID resource.
The plus-symbol (+) means that this is a new resource name that must be created
and added to our resources. To create an instance of the view object and capture it
from the layout, use the following −
Input controls are the interactive components in your app's user interface. Android
provides a wide variety of controls you can use in your UI, such as buttons, text
fields, seek bars, check box, zoom buttons, toggle buttons, and many more.
UI ELEMENTS
A View is an object that draws something on the screen that the user
can interact with and a ViewGroup is an object that holds other View
(and ViewGroup) objects in order to define the layout of the user
interface.
You define your layout in an XML file which offers a human-readable
structure for the layout, similar to HTML. For example, a simple vertical
layout with a text view and a button looks like this −
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
</LinearLayout>
Android UI Controls
There are number of UI controls provided by Android that allow you to
build the graphical user interface for your app.
1 TextView
2 EditText
EditText is a predefined subclass of TextView that includes rich editing
capabilities.
3 AutoCompleteTextView
The AutoCompleteTextView is a view that is similar to EditText, except
that it shows a list of completion suggestions automatically while the
user is typing.
4 Button
A push-button that can be pressed, or clicked, by the user to perform an
action.
5 ImageButton
An ImageButton is an AbsoluteLayout which enables you to specify the
exact location of its children. This shows a button with an image (instead
of text) that can be pressed or clicked by the user.
6 CheckBox
An on/off switch that can be toggled by the user. You should use check
box when presenting users with a group of selectable options that are
not mutually exclusive.
7 ToggleButton
An on/off button with a light indicator.
8 RadioButton
The RadioButton has two states: either checked or unchecked.
9 RadioGroup
A RadioGroup is used to group together one or more RadioButtons.
10 ProgressBar
The ProgressBar view provides visual feedback about some ongoing
tasks, such as when you are performing a task in the background.
11 Spinner
A drop-down list that allows users to select one value from a set.
12 TimePicker
The TimePicker view enables users to select a time of the day, in either
24-hour mode or AM/PM mode.
13 DatePicker
The DatePicker view enables users to select a date of the day.
Create UI Controls
Input controls are the interactive components in your app's user
interface. Android provides a wide variety of controls you can use in your
UI, such as buttons, text fields, seek bars, check box, zoom buttons,
toggle buttons, and many more.
As explained in previous chapter, a view object may have a unique ID
assigned to it which will identify the View uniquely within the tree. The
syntax for an ID, inside an XML tag is −
android:id="@+id/text_id"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="@+id/text_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
</LinearLayout>
Then finally create an instance of the Control object and capture it from
the layout, use the following −