Android Programming Overview and Basics
Android Programming Overview and Basics
Gauthier Picard
2013
1. Android Overview
2. Basics
3. Android Projects
4. Activities
5. XML Layout
6. Manifest
8. References
. Agenda
1. Android Overview
What is Android?
Who is Using Android?
The Challenges
Downloading and Uploading Apps
Architecture logicielle
2. Basics
3. Android Projects
4. Activities
5. XML Layout
6. Manifest
8. References
. What is Android?
.
An open source operating system
.
▸ A mobile-device-oriented operating system
▸ It consists in a set of software to interface hardware and applicative software
▸ An open source operating system
▸ source code is available
▸ huge developper community
▸ An operating system based on the Linux kernel
▸ A free development environment
▸ Programming with Java or C language
▸ Development kit (SDK Android) available at
[Link]
.
.
A lot of versions...
.
The different versions of Android are all named as desserts since version 1.5 and
follow an alphabetical order:
▸ 1.0 – 2007 : little known version (distributed before the first Android phone)
▸ 1.1 – 2008 : version included in the first Android phone, HTC Dream
▸ 1.5 - Cupcake - 04/2009 : last official revision in May 2010
▸ 1.6 - Donut - 09/2009 : last official revision in May 2010
▸ 2.0 - Eclair - 2009 : quickly relaced by versions 2.0.1 then 2.1 due to major bugs
▸ 2.1 - Eclair - 01/2010 : last official revision in May 2010
▸ 2.2 (2.2.3) - FroYo - 05/2010 : last official revision in 2011
▸ 2.3 (2.3.7) - Gingerbread - 12/2010 : version for smartphones and small tablets
▸ 3.0 (3.2) - Honeycomb - 01/2011 : version for large tablets and TVs
▸ 4.0 (4.0.3) - Ice Cream Sandwich - 10/2011 : unified version for smartphone, tablet and GoogleTV
▸ 4.1 (to 4.3) - Jelly Bean : current version
▸ 4.4 - KitKat : version to come
.
Google TV
Consoles
Tablets
.
Open Handset Alliance
.
▸ Creation: 5th November 2007, initiated by Google
.
Competitors
.
▸ Apple with iOS
.
Phone Sales by OS
.
Windows Mobile/ Bada (Samsung)
Phone (Microsoft)
Other
Symbian (Nokia) Android (Google)
iOS (Apple)
Symbian (Nokia)
Android (Google)
Windows Mobile/Phone
(Microsoft)
Bada (Samsung)
Other
. Share of worldwide 2013 Q2 smartphone sales to end users by operating system, according to Gartner
. The Challenges
.
Android Functionnalities
.
▸ Applicative Framework
.
Constraints
.
▸ Low frequency CPU
.
Google Play Store (former Android Market)
.
Standard system to upload/download apps
▸ No filtering/verification of apps
▸ Navigation more difficult than AppMarket (search by category, keywords,
price)
▸ Requires a certified terminal (camera, 3G, compass...)
▸ Authorization management before installation
▸ 1+ million available apps (57% free-of-charge)
If you want to sell your apps:
▸ 25$ to register as a developer
▸ 70% of the app price to the developer, 30% to Google
▸ Revenue earned from the Google Play is paid to developers via Google Wallet
.
.
Other Platforms
.
There exist other legal application distribution platforms (i.e. authorized by
Google). They allow access to non certified devices
▸ AppsLib (Archos) : [Link]
▸ AndroLib : [Link]
▸ Market Samsung
▸ ...
.
. Agenda
1. Android Overview
2. Basics
Some Vocabulary
Android Software Architecture
3. Android Projects
4. Activities
5. XML Layout
6. Manifest
8. References
. Important Terms
.
Activity
.
▸ Building block for user interfaces
.
Content provider
.
▸ Abstraction level for any data stored in the device
.
Service
.
▸ A Service is an application component representing either an application’s
desire to perform a longer-running operation while not interacting with the
user or to supply functionality for other applications to use
▸ Does not need any UI
▸ Examples:
▸ Service verifying RSS flux updates
▸ Service enabling to listen a given playlist (independently from any activity)
.
.
Manifest
.
▸ Starting point for every Android app
.
Widget
.
▸ Portmanteau combining window and gadget
.
View
.
▸ Represents the basic building block for user interface components
▸ Occupies a rectangular area on the screen and is responsible for drawing and
event handling
▸ View is the base class for widgets
.
.
Container (View Group)
.
▸ A View Group is a special view that can contain other views (called children)
.
XML
.
▸ Extensible Markup Language
▸ Used to structure data
.
.
XML Layout
.
▸ Used to design GUI easier than using Java
.
Uniform Resource Identifier (URI)
.
▸ is a string of characters used to identify a name of a web resource (real or not)
.
Android Virtual Devices
.
▸ AVDs allows to simulate the execution of an Android device on a computer
▸ These devices are customizable (Android version, CPU model, memory, ...)
⇒ Eases the development and the la implementation of apps
.
. Agenda
1. Android Overview
2. Basics
3. Android Projects
Create a project from an existing sample
Create your own project
Android Project Directory Structure
4. Activities
5. XML Layout
6. Manifest
8. References
.
Contents of directory /
.
[Link] : the manifest file that describes the app
[Link] : the ant script to build and install the app
[Link] (and [Link]): two property files used by the
ant script
bin/ : directory that contains the compiled app
gen/ : directory that contains the source code generated by the Android
compilation tools
libs/ : directory that contains the external JAR files
src/ : directory taht contains the Java source code of the app
res/ : directory that contains the resources (icons, layouts, ...)
. assets/ : directory that contains other static files provided with the app
.
Contents of directory res/
.
anim/ : For XML files that are compiled into animation objects
color/ : For XML files that describe colors
drawable/ : For bitmap files (PNG, JPEG, or GIF), 9-Patch image files, and XML
files that describe Drawable shapes or Drawable objects that
contain multiple states (normal, pressed, or focused)
layout/ : XML files that are compiled into screen layouts (or part of a
screen)
menu/ : For XML files that define application menus
raw/ : For arbitrary raw asset files (mp3, ogg, etc.)
values/ : For XML files that are compiled into many kinds of resource
xml/ : For miscellaneous XML files that configure application
. components
.
Contents of directory bin/
.
bin/classes/ : directory that contains the compiled java classes
bin/[Link] : directory that contains the executable generated using
compiled classes
bin/[Link] : archive that contains the real app to deploy on the device
. …
. Agenda
1. Android Overview
2. Basics
3. Android Projects
4. Activities
Definition
Activity Lifecycle
5. XML Layout
6. Manifest
8. References
. What is an Activity?
.
Definition (Activity)
.
An Activity is an application component that provides a screen with which
users can interact in order to do something, such as dial the phone, take a
photo, send an email, or view a map. Each activity is given a window in
which to draw its user interface. The window typically fills the screen, but
may be smaller than the screen and float on top of other windows.
.
. Activity Lifecycle
onCreate():
▸ called when the activity is created
. Activity Lifecycle
onRestart():
▸ called after the activity has been stopped,
just prior to it being started again
▸ always followed by onStart()
▸ this method is therefore called when the
app becomes the foreground app after a
long pause
. Activity Lifecycle
onStart():
▸ called just before the activity becomes
visible to the user
▸ this method is called after each call to
onCreate() or onRestart()
▸ followed by onResume() if the activity
comes to the foreground, or onStop() if it
becomes hidden
▸ if necessary, it reloads data saved before
the previous interruption
. Activity Lifecycle
onResume():
▸ called just before the activity starts
interacting with the user (foreground)
▸ called after each onStart()
▸ at this point the activity is at the top of the
activity stack, with user input going to it
▸ always followed by onPause()
▸ typically:
▸ it manages the connexion to the
database
▸ it updates data that may be changed
before onResume()
. Activity Lifecycle
onPause():
▸ Called when:
▸ the system is about to start resuming
another activity
▸ the user asks a finish() for this activity
▸ the system needs to free some memory
▸ automatically called before onStop()
▸ typically:
▸ commit unsaved changes to persistent
data
▸ stop animations
▸ disconnect the database…
▸ It should do whatever it does very quickly,
because the next activity will not be
resumed until it returns
. Activity Lifecycle
onStop():
▸ called when the activity is no longer visible
to the user (destruction or another activity
is foreground)
▸ followed either by onRestart() if the
activity is coming back to interact with the
user, or by onDestroy() if this activity is
going away
▸ free resources
. Activity Lifecycle
onDestroy():
▸ called before the activity is destroyed
▸ this is the final call that the activity will
receive
▸ it could be called either because the
activity is finishing (someone called
finish() on it), or because the system is
temporarily destroying this instance of the
activity to save space
▸ onCreate() should be called again to
launch the activity
▸ if necessary free temporary files
. Agenda
1. Android Overview
2. Basics
3. Android Projects
4. Activities
5. XML Layout
XML
Using XML Layouts
6. Manifest
8. References
.
Example (A sample XML declaration for a library stock)
.
1 <?xml version=”1.0” encoding=”ISO-8859-1”?> <!-- header of the XML file (i.e. prologue) -->
2 <BIBLIOTHEQUE> <!-- start tag for BIBLIOTHEQUE -->
3
4 <MAGAZINE> <!-- declaration of a MAGAZINE -->
5 <TITRE>Science et Vie</TITRE>
6 <DATEPARUTION>01-02-2012</DATEPARUTION>
7 <PRIX devise=”Euro”>4.20</PRIX> <!-- ”PRIX” is a tag, ”devise” is an attribute, ”32.30” is a value -->
8 </MAGAZINE>
9
10 <LIVRE type=”education” > <!-- declaration of a LIVRE of type ”education” -->
11 <TITRE>L’art du d�veloppement Android</TITRE>
12 <AUTEUR>Mark Murphy</AUTEUR>
13 <PRIX devise=”Euro”>32.30 </PRIX>
14 </LIVRE>
15
16 <LIVRE type=”roman”> <!-- declaration of a LIVRE of type ”roman” -->
17 <TITRE>Le livre d’Android</TITRE>
18 <AUTEUR>Patrick Beuzit</AUTEUR>
19 <PRIX devise=”Dollar”>22.25</PRIX>
20 </LIVRE>
21
22 .
</BIBLIOTHEQUE> <!-- end tag for BIBLIOTHEQUE -->
LinearLayout (lines 1 to 5)
1 <LinearLayout ... / >: specifies the view group (here widgets will be inline)
2 android:id=”...”: declaration of the name (id) of the resource
3 android:layout_width=”...”: the width set to fill the parent (the screen)
4 android:layout_height=”...”: the height set to fill the parent (the screen)
5 android:orientation=”...”: the contained views are be vertically lined up
.
Steps
.
▸ In your XML editor (e.g. Eclipse with ADT plugin):
. Create an XML layout (pay particular attention to the views’ ids)
1
.
(1.a) Choosing a Layout
.
▸ LinearLayout: arranges its children in a single column or a single row
.
(1.b) Choosing and Positionning the Views
.
▸ EditText : an editable text field ▸ RadioButton : a two-states
▸
button that can be either
Toast :a view containing a quick
checked or unchecked
popup message for the user
▸ ImageButton: a button with an
▸ ImageView : an arbitrary image,
image (instead of text) that can
such as an icon
be pressed or clicked by the user
▸ CheckBox : a specific type of
▸ DatePicker : a widget for
two-states button that can be
selecting a date
either checked or unchecked
▸ SlidingDrawer : hides content
▸ Button : a push-button widget
out of the screen and allows the
that can be pressed, or clicked,
user to drag a handle to bring
by the user to perform an action
the content on screen
.
.
Android™ Programming Master WI 40 / 55
XML Layout Using XML Layouts
.
Android™ Programming Master WI 40 / 55
XML Layout Using XML Layouts
.
Android™ Programming Master WI 40 / 55
XML Layout Using XML Layouts
.
Android™ Programming Master WI 40 / 55
XML Layout Using XML Layouts
.
Linking Java Code and Layouts
.
▸ When the app is compiled, each XML layout is compiled as a View resource
▸ The application code must load the layout in the onCreate() method of the
activity calling the setContentView() method
▸ The setContentView() method takes as input the rerefence of the layout
(its id) , e.g. [Link].layout1
.
.
Example (A minimal onCreate() method loading a layout)
.
public void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].main_layout);
}
.
.
Example (A sample onCreate() method adding an event listener to a Button)
.
public void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].layout1);
Button button1 = findViewById([Link].button1)
.setOnClickListener(new [Link]() {
@Override
public void onClick(View view) {
// perform an action on click
}
});
.
}
. Agenda
1. Android Overview
2. Basics
3. Android Projects
4. Activities
5. XML Layout
6. Manifest
What is the manifest file?
Contents
[Link]
Example
8. References
.
Definition (Manifest File)
.
Every application must have an [Link] file (with
precisely that name) in its root directory. The manifest presents essential
information about the application to the Android system, information the
system must have before it can run any of the application’s code.
.
▸ XML file
▸ Specify the architecture of the app
▸ [Link] is at the root of the project
. Contents
.
In a glance...
.
▸ It names the Java package for the app (the package name ≡ unique identifier
for the app!)
▸ It describes the components of the app
▸It lists the activities, services, and content providers that the app is composed of
▸It names the classes that implement each of the components and publishes their
capabilities (e.g. which Intent messages they can handle)
⇒ These let the Android system know what the components are and under what
conditions they can be launched
▸ It determines which processes will host app components
▸ It declares which permissions the app must have in order to access protected
parts of the API (GPS, internet, etc.) and interact with other apps
▸ It declares the minimum level of the Android API that the app requires
▸ It lists the libraries that the app must be linked against
▸ It lists the Instrumentation classes that provide profiling and other
information as the app is running (for development issues)
.
Android™ Programming Master WI 45 / 55
Manifest [Link]
. [Link]
.
Conventions
.
▸ Only two elements are required:
▸ <manifest>: contains the package, the version... this root englobes the whole
manifest
▸ <application>: describes the app and contains the list of its components
▸ All values are set through attributes, not as character data within an element
▸ All attribute names begin with an android: prefix (except for some
attributes of the root <manifest> element)
.
.
Resources
.
▸ Instead of containing data per se, the manifest refers to resources using the
following format: @[package:]type:name
e.g. <activity android:icon=”@drawable/smallPic”...>
. [Link] (cont.)
.
Permissions
.
▸ If an application needs access to a feature protected by a permission, it must
declare that it requires that permission
▸ Specifiy permission with a <uses-permission> element in the manifest
▸ There exist several standard permissions:
▸ [Link].CALL_EMERGENCY_NUMBERS
▸ [Link].READ_OWNER_DATA
▸ [Link].SET_WALLPAPER
▸ [Link].DEVICE_POWER
▸ It is also possible to define your own permissions
.
. [Link] (cont.)
.
Example (A sample manifest file declaring permissions)
.
1 <manifest . . . >
2 <permission android:name=”[Link].DEBIT_ACCT” . . . />
3 <uses-permission android:name=”[Link].DEBIT_ACCT” />
4 . . .
5 <application . . .>
6 <activity android:name=”[Link]”
7 android:permission=”[Link].DEBIT_ACCT”
8 . . . >
9 . . .
10 </activity>
11 </application>
12 .
</manifest>
. [Link] (cont.)
.
Intent Filter
.
▸ Components advertise their capabilities — the kinds of intents they can
respond to — through intent filters
▸ A component may have any number of filters, each one describing a different
capability (e.g. a filter to edit an existing document, a filter to initialize a new
document, etc.)
▸ A filter must point to an action
.
. [Link] (cont.)
.
Example (A sample manifest file)
.
1 <?xml version=”1.0” encoding=”utf-8”?>
2 <manifest xmlns:android=”[Link]
3 package=”[Link]”
4 android:versionCode=”1”
5 android:versionName=”1.0”>
6 <application android:icon=”@drawable/icon” android:label=”@string/app_name”>
7 <activity android:name=”.CapturePhoto”
8 android:label=”@string/app_name”>
9 <intent-filter>
10 <action android:name=”[Link]” />
11 <category android:name=”[Link]” />
12 </intent-filter>
13 </activity>
14 </application>
15 <uses-sdk android:minSdkVersion=”3” />
16 <uses-permission android:name=”[Link]”/>
17 .
</manifest>
. Agenda
1. Android Overview
2. Basics
3. Android Projects
4. Activities
5. XML Layout
6. Manifest
8. References
.
Let’s look at the online documentation...
.
▸ [Link]
▸ [Link]
▸ [Link]
▸ [Link]
▸ [Link]
▸ [Link]
.
. Agenda
1. Android Overview
2. Basics
3. Android Projects
4. Activities
5. XML Layout
6. Manifest
8. References
. References
.
Books
.
. [Link]
.
Other documents (lectures...)
.
▸ Romain Raveaux, Cours Android — Développement et API, Laboratoire L3I, IUT de La Rochelle
▸ Nazim Benbourahla, several resources at [Link]
▸ Philippe Lacomme, Raksmey Phan, Créer des applications Android
.