Sri
Lanka
2012
Android
Lecture
1:
Android
Basics
h3p://[Link]
Sri
Lanka
2012
Agenda
• Ge;ng
Started
– Intro
to
the
Android
pla?orm
– Android
programming
in
Eclipse
– Anatomy
of
a
project
• Basic
Android
Components
– Layouts
– Views
and
Widgets
– Menus
Sri
Lanka
2012
Agenda
• Ge;ng
Started
– Intro
to
the
Android
pla?orm
– Android
programming
in
Eclipse
– Anatomy
of
a
project
• Basic
Android
Components
– Layouts
– Views
and
Widgets
– Menus
Sri
Lanka
2012
Android
Pla?orm
• Linux-based OS
• Platform components: GPS, WiFi, Camera,
Audio/Video recording + playback, Sensors
(acceleration, temperature, proximity,
gyroscope, magnetic, …)
• SQLite Local Database Storage
• Built-in Applications (Home, Contacts, Phone,
Browser, Voice Recognition, Camera, …)
Sri
Lanka
2012
Structure
of
an
Android
App
Android
Source
Layouts
(xml),
Manifest Resources,
Files
and
Assets
.xml
Sri
Lanka
2012
Crea6ng
Android
Projects,
Step
1:
Sri
Lanka
2012
Crea6ng
Android
Projects,
Step
2:
– Project Name
– Build Target
– Application name
– Package name
– Activity name
– Min SDK Version
Sri
Lanka
2012
Anatomy
of
Android
Project
(in
Eclipse)
Sri
Lanka
2012
Agenda
• Ge;ng
Started
– Intro
to
the
Android
pla?orm
– Android
programming
in
Eclipse
– Anatomy
of
a
project
• Basic
Android
Components
– Layouts
– Views
and
Widgets
– Menus
Sri
Lanka
2012
Layouts
• Defined in two ways
– XML layout files (Declarative)
Layout
file
[Link]
is
auto-‐generated
when
an
Android
project
is
created
in
Eclipse.
App
layout
can
be
defined
in
this
file
in
XML.
– using code
(Programmatic, e.g. in the onCreate() method)
Sri
Lanka
2012
Which
to
use?
Declara6ve
or
Programma6c
Sri
Lanka
2012
Best
prac6ce:
Use
both!
• Declara6ve
(XML)
to
define
sta6c
UI
components
– Look
and
feel
– Layout,
widgets,
etc.
• Programma6c
(Java)
to
define
interac6on
– What
UI
does
Sri
Lanka
2012
Some
Layouts
LinearLayout
Rela6veLayout
TableLayout
Sri
Lanka
2012
LinearLayout
• Arrange components one after another,
left-to-right, top-to-bottom:
Sri
Lanka
2012
Rela6veLayout
• Position and align components relative to
other components:
android:layout_below
is
an
a3ribute
that
can
be
used
only
with
Rela6veLayout.
Other
such
a3ributes
include
layout_alignParentRight,
and
layout_toLeftOf.
Sri
Lanka
2012
TableLayout
• Position components in rows and columns:
Sri
Lanka
2012
Views
• What they are: UI components
• Some common views and widgets:
Button CheckBox
EditText (a text box) RadioButton
TextView (a text label) ToggleButton
ListView RatingBar
GridView MapView (for embedding Google
TabView Maps objects in applications)
Spinner (a drop-down menu) WebView (for embedding web
browsers in applications)
Sri
Lanka
2012
Adding
Views
to
Layouts
• Example: adding a button and text label to
a LinearLayout:
Sri
Lanka
2012
Menus
Options Menu Context
Menu
SubMenu
Sri
Lanka
2012
Op6onsMenu
Example
• Step 1: Implement onCreateOptionsMenu()
method
• Step 2: Implement onOptionsItemSelected()
method
Sri
Lanka
2012
Handling
events
Listen to events using callback methods:
– onClick()
– onLongClick()
– onFocusChange()
– onKey()
– onTouch()
– onCreateContextMenu()
Sri
Lanka
2012
Example:
Event-‐handling
with
Bu3ons
Method
1:
Define
call-‐backs
using
code
Method
2:
Define
call-‐backs
in
Layout
XML
files