Mobile Application Developemnt Introduction
Mobile Application Developemnt Introduction
ON
MOBILE APPLICATION DEVELOPMENT
1
MALLA REDDY UNIVERSITY
Course Outcomes
Analyze architecture of android and current trends in mobile operating systems.
Apply suitable software tools and APIs for the development User Interface of a
particularmobile application.
Apply intents and broadcast receivers in android application.
Develop and design apps for mobile devices using SQLiteDatabase.
UNIT-I
Introduction to Android Operating System: Android OS and Features – Android development
framework;
Installing and running applications on Android Studio, Creating AVDs, Types of Android
application; Creating Activities, Activity Life Cycle, Activity states, monitoring state changes;
UNIT- II
Android application components – Android Manifest file, Externalizing recourses like Simple
Values, Drawables, Layouts, Menus, etc,
Building User Interfaces: Fundamental Android UI design, Layouts – Linear, Relative, Grid and
Table Layouts. User Interface (UI) Components
UNIT-III
Fragments – Creating fragments, Lifecycle of fragments, Fragment states, Adding fragments to
Activity, adding, removing and replacing fragments with fragment transactions, interfacing
between fragments and Activities,
UNIT-IV
Intents and Broadcasts: Using intents to launch Activities, Types of Intents, Passing data to Intents,
Getting results from Activities, Broadcast Receivers – Using Intent filters to service implicit
Intents, Resolving Intent filters;
UNIT-V
Database: Introduction to SQLite database, creating and opening a database, creating tables, inserting
retrieving and deleting data;
TEXT BOOKS:
1. Professional Android 4 Application Development, Reto Meier, Wiley India, (Wrox), 2012
2. Android Application Development for Java Programmers, James C Sheusi, Cengage
Learning, 2013
REFERENCEs:
1. Beginning Android 4 Application Development, Wei-Meng Lee, Wiley India(Wrox),
2013
2. Android Application Development (with Kitkat Support), Black Book, Pradeep Kothari,
2014, Dreamtech Press publisher, Kogent Learning Inc.,2014
3. Android Programming: Pushing the Limits, Erik Hellman, 1st Edition,Wiley Publications,
2014
2
INDEX
UNIT
TOPIC PAGE NO
NO
Introduction
Fragments 66-82
III
Intents and Broadcasts 90-105
IV
Database 111-119
V
3
UNIT-I
Introductionto Android Operating System: Android OSand Features– Androiddevelopmentframework;
Installing andrunningapplicationson Android Studio, Creating AVDs, Typesof Androidapplication;
CreatingActivities, Activity LifeCycle, Activitystates,monitoringstatechanges;
Introduction To Android
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.
Android offers a unified approach to application development for mobile devices which means developers
need only develop for Android, and their applications should be able to run on different devices powered by
Android.
Android has come a long way from its humble beginnings,
as the product of a small start up, all the way to becoming
the leading mobile operating system worldwide. Google’s
introduction of Project Treble in Android Oreo should
make it easier for phone makers to update their devices
faster.
Operating Systems
Different OS run on different types of hardware and are designed for different types of
applications. For example, iOS is designed for iPhones and iPad tablets, while Mac desktops
and laptops use macOS.
Microsoft Windows :
Initial versions of Windows worked with MS-DOS, providing a modern graphical interface
on top of DOS's traditional text-based commands. The Windows Start menu helps users find
programs and files on their devices.
APPLE IOS
Apple's iOS is one of the most popular smartphone operating systems, second only to Android. It
runs on Apple hardware, including iPhones, iPad tablets and iPod Touch media players.
GOOGLE'S ANDROID OS
Android is the most popular operating system in the world judging by the number of devices
installed. Users can download custom versions of the operating system.
APPLE MAC OS
Apple's macOS, successor to the popular OS X operating system, runs on Apple laptops and
4
desktops.. MacOS is known for its user-friendly features, which include Siri and FaceTime.
LINUX OPERATING SYSTEM
Linux can be run on a wide variety of hardware and is available free of charge over the internet.
FeaturesofAndroid
Android is a powerful operating system competing with Apple 4GS and
support great features. Few of them are listed below:
Feature Description
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
Web browser Based on the open-source WebKit layout engine, coupled with
Chrome's V8 JavaScript engine supporting HTML5 and CSS3.
Multi-touch Android has native support for multi-touch which was initially made
available in handsets such as the HTC Hero.
Multi-tasking User can jump from one task to another and same time various
application can run simultaneously.
Resizable widgets Widgets are resizable, so users can expand them to show more content
or shrink them to save space
5
GCM Google Cloud Messaging (GCM) is a service that let developers send
short message data to their users on Android devices, without needing
a proprietary sync solution.
Wi-Fi Direct A technology that let apps discover and pair directly, over a high-
bandwidth peer-to-peer connection.
Android Beam A popular NFC-based technology that let users instantly share, just by
touching two NFC-enabled phones together.
6
ANDROID LIBRARIES
In addition to a set of standard Java development libraries (providing support for such general
purpose tasks as string handling, networking and file manipulation), the Android development
environment also includes the Android Libraries. These are a set of Java-based libraries that
are specific to Android development.
C/C++ LIBRARIES
The Android runtime core libraries are Java-based and provide the primary APIs for developers
writing Android applications. It is important to note, however, that the corelibraries do not
perform much of the actual work and are, in fact, essentially Java
―wrappers around a set of C/C++ based libraries.
APPLICATION FRAMEWORK
The Application Framework is a set of services that collectively form the environment in which
Android applications run and are managed. This framework implements the concept that
Android applications are constructed from reusable, interchangeable and replaceable
components. This concept is taken a step further in that an application is also able to publish its
capabilities along with any corresponding data so that they can be found and reused by other
applications.
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.
7
INSTALLING AND RUNNING APPLICATIONS ON ANDROID STUDIO
Android Studio is the official IDE for android application development.It works based on IntelliJ IDEA,
You can download the latest version of android studio from Android Studio 2.2 Download, If you are
new to installing Android Studio on windows,you will find a file, which is named as android-studio- bundle-
143.3101438-windows.exe.So just download and run on windows machine according to android studio
wizard guideline.
If you are installing Android Studio on Mac or Linux, You can download the latest version from Android
Studio Mac Download,or Android Studio Linux Download, check the instructions provided along with
the downloaded file for Mac OS and Linux. This tutorial will consider that you are going to setup your
environment on Windows machine having Windows 8.1 operating system.
Installation
So let's launch Android Studio.exe,Make sure before launch Android Studio, Our Machine should
required installed Java JDK. To install Java JDK,take a references of Android environment setup
8
Once you launched Android Studio, its time to mention JDK path or later version in android studio
installer.
Need to check the components, which are required to create applications, below the image has
selected AndroidStudio, AndroidSDK, AndroidVirtualMachineand performance(Intelchip).
9
Need to specify the location of local machine path for Android studio and Android SDK, below the image
has taken default location of windows 8.1 x64 bit architecture.
Need to specify the ram space for Android emulator by default it would take 512MB of local machine
RAM.
10
At final stage, it would extract SDK packages into our local machine, it would take a while time to finish
the task and would take 2626MB of Hard disk space.
After done all above steps perfectly, you must get finish button and it gonna be open android studio
project with Welcome to android studio message as shown below
11
You can start your application development by calling start a new android studio project. in a new installation
frame should ask Application name, package information and location of the project.
After entered application name, it going to be called select the form factors your application runs on, here
need to specify Minimum SDK, in our tutorial, I have declared as API23: Android 6.0(Mashmallow)
12
The next level of installation should contain selecting the activity to mobile, it specifies the default layout
for Applications
At the final stage it going to be open development tool to write the application code.
13
Step 3 - Create Android Virtual Device
To test your Android applications, you will need a virtual Android device. So before we start writing our
code, let us create an Android virtual device. Launch Android AVD Manager Clicking AVD_Manager icon
as shown below
After Click on a virtual device icon, it going to be shown by default virtual devices which are present on
your SDK, or else need to create a virtual device by clicking Create new Virtual device button
14
ur AVD
is created successfully it means your environment is ready for Android application development. If you like, you can close this
window using top-right cross button. Better you re-start your machine and once you are done with this last step, you are ready to
proceed for your first Android example but before that we will see few more important concepts related to Android Application
Development.
Hello Word Example
Before Writing a Hello word code, you must know about XML tags.To write hello word code, you should
redirect to App>res>layout>Activity_main.xml
To show hello word, we need to call text view with layout ( about text view and layout, you must take
references at Relative Layout and Text View ).
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:text="@string/hello_world"
android:layout_width="550dp"
android:layout_height="wrap_content" />
15
Need to run the program by clicking Run>Run App or else need to call shift+f10key.
16
hese are the settings. As of this writing, these settings make Hello World app compatible
with 9% of Android devices active on the Google Play Store.
8. Leave unchecked the Include Instant App support and all other options. Then click
Next. If your project requires additional components for your chosen target SDK,
Android Studio will install them automatically.
9. The Add an Activity window appears. An Activity is a single, focused thing that the
user can do. It is a crucial component of any Android app. An Activity typically has a
layout associated with it that defines how UI elements appear on a screen. Android
Studio provides Activity templates to help you get started. For the Hello Worldproject,
choose Empty Activity as shown below, and click Next.
10. The Configure Activity screen appears (which differs depending on which template
you chose in the previous step). By default, the empty Activity provided by the template
is named MainActivity. You can change this if you want, but this lesson uses
MainActivity.
11. Make sure that the Generate Layout file option is checked. The layout nameby
default is activity_main. You can change this if you want, but this lesson uses
activity_main.
12. Make sure that the Backwards Compatibility (App Compat) option is checked. This
ensures that your app will be backwards-compatible with previous versions of Android.
13. Click Finish.
Android Studio creates a folder for your projects, and builds the project
with Gradle. The Android Studio editor appears. Follow these steps:
1. Click the activity_main.xmltab to see the layout editor.
2. Click the layout editor Design tab, if not already selected, to show a graphicalrendition
of the layout as shown below.
3. Click the MainActivity.java tab to see the code editor as shown below.
e manifests folder contains files that provide essential information about your app to the
Android system, which the system must have before it can run any of the app's code.
17
Expand the manifests folder.
1.
Open the AndroidManifest.xml file.
2.
The AndroidManifest.xml file describes all of the components of your Android app. All
components for an app, such as each Activity, must be declared in this XML file. In other
course lessons you will modify this file to add features and feature permissions. For an
introduction, see App Manifest Overview.
RUN ON EMULATOR
1.Lets create an android virtual device (avd). In order to run an emulator on your computer,
you have to create a configuration that describes the virtual device. In Android Studio, select
Tools > Android > AVD Manager, or click the AVD Manager icon in the toolbar.
The Your Virtual Devices screen appears. If you've already created virtual devices, the screen
shows them; otherwise you see a blank list.
2.Click the +Create Virtual Device. The Select Hardware window appears showing a list of
pre configured hardware devices. For each device, the table provides a column for its diagonal
display size (Size), screen resolution in pixels (Resolution), and pixel density(Density).
3. Choose a device such as Nexus 5x or Pixel XL, and click Next. The System Image screen appears.
4.Click the Recommended tab if it is not already selected, and choose which version of the
Android system to run on the virtual device (such as Oreo).Click the link to start thedownload,
and click Finishwhen it's done.
5.After choosing a system image, click Next. The Android Virtual Device (AVD) window
appears. You can also change the name of the AVD. Check your configuration and click Finish.
Run the app on the virtual device
Let’s run your Hello World app.
1. In Android Studio, choose Run> Runapp or click the Run icon in thetoolbar.
2. TheSelectDeploymentTarget window,underAvailableVirtual Devices,selectthe
virtual device, which you just created, and click OK
The emulator starts and boots just like a physical device. Your app builds, and once the
emulato
r is ready, Android Studio will upload the app to the emulator and run it.
18
DEPLOY IT ON USB-CONNECTED ANDROID DEVICE
In order to install an application directly to your device, you need to configure it to use a
USB connection. The configuration settings vary by device.
For Android 4.2 and later devices, you need to enable Developer options by opening
Settings, click About then click the Build number item seven items. If you do not do this,
you will not see the Developer options item in Settings.
1. Open Settings.
2. Click Security.
3. Enable Unknown sources, that is, check this option. This permits the device to
install apps that do not originate from Google Play.
4. Back out to Settings.
5. Click Developer options.
6. If available: Set the switch in the title bar to on.
7. Enable USB debugging, that is, check this option. This permits the device to install
apps over a USB connection.
8. Optional: Enable Stay awake, that is, check this option. This option keeps the
screen on and disables the lock screen while the device is connected to USB.
9. Optional: Enable Allow mock locations, that is, check this option. This option
creates fake GPS locations to test location services.
10. Back out of or close
(Windows only)
Developers on Windows may need to install a USB driver specific to the manufacturer and
model of the device on which they'll be testing. The driver enables your Windows computer
to communicate with your Android device. Google provides download links to the drivers
at Android Developer: OEM USB Drivers.
Connect the Android device to your computer using an USB cord. Note that some USB
cables are only power cables and do not allow communications with the device. Make sure
you use a USB cable that allows a data connection.
For 4.2 devices, an "Allow USB debugging?" dialog will appear once connected via
USB. Click the OK button.
19
In Studio, first select the project in the Project Explorer view, then in the global tool bar,
select Run from the Launch Mode drop-down list and an Android device from the Target
drop-down list under the Android Application Installer category. If the Launch
Automatically option is enabled under the Target drop- down list, the application will be
automatically launched after the device is selected. If not, you need to click the Run button
to start the build process. Your app will be built, installed to your device and automatically
launched
CREATING ACTIVITIES
By the help of activity, you can place all your UI components or widgets in a single screen.
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.
20
Android Activity Lifecycle
Let's see the lifecycle methods of android activity.
21
Sr.No Callback & Description
1 onCreate()
This is the first callback and called when the activity is first created.
onStart()
2
This callback is called when the activity becomes visible to the user.
onResume()
3
This is called when the user starts interacting with the application.
onPause()
4 The paused activity does not receive user input and cannot execute any code and call ed
when the c activity is being resumed.
onStop()
5
This callback is called when the activity is no longer visible.
onDestroy()
6
This callback is called before the activity is destroyed by the system.
onRestart()
This callback is called when the activity restarts after stopping it.
22
xample
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 pack ge
World Example chapter.
2 Modify main activity file MainActivity.java as explained below. Keep rest of the files unchanged.
3 Run the application to launch Android emulator and verify the result of the changes done in the applic tio
Page 23
23
Android Activity Lifecycle Example
It provides the details about the invocation of life cycle methods of activity. In this example, we are
displaying the content on the logcat.
File: MainActivity.java
package example.mrcet.com.activitylifecycle;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
{ @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("lifecycle","onCreate invoked");
}
@Override
protected void onStart() {
super.onStart();
Log.d("lifecycle","onStart
invoked");
}
@Override
protected void onResume() {
super.onResume();
Log.d("lifecycle","onResume
invoked");
}
@Override
protected void onPause() {
super.onPause();
Log.d("lifecycle","onPause
invoked");
}
@Override
protected void onStop() {
super.onStop();
Log.d("lifecycle","onStop
invoked");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d("lifecycle","onRestart
invoked");
}
24
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:
File: activity_main.xml
1. <?xml version="1.0" encoding="utf-8"?>
2. <android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
3. xmlns:app="http://schemas.android.com/apk/res-auto"
4. xmlns:tools="http://schemas.android.com/tools"
5. android:layout_width="match_parent"
6. android:layout_height="match_parent"
7. tools:context="example.mrcet.com.activitylifecycle.MainActivity"> 8.
9. <TextView
10. android:layout_width="wrap_content"
11. android:layout_height="wrap_content"
12. android:text="Hello World!"
13. app:layout_constraintBottom_toBottomOf="parent"
14. app:layout_constraintLeft_toLeftOf="parent"
15. app:layout_constraintRight_toRightOf="parent"
16. app:layout_constraintTop_toTopOf="parent" />
1.
18. </android.support.constraint.ConstraintLayout>
25