SlideShare uma empresa Scribd logo
1 de 263
Android	
  Programming	
  
Pasi	
  Manninen	
  
	
  

JAMK	
  University	
  of	
  Applied	
  Sciences	
  
Slide	
  version	
  Nov	
  20th,	
  2013	
  

	
  
1.	
  Android	
  IntroducDon	
  
What	
  is	
  Android?	
  
Android	
  Version	
  History	
  
Features	
  
Development	
  Framework	
  and	
  
Android	
  SoMware	
  Stack	
  
•  A	
  few	
  important	
  Android	
  terms	
  to	
  
understand	
  
•  Android	
  ApplicaDon	
  Fundamentals	
  
• 
• 
• 
• 

Android	
  Programming,	
  Pasi	
  Manninen.	
  

2	
  
What	
  is	
  Android?	
  
•  1.	
  A	
  free	
  open-­‐source	
  operaDng	
  system	
  for	
  mobile	
  devices	
  
•  2.	
  An	
  open-­‐source	
  development	
  pla.orm	
  for	
  creaDng	
  
mobile	
  and	
  applicaDons	
  
•  3.	
  Devices	
  that	
  runs	
  Android	
  applicaDons	
  
•  Allows	
  coding	
  with	
  Java	
  language	
  
•  Based	
  on	
  the	
  Linux	
  kernel	
  
•  Open	
  (all	
  features	
  are	
  open	
  to	
  developer)	
  
•  All	
  applicaDons	
  are	
  equal	
  (core	
  and	
  3th	
  party)	
  
•  Really	
  fast	
  and	
  easy	
  applicaDon	
  development	
  
•  Developed	
  by	
  Google	
  and	
  the	
  Open	
  Handset	
  Alliance,	
  OHA	
  
•  Announced	
  5th	
  Nov,	
  2007	
  
•  h[p://en.wikipedia.org/wiki/Android_(operaDng_system)	
  	
  	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

3	
  
Open	
  Handset	
  Alliance	
  	
  
•  OHA,	
  Group	
  of	
  technology	
  and	
  mobile	
  
companies	
  
•  Android	
  is	
  the	
  first	
  complete,	
  open,	
  and	
  free	
  
mobile	
  pla`orm	
  provided	
  by	
  OHA	
  
•  The	
  goal	
  is	
  offer	
  richer	
  experience	
  with	
  mobile	
  
devices	
  (with	
  open	
  standards)	
  
•  Handset	
  manufacturers	
  don’t	
  need	
  to	
  pay	
  any	
  
lisencing	
  fees	
  to	
  load	
  Android	
  on	
  their	
  devices	
  
•  h[p://www.openhandsetalliance.com	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

4	
  
Android	
  Version	
  History	
  
•  Android	
  has	
  seen	
  a	
  number	
  of	
  updates	
  since	
  
its	
  original	
  release	
  (July	
  22nd,	
  2013):	
  
	
  
	
  
	
  
	
  
	
  
	
  
	
  
	
  
h[p://en.wikipedia.org/wiki/Ice_Cream_Sandwich_(operaDng_system)#Android_4.0.x_Ice_Cream_Sandwich	
  	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

5	
  
NaDve	
  Android	
  ApplicaDons	
  
• 
• 
• 
• 
• 
• 
• 
• 
• 
• 
• 
• 
• 

An	
  e-­‐mail	
  client	
  
An	
  SMS	
  management	
  applicaDon	
  
A	
  full	
  PIM	
  (Calendar,	
  Contacts,	
  ...)	
  
A	
  WebKit-­‐based	
  wed	
  browser	
  
A	
  music	
  player	
  and	
  picture	
  gallery	
  
A	
  Camera	
  and	
  video	
  recording	
  apps	
  
The	
  Home	
  Screen	
  
The	
  Android	
  Market	
  (Google	
  mobile)	
  
Google	
  Maps	
  (Google	
  mobile)	
  
Gmail	
  Client	
  (Google	
  Mobile)	
  
Google	
  Talk	
  (Google	
  Mobile)	
  
YouTube	
  video	
  Player	
  (Google	
  Mobile)	
  
…	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

6	
  
Features	
  (Android	
  SDK)	
  
•  GSM,	
  EDGE,	
  3G,	
  Wi-­‐Fi	
  networks	
  for	
  telephony	
  or	
  data	
  transfer	
  
•  LocaDon	
  based	
  services	
  as	
  GPS,	
  Google	
  Maps,	
  Geocoding	
  
•  Full	
  mulDmedia	
  control	
  (playback	
  and	
  recording	
  with	
  camera	
  and	
  
microphone),	
  formats	
  MPEG4,	
  H.264,MP3,AAC,AMR,JPG,PNG,GIF...	
  
•  Sensors,	
  accelerometers,	
  compass	
  
•  Bluetooth	
  
•  Shared	
  data	
  stores	
  (SQLite,	
  Preferenses,	
  Content	
  Providers)	
  
•  Home-­‐Screen	
  widgets,	
  Live	
  Folders,	
  Live	
  Wallpapers	
  
•  An	
  integrated	
  WebKit-­‐based	
  browser	
  
•  Hardware	
  accelerated	
  graphics	
  (OpenGL	
  ES	
  2.0)	
  
•  Background	
  applicaDons	
  and	
  processes	
  
•  No	
  licensing,	
  distribuDon	
  or	
  development	
  fees	
  
•  …	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

7	
  
Development	
  Framework	
  
•  Android	
  applicaDons	
  are	
  wri[en	
  with	
  Java	
  
•  Programs	
  are	
  executed	
  in	
  custom	
  virtual	
  machine	
  
called	
  Dalvik	
  
•  Each	
  applicaDon	
  runs	
  its	
  own	
  process	
  in	
  Dalvik	
  
•  SDK	
  gives	
  all	
  what	
  you	
  need	
  start	
  developing,	
  tesDng	
  
and	
  debugging	
  your	
  applicaDon	
  
–  Android	
  API	
  
–  Development	
  tools	
  
–  Android	
  Virtual	
  Device	
  Manager	
  and	
  Emulator	
  
–  Full	
  documentaDon	
  with	
  sample	
  codes	
  
–  Online	
  support	
  h[p://developer.android.com	
  	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

8	
  
Android	
  SoMware	
  Stack	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

9	
  
A	
  few	
  important	
  Android	
  “terms”	
  
• 

AcDvity	
  
– 

• 

ApplicaDon	
  Class	
  
– 

• 

A	
  component	
  that	
  does	
  nothing	
  but	
  receive	
  and	
  react	
  to	
  broadcast	
  announcements	
  

Views	
  
– 
– 

• 

”intenDon”	
  to	
  do	
  some	
  work	
  in	
  your	
  applicaDon	
  
Asynchronous	
  messages	
  which	
  holds	
  the	
  content	
  of	
  the	
  message	
  

IntentReceiver	
  
– 

• 

Makes	
  a	
  specific	
  set	
  of	
  the	
  applicaDon's	
  data	
  available	
  to	
  other	
  applicaDons	
  

Intent	
  
– 
– 

• 

Doesn’t	
  have	
  visual	
  interface,	
  runs	
  background	
  for	
  an	
  indefinite	
  period	
  of	
  Dme	
  

ContentProvider	
  
– 

• 

Differences	
  to	
  AcDvity	
  based	
  applicaDons:	
  maintain	
  applicaDon	
  state	
  and	
  it	
  is	
  easy	
  to	
  transfer	
  objects	
  and	
  
use	
  resources	
  between	
  components	
  

Service	
  
– 

• 

A	
  single	
  visual	
  user	
  interface,	
  applicaDon	
  is	
  made	
  up	
  of	
  one	
  or	
  more	
  AcDviDes	
  

User	
  interface	
  elements	
  like	
  a	
  Bu[on	
  or	
  a	
  Label	
  or	
  lots	
  of	
  other	
  UI	
  elements	
  
Hierarchical	
  structure	
  

Home	
  Screen	
  Widgets	
  
– 

	
  ApplicaDons	
  that	
  can	
  be	
  run	
  in	
  Android	
  devices	
  home	
  screen	
  (can	
  be	
  an	
  entry	
  point	
  to	
  real	
  applicaDon	
  also)	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

10	
  
Android	
  ApplicaDon	
  Fundamentals	
  
•  Android	
  applicaDon	
  lives	
  in	
  its	
  own	
  world	
  
–  Every	
  applicaDon	
  runs	
  in	
  its	
  own	
  process	
  
–  Each	
  applicaDon	
  is	
  assigned	
  a	
  unique	
  Linux	
  user	
  
ID,	
  applicaDon	
  files	
  are	
  visible	
  only	
  that	
  user	
  and	
  
applicaDon	
  (permissions)	
  

•  ApplicaDon	
  can	
  make	
  use	
  of	
  elements	
  of	
  other	
  
applicaDons	
  
•  System	
  must	
  be	
  able	
  to	
  start	
  an	
  applicaDon	
  
process	
  when	
  any	
  part	
  of	
  it	
  is	
  needed	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

11	
  
2.	
  Set	
  up	
  a	
  complete	
  Android	
  
development	
  environment	
  
• 
• 
• 
• 

System	
  requirements	
  
Installing	
  Java	
  	
  
Serng	
  up	
  the	
  ADT	
  Bundle	
  
Hands-­‐on	
  training:	
  	
  
–  Install	
  and	
  configure	
  Android	
  development	
  
environment	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

12	
  
System	
  Requirements	
  
•  OperaDng	
  systems	
  
–  Windows	
  XP	
  (32-­‐bit),	
  Vista	
  (32-­‐	
  or	
  64-­‐bit),	
  or	
  
Windows	
  7	
  (32-­‐	
  or	
  64-­‐bit)	
  
–  Mac	
  OS	
  X	
  10.5.8	
  or	
  later	
  (x86	
  only)	
  
–  Linux	
  (tested	
  on	
  Ubuntu	
  Linux,	
  Lucid	
  Lynx)	
  

•  Eclipse	
  IDE	
  3.6.2	
  or	
  later,	
  JDT-­‐plugin	
  
•  JDK	
  6	
  (or	
  later)	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

13	
  
Installing	
  Java	
  	
  
•  Needed	
  Java	
  JDK	
  can	
  be	
  download	
  from	
  
Oracle	
  site:	
  
–  h[p://www.oracle.com/technetwork/java/javase/
downloads/index.html	
  	
  

•  Remember	
  download	
  right	
  version	
  of	
  Java	
  (32	
  
or	
  64	
  bit)	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

14	
  
Serng	
  up	
  the	
  ADT	
  Bundle	
  
•  To	
  get	
  started	
  go	
  to	
  
–  h[p://developer.android.com/sdk/index.html	
  

•  Download	
  the	
  SDK	
  and	
  unpack	
  the	
  ZIP	
  file	
  to	
  
appropriate	
  locaDon	
  
•  Open	
  Eclipse	
  from	
  eclipse	
  folder	
  
•  Install	
  addiDonal	
  version	
  of	
  Android	
  or	
  other	
  
needed	
  packages	
  with	
  SDK	
  Manager	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

15	
  
Install	
  and	
  Configure	
  Android	
  
development	
  environment	
  
•  Hands-­‐on	
  training:	
  
–  Download	
  and	
  install	
  Java	
  JDK	
  
–  Download	
  and	
  install	
  the	
  Android	
  SDK	
  (it	
  installs	
  
eclipse	
  too)	
  
–  Start	
  eclipse	
  and	
  Android	
  SDK	
  Manager	
  
–  Add	
  needed	
  Android	
  packages	
  and	
  other	
  extras	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

16	
  
3.	
  CreaDng	
  a	
  first	
  Android	
  ApplicaDon	
  
• 
• 
• 
• 

CreaDng	
  Android	
  Project	
  in	
  Eclipse	
  
Running	
  applicaDon	
  in	
  Emulator	
  
Running	
  applicaDon	
  in	
  Device	
  
Debugging	
  applicaDon	
  
–  Eclipse	
  debug	
  perspecDve	
  
–  Dalvik	
  Debug	
  Monitor	
  Service	
  (DDMS)	
  

•  Hands-­‐on	
  training:	
  	
  
–  Hello	
  Android!	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

17	
  
CreaDng	
  an	
  Android	
  Project	
  in	
  Eclipse	
  
•  Android	
  project	
  contains	
  
all	
  the	
  files	
  that	
  comprise	
  
the	
  source	
  code	
  for	
  your	
  
Android	
  app	
  
•  Android	
  SDK	
  tools	
  make	
  it	
  
easy	
  to	
  start	
  a	
  new	
  
Android	
  project	
  with	
  a	
  set	
  
of	
  default	
  project	
  
directories	
  and	
  files	
  
•  Select	
  File	
  >	
  New	
  >	
  
Android	
  ApplicaDon	
  
Project	
  from	
  Eclipse	
  
•  A	
  few	
  dialogs	
  will	
  be	
  shown	
  to	
  configure	
  your	
  
applicaDon	
  default	
  serngs	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

18	
  
Running	
  applicaDon	
  in	
  Emulator	
  
•  With	
  ADT	
  running	
  your	
  applicaDon	
  does	
  
following:	
  

–  Current	
  project	
  compiles	
  and	
  converts	
  to	
  Android	
  
executable	
  (.dex)	
  
–  Executable	
  and	
  resources	
  will	
  be	
  packed	
  into	
  an	
  
Android	
  package	
  (.apk)	
  
–  Selected	
  virtual	
  device	
  will	
  be	
  started	
  and	
  your	
  
applicaDon	
  will	
  be	
  installed	
  and	
  started	
  

•  Needed	
  emulators	
  (virtual	
  devices)	
  can	
  be	
  
created	
  with	
  Android	
  Virtual	
  Device	
  Manager	
  
(AVD)	
  	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

19	
  
Running	
  applicaDon	
  in	
  device	
  
•  Device	
  has	
  to	
  be	
  plugged	
  
to	
  development	
  machine	
  
with	
  USB	
  cable	
  
•  USB	
  debugging	
  has	
  to	
  be	
  
enabled	
  in	
  device	
  
•  Select	
  Run	
  
ConfiguraDons…	
  and	
  
choose	
  device	
  from	
  
Android	
  Device	
  Chooser	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

20	
  
Debugging	
  applicaDon	
  
•  The	
  Debug	
  PerspecDve	
  in	
  Eclipse	
  
–  Use	
  Debug,	
  Variables,	
  Breakpoints	
  and	
  LogCat	
  
tabs	
  in	
  Eclipse	
  

•  The	
  DDMS	
  (Dalvik	
  Debug	
  Monitor	
  Service)	
  
PerspecDve	
  
–  Show	
  the	
  AVDs	
  and	
  real	
  devices	
  list	
  
–  Emulator	
  controls	
  (locaDon,	
  phone	
  call,	
  …)	
  
–  Processes,	
  Threads,	
  heap,	
  memory	
  allocaDon,	
  
files,	
  …	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

21	
  
CreaDng	
  a	
  first	
  Android	
  ApplicaDon	
  
•  Hands-­‐on	
  training	
  :	
  Hello	
  Android!	
  
–  Create	
  a	
  new	
  Android	
  Project	
  
–  Create	
  first	
  Android	
  ApplicaDon	
  
–  Run,	
  debug	
  and	
  test	
  in	
  an	
  	
  
emulator	
  and	
  real	
  device	
  
–  DDMS,	
  Dalvik	
  Debug	
  
Monitor	
  Service	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

22	
  
4.	
  Publishing	
  ApplicaDon	
  to	
  Google	
  
Play	
  
•  ApplicaDon	
  Requirements	
  
•  Signing	
  the	
  ApplicaDon	
  
–  Strategy	
  
–  Debug	
  and	
  Public	
  Release	
  

•  Google	
  Play	
  
–  ApplicaDon	
  LisDng	
  
–  Uploading	
  a	
  new	
  ApplicaDon	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

23	
  
ApplicaDon	
  Requirements	
  
•  ApplicaDon	
  must	
  be	
  signed	
  with	
  private	
  key,	
  validity	
  period	
  
ends	
  aMer	
  22	
  October	
  2033	
  
•  ApplicaDon	
  must	
  define	
  both	
  an	
  android:versionCode	
  and	
  
an	
  android:versionName	
  a[ribute	
  in	
  the	
  manifest	
  file	
  
•  ApplicaDon	
  must	
  define	
  both	
  an	
  android:icon	
  and	
  an	
  
android:label	
  a[ribute	
  in	
  the	
  <applica:on>	
  element	
  of	
  its	
  
manifest	
  
<manifest	
  xmlns:android="h[p://schemas.android.com/apk/res/android"
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  package="fi.ptm.nameday" 	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  android:versionCode="8"	
   	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  android:versionName="1.0.6">	
  	
  	
  	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  <applicaDon	
  android:icon="@drawable/icon"	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  android:label="@string/app_name">	
  
...	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

	
  	
  

24	
  
Signing	
  the	
  ApplicaDon	
  
•  All	
  Android	
  apps	
  must	
  be	
  signed	
  

–  the	
  system	
  will	
  not	
  install	
  an	
  applicaDon	
  that	
  is	
  not	
  signed	
  

•  You	
  can	
  use	
  self-­‐signed	
  cerDficates	
  to	
  sign	
  your	
  applicaDons	
  
–  no	
  cerDficate	
  authority	
  is	
  needed	
  

•  ApplicaDon	
  must	
  be	
  signed	
  it	
  with	
  a	
  suitable	
  private	
  key	
  before	
  
publishing	
  to	
  Android	
  Market	
  
–  By	
  default	
  applicaDon	
  is	
  signed	
  with	
  debug	
  key	
  (generated	
  with	
  SDK	
  
Tools	
  in	
  Eclipse)	
  

•  The	
  system	
  tests	
  a	
  signer	
  cerDficate's	
  expiraDon	
  date	
  only	
  at	
  install	
  
Dme	
  
•  You	
  can	
  use	
  standard	
  tools	
  to	
  generate	
  keys	
  and	
  sign	
  your	
  
applicaDon	
  .apk	
  files	
  
–  Keytool	
  and	
  Jarsigner	
  (in	
  command	
  line)	
  
–  ADT	
  Export	
  Wizard	
  in	
  Eclipse	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

25	
  
Signing	
  Strategy	
  
•  Use	
  same	
  cerDficate	
  for	
  all	
  applicaDons	
  
–  applicaDon	
  upgrade	
  (seamless)	
  
–  applicaDon	
  modularity	
  (same	
  process)	
  
–  code	
  and	
  data	
  sharing	
  (through	
  permissions)	
  

•  Validity	
  period	
  of	
  cerDficate	
  exceeds	
  the	
  
expected	
  lifespan	
  of	
  applicaDon	
  
–  validity	
  period	
  of	
  25	
  years	
  or	
  more	
  is	
  recommended	
  
(no	
  upgrades)	
  
–  Android	
  Market	
  (validity	
  period	
  ending	
  aMer	
  22	
  
October	
  2033)	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

26	
  
Signing	
  in	
  Debug	
  Mode	
  
•  The	
  Android	
  build	
  tools	
  provide	
  a	
  debug	
  signing	
  mode	
  
•  ADT	
  generates	
  a	
  debug	
  cerDficate	
  automaDcally	
  for	
  
emulator	
  and	
  device	
  	
  
–  Keystore	
  name:	
  "debug.keystore"	
  
–  Keystore	
  password:	
  "android"	
  
–  Key	
  alias:	
  "androiddebugkey"	
  
–  Key	
  password:	
  "android"	
  
–  CN:	
  "CN=Android	
  Debug,O=Android,C=US“	
  
–  Keystore	
  is	
  located	
  in	
  your	
  home	
  directory’s	
  .android	
  dir	
  

•  Debug	
  cerDficates	
  expire	
  aMer	
  365	
  days	
  aMer	
  creaDon	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

27	
  
Signing	
  Public	
  Release	
  
• 
• 
• 
• 
• 

Make	
  sure	
  that	
  your	
  applicaDon	
  is	
  ready	
  
Obtain	
  a	
  suitable	
  private	
  key	
  
Compile	
  applicaDon	
  in	
  release	
  mode	
  
Sign	
  applicaDon	
  with	
  private	
  key	
  
On	
  Eclipse	
  right-­‐click	
  on	
  your	
  project	
  folder	
  
–  select	
  Android	
  Tools	
  >	
  Export	
  Unsigned	
  
ApplicaDon	
  Package	
  
–  select	
  File	
  >	
  Export	
  to	
  export	
  signed	
  APK,	
  select	
  
Export	
  Android	
  ApplicaDon	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

28	
  
Publish	
  -­‐	
  KeyStore	
  
•  Create	
  a	
  new	
  KeyStore	
  if	
  needed	
  
•  Remember	
  your	
  
password	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

29	
  
Publish	
  –	
  Key	
  CreaDon	
  
•  KeyStore	
  Alias	
  for	
  later	
  use	
  
•  Validity	
  period	
  
•  Personal,	
  corporate	
  or	
  
organizaDon	
  
idenDficaDon	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

30	
  
Publish	
  –	
  Signed	
  APK	
  File	
  
•  Enter	
  desDnaDon	
  for	
  the	
  APK	
  file	
  
•  APK	
  file	
  is	
  ready	
  
to	
  publish	
  in	
  	
  
Android	
  Market	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

31	
  
Google	
  Play	
  
•  Google	
  Play	
  is	
  a	
  hosted	
  service	
  

–  easy	
  for	
  users	
  to	
  find	
  and	
  download	
  Android	
  applicaDons	
  
–  easy	
  for	
  developers	
  to	
  publish	
  their	
  applicaDons	
  

•  To	
  publish	
  your	
  applicaDon	
  on	
  Google	
  Play	
  

–  register	
  with	
  the	
  service	
  using	
  your	
  Google	
  account	
  
–  agree	
  to	
  the	
  terms	
  of	
  service	
  
–  developer	
  fee	
  is	
  $25	
  (Iphone	
  developer	
  cost	
  is	
  $99)	
  

•  Once	
  published,	
  users	
  can	
  see	
  your	
  applicaDon,	
  
download	
  it,	
  and	
  rate	
  it	
  using	
  the	
  Play	
  applicaDon	
  
installed	
  on	
  their	
  Android-­‐powered	
  devices	
  
•  h[ps://play.google.com/apps/publish/	
  	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

32	
  
ApplicaDon	
  LisDngs	
  
•  All	
  applicaDons	
  are	
  listed	
  nicely	
  with	
  basic	
  
informaDon	
  
–  Name,	
  download	
  counts,	
  stars	
  and	
  so	
  on	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

33	
  
Uploading	
  applicaDon	
  
•  ApplicaDon	
  uploading	
  
is	
  easy	
  
•  You	
  can	
  add	
  a	
  
screenshots	
  and	
  
promoDonal	
  	
  
graphics	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

34	
  
ApplicaDon	
  Details	
  
•  Select:	
  
–  languages	
  
–  descripDon	
  
–  app	
  type	
  and	
  
category	
  
–  price	
  
–  protecDon	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

35	
  
Contact	
  InformaDons	
  
•  Select	
  Copy	
  
protecDon	
  
•  Give	
  contact	
  
InformaDon	
  
•  And	
  	
  
Publish!	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

36	
  
5.	
  Directory	
  Structure	
  of	
  an	
  Android	
  
ApplicaDon	
  Project	
  
•  Android	
  ApplicaDon	
  Project	
  folder	
  
•  Android	
  ApplicaDon	
  Resources	
  
•  Resource	
  examples:	
  
–  strings.xml	
  
–  colors.xml	
  

•  AndroidManifest.xml	
  
•  AndroidManifest.xml	
  example:	
  
–  HelloAndroid	
  project	
  

•  SupporDng	
  different	
  languages	
  and	
  hardwares	
  
•  Exercise:	
  Texts,	
  colors	
  and	
  localizaDon	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

37	
  
Android	
  ApplicaDon	
  Project	
  folder	
  
•  Android	
  project	
  holds	
  all	
  the	
  code	
  and	
  resources	
  in	
  
different	
  folders.	
  
•  Some	
  of	
  the	
  folders	
  are	
  generated	
  by	
  default	
  and	
  	
  
some	
  has	
  to	
  be	
  done	
  by	
  self	
  if	
  needed	
  
	
  
src	
  	
  (all	
  source	
  codes)	
  
gen	
  (Java	
  files	
  generated	
  by	
  ADT)	
  
assets	
  (used	
  for	
  example	
  store	
  raw	
  asset	
  files)	
  
bin	
  (output	
  directory,	
  apk	
  is	
  here)	
  
res	
  (resources	
  of	
  the	
  applicaDon)	
  
res/drawable	
  (image	
  and	
  image-­‐descriptor	
  files)	
  
res/layout	
  (views	
  of	
  the	
  applicaDon)	
  
res/menu	
  (applicaDon	
  menus)	
  
res/values	
  (other	
  resources	
  of	
  the	
  applicaDon)	
  
…	
  

–  Strings,	
  styles,	
  colors	
  and	
  so	
  on...	
  (xml	
  based)	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

38	
  
Android	
  ApplicaDon	
  Resources	
  
•  It	
  is	
  good	
  to	
  keep	
  non-­‐code	
  resources	
  (images,	
  strings,	
  animaDon,	
  themes,	
  
layouts)	
  external	
  to	
  code	
  
•  Easy	
  to	
  maintain,	
  update	
  and	
  manage	
  
•  Supports	
  different	
  devices	
  and	
  localizaDon	
  
•  Android	
  selects	
  resources	
  dynamically	
  in	
  runDme	
  (no	
  need	
  to	
  code)	
  
•  ApplicaDon	
  resources	
  are	
  stored	
  under	
  res/	
  folder	
  
– 
– 
– 
– 
– 

res/anim	
  (frame	
  by	
  frame	
  animaDons)	
  
res/drawable	
  (bitmaps,	
  other	
  types	
  drawable	
  types)	
  
res/layout	
  (UI	
  layouts)	
  
res/menu	
  (menu	
  layouts)	
  
res/values	
  (different	
  resources)	
  for	
  example	
  

•  arrays.xml,	
  colors.xml,	
  dimens.xml,	
  strings.xml,	
  styles.xml,	
  themes.xml	
  

–  res/xml	
  (arbitrary	
  XML	
  files)	
  

• 
• 

Resources	
  can	
  be	
  used	
  from	
  code	
  or	
  from	
  other	
  recources	
  
System	
  Resources	
  are	
  defined	
  under	
  android.R	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

39	
  
Resource	
  example:	
  strings.xml	
  
•  Easy	
  to	
  update	
  later,	
  localizaDon	
  
<?xml	
  version="1.0"	
  encoding="u/-­‐8"?>	
  
<resources>	
  
	
  	
  	
  	
  <string	
  name="app_name">NameDay</string>	
  
	
  	
  	
  	
  <string	
  name="widget_header">Nameday's</string>	
  
	
  	
  	
  	
  <string	
  name="widget_footer">(c)	
  2013	
  PTM</string>	
  
</resources>	
  
Used	
  in	
  main.xml	
  layout	
  (for	
  example):	
  
<TextView	
  
	
  	
  	
  	
  android:id=”@+id/text”	
  
	
  	
  	
  	
  android:text=”@string/widget_header”	
  />	
  
//	
  Used	
  in	
  code:	
  
CharSequence	
  str	
  =	
  getString(R.string.widget_header);	
  
TextView	
  tv	
  =	
  (TextView)	
  findViewById(R.id.text);	
  
tv.setText(str);	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

40	
  
Resource	
  example:	
  colors.xml	
  
•  Stored	
  in	
  res/values	
  folder	
  
•  It	
  is	
  good	
  pracDce	
  to	
  keep	
  all	
  color	
  values	
  in	
  same	
  file	
  
•  Used	
  same	
  way	
  as	
  strings	
  in	
  earlier	
  example	
  
<?xml	
  version="1.0"	
  encoding="u/-­‐8"?>	
  
<resources>	
  
	
  	
  	
  	
  <color	
  name="opaque_blue">#00F</color>	
  
	
  	
  	
  	
  <color	
  name="transparent_green">#7700FF00</color>	
  
	
  	
  	
  	
  <color	
  name="Ptle_color_dark">#FF860000</color>	
  
</resources>	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

41	
  
AndroidManifest.xml	
  
•  ApplicaDon	
  descriptor	
  file	
  
•  Defines	
  your	
  applicaDon	
  
–  AcDviDes	
  
–  ContentProviders	
  
–  Services	
  
–  Intent	
  Receivers	
  
–  Permissions	
  
–  Version	
  number	
  
–  	
  and	
  a	
  lots	
  of	
  more...	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

42	
  
AndroidManifest.xml	
  
example:	
  HelloAndroid	
  project	
  
<?xml	
  version="1.0"	
  encoding="u/-­‐8"?>	
  
<manifest	
  xmlns:android="hTp://schemas.android.com/apk/res/android"	
  
	
  	
  	
  	
  package="fi.ptm.helloandroid"	
  
	
  	
  	
  	
  android:versionCode="1"	
  
	
  	
  	
  	
  android:versionName="1.0"	
  >	
  
	
  
	
  	
  	
  	
  <uses-­‐sdk	
  android:minSdkVersion="16"	
  android:targetSdkVersion="17"	
  />	
  
	
  
	
  	
  	
  	
  <applicaDon	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:allowBackup="true"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:icon="@drawable/ic_launcher"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:label="@string/app_name"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:theme="@style/AppTheme"	
  >	
  
	
  	
  	
  	
  	
  	
  	
  	
  <acDvity	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  android:name="fi.ptm.helloandroid.MainAcPvity"	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  android:label="@string/app_name"	
  >	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  <intent-­‐filter>	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  <acDon	
  android:name="android.intent.acPon.MAIN"	
  />	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  <category	
  android:name="android.intent.category.LAUNCHER"	
  />	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  </intent-­‐filter>	
  
	
  	
  	
  	
  	
  	
  	
  	
  </acDvity>	
  
	
  	
  	
  	
  </applicaDon>	
  
	
  
</manifest>	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

43	
  
Different	
  languages	
  and	
  hardware	
  
•  Like	
  said	
  Android	
  has	
  dynamic	
  resource	
  selecDon	
  
mechanism	
  
•  All	
  is	
  done	
  with	
  using	
  directory	
  structure	
  
•  Spesific	
  languages,	
  locaDons	
  and	
  hardware	
  
•  AlternaDves	
  are	
  described	
  with	
  –	
  mark	
  
	
  
For	
  example	
  project:	
  
-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐	
  
res/layout/main.xml	
  
res/layout-­‐land/main.xml	
  
res/values-­‐fi/strings.xml	
  
res/values-­‐en/strings.xml	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

44	
  
Support	
  different	
  screen	
  sizes	
  
•  Portrait	
  and	
  lanscape	
  modes	
  
can	
  be	
  easily	
  done	
  with	
  
resources	
  
•  Portrait	
  mode	
  is	
  created	
  
automaDcally	
  when	
  project	
  is	
  
done	
  (layout)	
  
•  Create	
  a	
  new	
  folder	
  (layout-­‐
land)	
  for	
  landscape	
  acDviDes	
  
•  Android	
  switches	
  layouts	
  
automaDcally	
  when	
  new	
  
mode	
  is	
  detected	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

45	
  
Support	
  different	
  languages	
  
•  Language	
  support	
  is	
  done	
  
via	
  resources	
  also	
  
•  Default	
  language	
  strings	
  
are	
  stored	
  to	
  values	
  folder	
  
•  Create	
  a	
  new	
  values-­‐
(language	
  code)	
  folder	
  for	
  
all	
  supported	
  languages	
  
–  For	
  example	
  values-­‐fi	
  for	
  
Finnish	
  

•  Android	
  automaDcally	
  uses	
  
right	
  values	
  folder	
  if	
  device	
  
switches	
  language	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

46	
  
Exercise:	
  Texts,	
  Colors	
  and	
  LocalizaDon	
  
•  Create	
  an	
  applicaDon	
  with	
  a	
  few	
  TextViews	
  
with	
  different	
  colors	
  and	
  texts	
  (use	
  XML	
  files).	
  
Make	
  a	
  few	
  localizaDon	
  test	
  (for	
  example	
  fi)	
  
and	
  test	
  localizaDon	
  in	
  emulator.	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

47	
  
6.	
  AcDviDes	
  and	
  User	
  Interface	
  
• 
• 
• 
• 
• 
• 
• 
• 
• 
• 
• 

What	
  is	
  AcDvity?	
  
AcDviDes	
  and	
  tasks	
  
StarDng	
  process	
  and	
  threads	
  
AcDvity	
  lifecycle	
  
Declaring	
  acDvity	
  in	
  the	
  manifest	
  
Basics	
  of	
  the	
  Views	
  and	
  Layouts	
  
Defining	
  layouts	
  with	
  XML	
  
How	
  Android	
  draws	
  a	
  view	
  (AcDvity)	
  
Common	
  Layouts	
  and	
  Controls	
  with	
  examples	
  
Handling	
  UI	
  events	
  
Exercise:	
  Basic	
  UI	
  Controls	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

48	
  
What	
  is	
  AcDvity?	
  
•  ApplicaDon	
  component	
  (user	
  interface)	
  that	
  provides	
  a	
  
screen	
  which	
  user	
  can	
  interact	
  to	
  do	
  something	
  
•  ApplicaDon	
  usually	
  contains	
  a	
  mulDple	
  acDviDes	
  which	
  
makes	
  the	
  whole	
  applicaDon	
  
•  Typically,	
  one	
  acDvity	
  in	
  an	
  applicaDon	
  is	
  specified	
  as	
  the	
  
main	
  acDvity,	
  which	
  is	
  presented	
  to	
  the	
  user	
  when	
  
launching	
  the	
  applicaDon	
  for	
  the	
  first	
  Dme	
  
•  Each	
  acDvity	
  can	
  then	
  start	
  another	
  acDvity	
  in	
  order	
  to	
  
perform	
  different	
  acDons	
  
•  AcDviDes	
  are	
  subclasses	
  from	
  AcDvity	
  class	
  
•  Developer	
  implements	
  callback	
  methods	
  that	
  the	
  system	
  
calls	
  different	
  states	
  of	
  the	
  applicaDon	
  (AcDvity)	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

49	
  
AcDviDes	
  and	
  tasks	
  
•  A	
  task	
  is	
  what	
  the	
  user	
  experiences	
  as	
  an	
  applicaDon	
  
•  Task	
  is	
  like	
  a	
  collecDon	
  of	
  AcDvites	
  what	
  runs	
  in	
  same	
  
applicaDon	
  
•  All	
  acDviDes	
  are	
  set	
  in	
  the	
  stack	
  
•  The	
  root	
  acDvity	
  in	
  the	
  stack	
  is	
  the	
  one	
  that	
  began	
  the	
  task	
  
(the	
  applicaDon	
  launcher)	
  
•  AcDvity	
  at	
  the	
  top	
  of	
  the	
  stack	
  is	
  one	
  that's	
  currently	
  
running	
  
•  The	
  previous	
  acDviDes	
  remains	
  in	
  the	
  stack	
  and	
  are	
  paused/
stopped	
  (acDvated	
  when	
  user	
  presses	
  back	
  bu[on)	
  
•  By	
  default,	
  all	
  the	
  acDviDes	
  in	
  an	
  applicaDon	
  have	
  an	
  
affinity	
  for	
  each	
  other	
  (name	
  of	
  the	
  .apk	
  package	
  name)	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

50	
  
StarDng	
  process	
  and	
  threads	
  
•  When	
  the	
  first	
  of	
  an	
  applicaDon's	
  components	
  needs	
  to	
  be	
  
run,	
  Android	
  starts	
  a	
  Linux	
  process	
  for	
  it	
  with	
  a	
  single	
  
thread	
  of	
  execuDon	
  
•  The	
  process	
  where	
  a	
  component	
  runs	
  is	
  controlled	
  by	
  the	
  
manifest	
  file	
  	
  
•  	
  
Note!	
  
•  Android	
  may	
  decide	
  to	
  shut	
  down	
  a	
  process	
  at	
  some	
  point	
  
•  Everything	
  runs	
  in	
  the	
  main	
  thread	
  (including	
  UI),	
  avoid	
  
long	
  lasDng	
  operaDons	
  
–  Maintain	
  UI	
  responsive	
  
–  Use	
  another	
  thread	
  (AsyncTask)	
  for	
  the	
  long	
  operaDons	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

51	
  
AcDvity	
  lifecycle	
  
•  Three	
  main	
  states:	
  running,	
  	
  
paused	
  and	
  stopped	
  
•  If	
  an	
  acDvity	
  is	
  paused	
  or	
  	
  
stopped,	
  the	
  system	
  can	
  	
  
drop	
  it	
  from	
  memory	
  
•  Do	
  your	
  applicaDon	
  UI,	
  
bind	
  Data	
  sources	
  and	
  event	
  	
  
handlers	
  in	
  onCreate()	
  
•  AMer	
  onResume()	
  acDvity	
  
is	
  visible	
  to	
  end	
  user	
  
•  In	
  onPause()	
  save	
  criDcal	
  
data	
  to	
  applicaDon’s	
  data	
  store	
  
Note:	
  This	
  will	
  be	
  discussed	
  more	
  deeply	
  later…	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

52	
  
Declaring	
  acDvity	
  in	
  the	
  manifest	
  
•  AcDvity	
  has	
  to	
  be	
  declared	
  in	
  the	
  manifest	
  file	
  
•  Use	
  android:name	
  a[ribute	
  to	
  specify	
  the	
  class	
  
name	
  of	
  the	
  acDvity	
  
•  Use	
  other	
  a[ributes	
  to	
  add	
  addiDonal	
  
informaDon	
  of	
  the	
  acDvity	
  (label,	
  icon,	
  theme,	
  …)	
  
•  Use	
  Intent	
  filters	
  if	
  you	
  want	
  to	
  make	
  your	
  
acDvity	
  available	
  to	
  system	
  or	
  other	
  applicaDons	
  
<acDvity	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  android:name="fi.ptm.helloandroid.MainAcPvity”	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  android:label="@string/app_name"	
  >	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  <intent-­‐filter>	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  <acDon	
  android:name="android.intent.acPon.MAIN"	
  />	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  <category	
  android:name="android.intent.category.LAUNCHER"	
  />	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  </intent-­‐filter>	
  
</acDvity>	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

53	
  
Basics	
  of	
  the	
  Views	
  and	
  Layouts	
  
•  Android	
  applicaDon	
  UI	
  is	
  built	
  
using	
  View	
  and	
  ViewGroup	
  
objects	
  
•  Bu[on,	
  Text	
  (and	
  so	
  on	
  UI	
  
objects)	
  are	
  View’s	
  subclasses	
  
and	
  they	
  are	
  called	
  as	
  
”widgets”	
  or	
  ”Views”	
  
•  Layout	
  architectures	
  like	
  
linear,	
  tabular	
  and	
  relaDve	
  are	
  
ViewGroup’s	
  subclasses	
  and	
  
they	
  are	
  called	
  as	
  “Layouts”	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

54	
  
Defining	
  layouts	
  with	
  XML	
  
•  Most	
  common	
  way	
  is	
  to	
  use	
  XML	
  layout	
  for	
  
declaring	
  AcDvity	
  layout	
  
•  Each	
  element	
  in	
  XML	
  is	
  either	
  a	
  View	
  or	
  
ViewGroup	
  object	
  	
  
•  View	
  objects	
  are	
  leaves	
  in	
  the	
  tree,	
  ViewGroup	
  
objects	
  are	
  branches	
  in	
  the	
  tree	
  
•  The	
  name	
  of	
  an	
  XML	
  element	
  is	
  respecDve	
  to	
  the	
  
Java	
  class	
  that	
  it	
  represents	
  like:	
  <TextView>	
  
element	
  creates	
  a	
  TextView	
  in	
  your	
  UI	
  
•  Layout	
  and	
  its	
  elements	
  can	
  be	
  instanDate	
  at	
  
runDme	
  programmaDcally	
  also	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

55	
  
How	
  Android	
  draws	
  a	
  view	
  
•  A[ach	
  the	
  view	
  hierarchy	
  tree	
  to	
  the	
  screen,	
  AcDvity	
  must	
  call	
  the	
  
setContentView()	
  method	
  and	
  pass	
  a	
  reference	
  to	
  the	
  root	
  node	
  
object	
  
•  The	
  draw()	
  method	
  of	
  a	
  View	
  is	
  called	
  
•  Two	
  process:	
  measure	
  and	
  layout	
  pass	
  
•  Mostly	
  used	
  layout	
  params:	
  

–  Exact	
  number	
  
–  match_parent,	
  view	
  wants	
  to	
  be	
  as	
  big	
  as	
  its	
  parent	
  
–  wrap_content,	
  view	
  wants	
  to	
  be	
  just	
  big	
  enough	
  to	
  enclose	
  its	
  content	
  

•  Size,	
  Padding,	
  Margins	
  is	
  used	
  like	
  in	
  HTML	
  
•  Dimensions:	
  dp,	
  sp,	
  pt,	
  px,	
  mm	
  and	
  in	
  
•  Different	
  Views	
  and	
  Layouts	
  has	
  its	
  own	
  subclass	
  of	
  Layout	
  Params	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

56	
  
Common	
  Layouts	
  
•  FrameLayout	
  

–  a	
  blank	
  space	
  on	
  your	
  screen	
  that	
  you	
  can	
  later	
  fill	
  with	
  a	
  single	
  object	
  

•  LinearLayout	
  

–  aligns	
  all	
  children	
  in	
  a	
  single	
  direcDon	
  (verDcally	
  or	
  horizontally)	
  
–  all	
  children	
  are	
  stacked	
  one	
  aMer	
  the	
  other	
  

•  TableLayout	
  

–  posiDons	
  its	
  children	
  into	
  rows	
  and	
  columns	
  
–  doesn’t	
  display	
  border	
  lines	
  for	
  their	
  rows,	
  columns,	
  or	
  cells	
  

•  RelaDveLayout	
  

–  child	
  views	
  specify	
  their	
  posiDon	
  relaDve	
  to	
  the	
  parent	
  view	
  or	
  to	
  each	
  
other	
  

•  More:	
  	
  

–  Gallery,	
  GridView,	
  ListView,	
  ScrollView,	
  Spinner,	
  SurfaceView,	
  
TabHost,	
  ViewFlipper,	
  ViewSwitcher,	
  Fragments,	
  WebView,…	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

57	
  
LinearLayout	
  example	
  
<!–	
  ac:vity_main.xml-­‐-­‐>	
  
<LinearLayout	
  xmlns:android="hTp://schemas.android.com/apk/res/android"	
  
	
  	
  	
  	
  xmlns:tools="hTp://schemas.android.com/tools”>	
  
	
  	
  	
  	
  <TextView	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:id="@+id/textView1"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:layout_width="wrap_content"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:layout_height="wrap_content"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:text="@string/hello_world"	
  />	
  
	
  	
  	
  	
  <Bu[on	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:id="@+id/buTon1"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:layout_width="wrap_content"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:layout_height="wrap_content”	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:text="@string/buTon_string"	
  />	
  
</LinearLayout>	
  
//	
  load	
  acDvity_main.xml	
  
public	
  void	
  onCreate(Bundle	
  savedInstanceState)	
  {	
  
	
  	
  	
  	
  super.onCreate(savedInstanceState);	
  
	
  	
  	
  	
  setContentView(R.layout.ac:vity_main);	
  
}	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

58	
  
RelaDveLayout	
  example	
  
<RelaDveLayout	
  xmlns:android="hTp://schemas.android.com/apk/res/android"	
  
	
  	
  	
  	
  xmlns:tools="hTp://schemas.android.com/tools"	
  
	
  	
  	
  	
  xmlns:ads="hTp://schemas.android.com/apk/lib/com.google.ads"	
  
	
  	
  	
  	
  android:layout_width="match_parent"	
  
	
  	
  	
  	
  android:layout_height="match_parent"	
  
	
  	
  	
  	
  tools:context=".MainAcPvity"	
  >	
  
	
  
	
  	
  	
  	
  <com.google.ads.AdView	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:id="@+id/adView"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:layout_width="match_parent"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:layout_height="50dip"	
  
	
  	
  	
  	
  	
  	
  	
  	
  ads:adUnitId=”xxxxxxx"	
  
	
  	
  	
  	
  	
  	
  	
  	
  ads:adSize="BANNER"	
  
	
  	
  	
  	
  	
  	
  	
  	
  ads:testDevices="TEST_EMULATOR,	
  TEST_DEVICE_ID"	
  
	
  	
  	
  	
  	
  	
  	
  	
  ads:loadAdOnCreate="true"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:layout_alignParentBoOom="true"	
  
	
  	
  	
  	
  	
  	
  	
  	
  />	
  	
  
…	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

59	
  
RelaDveLayout	
  example	
  
…	
  
<TableLayout	
  
	
  
	
  	
  
	
  android:id="@+id/keypad"	
  	
  
	
  android:orientaDon="verPcal"	
  	
  
	
  android:layout_width="fill_parent"	
  	
  
	
  android:layout_height="wrap_content”	
  
	
  android:stretchColumns="*”	
  
	
  android:layout_above="@+id/adView”>	
  	
  
	
  <TableRow>	
  
	
  <Bu[on	
  android:id="@+id/keypad_1"	
  android:text="1"	
  	
  
	
  android:onClick="buTonClicked"></BuTon>	
  
	
  <Bu[on	
  android:id="@+id/keypad_2"	
  android:text=“2"	
  	
  
	
  android:onClick="buTonClicked"></BuTon>	
  
	
  …	
  
	
  </TableRow>	
  	
  
	
  …	
  
</TableLayout>	
  
…	
  	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

60	
  
RelaDveLayout	
  example	
  
…	
  
<HorizontalScrollView	
  
	
  	
  	
  	
  	
  	
  	
  android:id="@+id/scrollView"	
  
	
  	
  	
  	
  	
  	
  	
  android:layout_width="fill_parent"	
  
	
  	
  	
  	
  	
  	
  	
  android:layout_height="75dp"	
  
	
  	
  	
  	
  	
  	
  	
  android:layout_above="@+id/keypad"	
  
	
  	
  	
  	
  	
  	
  	
  android:paddingTop="2dp"	
  
	
  	
  	
  	
  	
  	
  	
  android:paddingBo[om="2dp"	
  
	
  	
  	
  	
  	
  	
  	
  >	
  
	
  	
  	
  	
  	
  	
  	
  <LinearLayout	
  android:id="@+id/linearLayoutOfImages"	
  	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  android:layout_width="fill_parent"	
  	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  android:layout_height="fill_parent"	
  	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  android:orientaDon="horizontal">	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  
	
  	
  	
  	
  	
  	
  	
  </LinearLayout>	
  	
  	
  
	
  	
  	
  </HorizontalScrollView>	
  
…	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

61	
  
RelaDveLayout	
  example	
  
…	
  
<fi.ptm.teksDtv.AspectRaDoImageView	
  	
  
	
  
	
  	
  	
  	
  	
  android:layout_centerHorizontal="true"	
  
	
  
	
  	
  	
  	
  	
  android:id="@+id/imageView"	
  
	
  
	
  	
  	
  	
  	
  android:src="@drawable/ladataan"	
  	
  
	
  
	
  	
  	
  	
  	
  android:layout_alignParentTop="true"	
  	
  
	
  
	
  	
  	
  	
  	
  android:layout_height="wrap_content"	
  
	
  
	
  	
  	
  	
  	
  android:layout_width="match_parent"	
  	
  
	
  
	
  	
  	
  	
  	
  android:adjustViewBounds="true"/>	
  
<TextView	
  	
  
	
  
	
  	
  	
  	
  	
  android:id="@+id/pageTitle"	
  
	
  
	
  	
  	
  	
  	
  android:layout_width="wrap_content"	
  
	
  
	
  	
  	
  	
  	
  android:layout_height="wrap_content"	
  
	
  
	
  	
  	
  	
  	
  android:layout_alignParentLeR="true"	
  
	
  
	
  	
  	
  	
  	
  android:text="100/01"	
  
	
  
	
  	
  	
  	
  	
  android:background="#FFFFFF"	
  
	
  
	
  	
  	
  	
  	
  android:textColor="#000000"/>	
  
	
  </RelaDveLayout>	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

62	
  
Common	
  Input	
  Controls	
  
•  Input	
  controls	
  are	
  the	
  interacDve	
  
components	
  in	
  your	
  applicaDon’s	
  user	
  
interface	
  
•  Android	
  provides	
  a	
  wide	
  variety	
  of	
  
controls	
  to	
  use	
  in	
  your	
  UI	
  

–  Bu[ons,	
  text	
  fields,	
  seek	
  bars,	
  checkboxes,	
  
spinners,	
  pickers	
  and	
  so	
  on…	
  

•  All	
  controls	
  can	
  be	
  found	
  in	
  layout	
  
pale[e	
  
•  Use	
  drag	
  and	
  drop	
  to	
  set	
  controls	
  to	
  
layout	
  or	
  edit	
  your	
  XML	
  file	
  directly	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

63	
  
Handling	
  UI	
  Events	
  
•  Many	
  ways	
  to	
  intercept	
  the	
  events	
  from	
  a	
  
user's	
  interacDon	
  (depends	
  the	
  SDK	
  version)	
  
•  The	
  approach	
  is	
  to	
  capture	
  the	
  events	
  from	
  
the	
  specific	
  View	
  object	
  that	
  the	
  user	
  interacts	
  
with	
  
•  Use	
  different	
  EventListeners	
  with	
  code	
  or	
  in	
  
XML	
  
–  onClick,	
  onLongClick,	
  onFocusChange,	
  …	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

64	
  
Example:	
  Handling	
  UI	
  Events	
  
•  Only	
  with	
  coding	
  
//	
  Create	
  an	
  implementaDon	
  of	
  OnClickListener	
  
private	
  OnClickListener	
  buOon1Listener	
  =	
  new	
  OnClickListener()	
  {	
  
	
  	
  	
  	
  public	
  void	
  onClick(View	
  v)	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  //	
  Do	
  something	
  when	
  the	
  bu[on	
  is	
  clicked	
  
	
  	
  	
  	
  	
  	
  	
  	
  Toast.makeText(getApplicaDonContext(),	
  "Bu[on	
  1	
  clicked",	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  Toast.LENGTH_SHORT).show();	
  
	
  	
  	
  	
  }	
  
};	
  
	
  
@Override	
  
protected	
  void	
  onCreate(Bundle	
  savedInstanceState)	
  {	
  
	
  	
  super.onCreate(savedInstanceState);	
  
	
  	
  setContentView(R.layout.acDvity_main);	
  
	
  	
  //	
  Capture	
  our	
  bu[on	
  from	
  layout	
  
	
  	
  Bu[on	
  buOon1	
  =	
  (Bu[on)findViewById(R.id.bu[on1);	
  
	
  	
  //	
  Register	
  the	
  onClick	
  listener	
  with	
  the	
  implementaDon	
  above	
  
	
  	
  buOon1.setOnClickListener(buOon1Listener);	
  
}	
  

•  With	
  XML	
  and	
  code	
  
<Bu[on	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:id="@+id/bu[on2"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:layout_width="wrap_content"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:layout_height="wrap_content"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:layout_below="@+id/bu[on1"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:layout_centerHorizontal="true"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:layout_marginTop="19dp"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:onClick="buOon2Clicked"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:text="OnClick	
  with	
  XML"	
  />	
  

public	
  void	
  buOon2Clicked(View	
  view)	
  {	
  
	
  	
  Toast.makeText(getApplicaDonContext(),	
  	
  
	
  	
  	
  	
  	
  "Bu[on	
  2	
  clicked",	
  Toast.LENGTH_SHORT).show();	
  
}	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

65	
  
Bu[ons	
  Example	
  
•  Different	
  bu[on	
  types	
  
–  Bu[on	
  with	
  text	
  
–  Bu[on	
  with	
  text	
  and	
  image	
  
–  ImageBu[on	
  with	
  image	
  

•  Responding	
  Click	
  Events	
  
–  onClick	
  
–  OnClickListener	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

66	
  
Text	
  Fields	
  Example	
  
•  Use	
  TextView	
  to	
  show	
  text	
  
•  Use	
  EditText	
  to	
  ask	
  text	
  from	
  the	
  end	
  user	
  
–  automaDcally	
  displays	
  the	
  keyboard	
  
–  different	
  input	
  types	
  (android:inputType)	
  

•  text,	
  textEmailAddress,	
  textUri,	
  number,	
  phone	
  
•  textCapSentences,	
  textCapWords,	
  
textAutoCorrecDon,	
  textPassword,	
  	
  
textMulDline	
  

–  text	
  selecDon	
  

•  cut,	
  copy,	
  paste	
  

–  auto-­‐compleDon	
  

•  use	
  AutoCompleteTextView	
  
•  define	
  the	
  array	
  that	
  contains	
  all	
  text	
  
suggesDons	
  in	
  strings	
  resource	
  
•  create	
  String	
  based	
  Adapter	
  in	
  code	
  and	
  
and	
  use	
  it	
  in	
  your	
  AutoCompleteTextView	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

67	
  
CheckBoxes,	
  Radio	
  and	
  Toggle	
  Bu[ons	
  
Example	
  
•  Checkboxes	
  allow	
  the	
  user	
  to	
  
select	
  one	
  or	
  more	
  opDons	
  
from	
  a	
  set	
  
•  Radio	
  bu[ons	
  allow	
  the	
  user	
  
to	
  select	
  one	
  opDon	
  from	
  a	
  set	
  
•  A	
  toggle	
  bu[on	
  or	
  switch	
  
allows	
  the	
  user	
  to	
  change	
  a	
  
serng	
  between	
  two	
  states	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

68	
  
Spinner	
  Example	
  
•  Provides	
  a	
  quick	
  way	
  to	
  
select	
  one	
  value	
  from	
  a	
  set	
  
•  Touching	
  the	
  spinner	
  displays	
  
a	
  dropdown	
  menu	
  with	
  all	
  
other	
  available	
  value	
  (user	
  
can	
  select	
  one)	
  
•  Define	
  String	
  resource	
  and	
  
create	
  String	
  based	
  Adapter	
  
in	
  code	
  and	
  use	
  it	
  to	
  your	
  
Spinner	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

69	
  
Pickers	
  Example	
  
•  Android	
  provides	
  controls	
  for	
  
the	
  user	
  to	
  pick	
  a	
  Dme	
  or	
  pick	
  
a	
  date	
  as	
  ready-­‐to-­‐use	
  dialogs	
  
•  Using	
  these	
  pickers	
  helps	
  
ensure	
  that	
  your	
  users	
  can	
  
pick	
  a	
  Dme	
  or	
  date	
  that	
  is	
  
valid,	
  forma[ed	
  correctly,	
  
and	
  adjusted	
  to	
  the	
  user's	
  
locale	
  
•  It	
  is	
  recommended	
  to	
  use	
  
DialogFragment	
  to	
  display	
  
Dme	
  or	
  date	
  picker	
  	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

70	
  
Exercise:	
  Basic	
  UI	
  Controls	
  
•  RadioBu[ons	
  and	
  selecDon	
  
•  Different	
  texts	
  controls	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

71	
  
7.	
  Menus,	
  NoDfying	
  and	
  Dialogs	
  
• 

Working	
  with	
  Menus	
  
– 
– 
– 
– 
– 
– 
– 

Using	
  OpDons	
  Menu	
  
Example:	
  OpDons	
  Menu	
  with	
  XML	
  
Contextual	
  Menus	
  
Using	
  Context	
  Menu	
  
Examples	
  with	
  Context	
  Menu	
  
Using	
  Contextual	
  AcDon	
  Mode	
  
Using	
  PopUp	
  Menu	
  

• 

NoDfying	
  the	
  User	
  

• 

NoDfying	
  the	
  user	
  with	
  Dialogs	
  

–  NoDfying	
  with	
  the	
  Toast	
  
–  Example:	
  Display	
  Toasts	
  
–  Using	
  NoDficaDon	
  Area	
  
– 
– 
– 
– 
– 
– 
– 

CreaDng	
  a	
  Dialog	
  
Examples:	
  AlertDialog	
  with	
  Bu[ons	
  and	
  List	
  
Custom	
  Dialog	
  Layouts	
  
Examples:	
  Custom	
  Dialog	
  
Passing	
  Events	
  back	
  to	
  Dialog’s	
  Host	
  
ProgressDialog,	
  ProgressBar	
  and	
  Indicators	
  
Example:	
  ProgressBar	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

72	
  
Working	
  with	
  Menus	
  
•  OpDons	
  Menu	
  

–  primary	
  set	
  of	
  menu	
  items	
  for	
  an	
  AcDvity	
  
–  opened	
  by	
  pressing	
  the	
  device	
  MENU	
  key	
  
–  two	
  groups	
  of	
  menu	
  items:	
  	
  
•  Icon	
  Menu	
  
•  Expanded	
  Menu	
  

•  Context	
  Menu	
  

–  floaDng	
  list	
  of	
  menu	
  items	
  	
  
–  appear	
  when	
  you	
  perform	
  a	
  long-­‐press	
  on	
  a	
  View	
  (like	
  right	
  click	
  in	
  PC)	
  

•  PopUp	
  

–  a	
  modal	
  menu	
  anchored	
  to	
  a	
  View	
  
–  appears	
  below	
  the	
  anchor	
  view	
  if	
  there	
  is	
  room,	
  or	
  above	
  the	
  view	
  
otherwise	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

73	
  
Using	
  OpDons	
  Menu	
  
•  Implemented	
  by	
  onCreateOp:onsMenu()	
  callback,	
  which	
  receives	
  
an	
  instance	
  of	
  Menu	
  
•  Menu	
  can	
  be	
  structured	
  with	
  coding	
  or	
  using	
  XML	
  resource	
  (save	
  to	
  
menu	
  folder)	
  
•  Use	
  XML	
  to	
  get	
  
–  easier	
  to	
  visualize	
  the	
  menu	
  structure	
  	
  
–  separates	
  the	
  content	
  for	
  the	
  menu	
  from	
  your	
  applicaDon's	
  behavioral	
  
code	
  
–  allows	
  you	
  to	
  create	
  alternaDve	
  menu	
  configuraDons	
  for	
  different	
  
pla`orm	
  	
  

•  Menu	
  selecDon	
  will	
  call	
  onOp:onsItemSelected(MenuItem)	
  
funcDon	
  
•  Android	
  3.0	
  (API	
  level	
  11)	
  and	
  higher,	
  items	
  from	
  the	
  opDons	
  menu	
  
can	
  be	
  available	
  in	
  the	
  acDons	
  bar	
  
(android:showAsAcDon=“ifRoom”)	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

74	
  
Example:	
  OpDons	
  Menu	
  with	
  XML	
  
<!–	
  res/menu/main.xml	
  -­‐-­‐>	
  
<menu	
  	
  
	
  	
  	
  xmlns:android="hTp://schemas.android.com/apk/res/android"	
  >	
  
	
  	
  	
  	
  <item	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:id="@+id/acPon_video"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:orderInCategory="100"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:showAsAcDon="ifRoom"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:icon="@drawable/video"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:Dtle="@string/acPon_video"/>	
  
	
  	
  	
  <item	
  …	
  
	
  	
  	
  <item	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:id="@+id/acPon_quit"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:orderInCategory="100"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:showAsAcDon="never"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:Dtle="@string/acPon_quit"/>	
  
/*	
  Create	
  menu	
  items	
  */	
  
public	
  boolean	
  onCreateOpDonsMenu(Menu	
  menu)	
  {	
  
	
  	
  	
  	
  	
  //	
  load	
  menu	
  from	
  resources	
  
	
  	
  	
  	
  	
  MenuInflater	
  inflater	
  =	
  getMenuInflater();	
  
	
  	
  	
  	
  	
  inflater.inflate(R.menu.main,	
  menu);	
  
	
  	
  	
  	
  	
  return	
  true;	
  
}	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

75	
  
Example:	
  OpDons	
  Menu	
  with	
  XML	
  
•  Use	
  Resource	
  Id’s	
  in	
  onOpDonsItemSelected()	
  
	
  	
  	
  /**	
  Handle	
  item	
  selecDons	
  */	
  
	
  	
  	
  	
  public	
  boolean	
  onOpDonsItemSelected(MenuItem	
  item)	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  switch	
  (item.getItemId())	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  case	
  R.id.ac:on_music:	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  Toast.makeText(getBaseContext(),	
  "Music",	
  Toast.LENGTH_SHORT).show();	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  return	
  true;	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  case	
  R.id.ac:on_video:	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  Toast.makeText(getBaseContext(),	
  “Video",	
  Toast.LENGTH_SHORT).show();	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  return	
  true;	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  case	
  R.id.ac:on_quit:	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  Toast.makeText(getBaseContext(),	
  "Quit",	
  Toast.LENGTH_SHORT).show();	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  return	
  true;	
  
	
  	
  	
  	
  	
  	
  	
  	
  }	
  
	
  	
  	
  	
  	
  	
  	
  	
  return	
  false;	
  
	
  	
  	
  	
  }	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

76	
  
Contextual	
  Menus	
  
•  Offers	
  acDons	
  that	
  affect	
  a	
  specific	
  item	
  or	
  
context	
  frame	
  in	
  the	
  UI	
  
•  OMen	
  used	
  with	
  List	
  or	
  Grid	
  Views	
  
•  Two	
  different	
  ways	
  to	
  use	
  
–  floaDng	
  context	
  menu	
  (list	
  of	
  menu	
  items	
  showed	
  
when	
  user	
  performs	
  a	
  long	
  press),	
  one	
  selecDon	
  
–  contextual	
  acDon	
  mode	
  	
  (Android	
  3.0	
  or	
  higher),	
  	
  
with	
  mulDple	
  selecDon	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

77	
  
Using	
  Context	
  Menu	
  
•  Override	
  the	
  AcDvity's	
  context	
  menu	
  callback	
  
methods:	
  
– 

onCreateContextMenu(ContextMenu,View,Conte
xtMenuInfo)	
  
–  onContextItemSelected(MenuItem)	
  

•  Use	
  registerForContextMenu()	
  method	
  to	
  
register	
  ContextMenu	
  to	
  View	
  
•  Menu	
  can	
  be	
  structured	
  with	
  coding	
  or	
  using	
  
XML	
  resource	
  (save	
  to	
  menu	
  folder)	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

78	
  
Example:	
  Context	
  Menu	
  
•  Populate	
  ListView	
  with	
  ArrayAdapter	
  
•  AcDvity	
  should	
  extend	
  ListAcDvity	
  

ListView	
  in	
  Layout	
  

//	
  create	
  string	
  array	
  
private	
  String	
  []	
  items=	
  {"John	
  Koch","Peter	
  Michell","Ina	
  kudson”};	
  
	
  
@Override	
  
public	
  void	
  onCreate(Bundle	
  savedInstanceState)	
  {	
  
	
  	
  	
  	
  setListAdapter(new	
  ArrayAdapter<String>	
  (	
  
	
  	
  	
  	
  	
  	
  	
  	
  this,	
  
	
  	
  	
  	
  	
  	
  	
  	
  android.R.layout.simple_list_item_1,	
  
	
  	
  	
  	
  	
  	
  	
  	
  items));	
  
	
  	
  	
  	
  	
  	
  	
  	
  //	
  register	
  contextMenu	
  for	
  this	
  ListAcDvity	
  
	
  	
  	
  	
  	
  	
  	
  	
  registerForContextMenu(getListView());	
  
	
  	
  	
  	
  }	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

79	
  
Example:	
  Context	
  Menu	
  
•  Context	
  Menu	
  menu	
  items	
  can	
  be	
  added	
  like	
  
in	
  OpDons	
  Menu	
  (coding	
  or	
  XML)	
  

Long	
  press	
  here	
  

<?xml	
  version="1.0"	
  encoding="u/-­‐8"?>	
  
<menu	
  …>	
  
	
  	
  	
  	
  <item	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:id="@+id/acPon_call"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:orderInCategory="100"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:Dtle="@string/acPon_call"/>	
  
	
  	
  	
  	
  <item	
  	
  ….	
  	
  	
  	
  	
  	
  	
  
</menu>	
  
@Override	
  
public	
  void	
  onCreateContextMenu(ContextMenu	
  menu,	
  View	
  v,	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  ContextMenuInfo	
  menuInfo)	
  {	
  
	
  	
  	
  	
  super.onCreateContextMenu(menu,	
  v,	
  menuInfo);	
  
	
  	
  	
  	
  MenuInflater	
  inflater	
  =	
  getMenuInflater();	
  
	
  	
  	
  	
  inflater.inflate(R.menu.context_menu,	
  menu);	
  
}	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

80	
  
Example	
  Context	
  Menu	
  
•  Get	
  the	
  selecDon	
  with	
  onContextItemSelected	
  
@Override	
  
public	
  boolean	
  onContextItemSelected(MenuItem	
  item)	
  {	
  
	
  	
  	
  	
  //	
  context	
  menu	
  item	
  
	
  	
  	
  	
  AdapterContextMenuInfo	
  info	
  =	
  (AdapterContextMenuInfo)	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  item.getMenuInfo();	
  
	
  	
  	
  	
  //	
  text	
  view	
  in	
  list	
  
	
  	
  	
  	
  TextView	
  textView	
  =	
  (TextView)	
  info.targetView;	
  
	
  	
  	
  	
  //	
  name	
  in	
  textview	
  
	
  	
  	
  	
  String	
  name	
  =	
  textView.getText().toString();	
  
	
  	
  	
  	
  //	
  acDon	
  
	
  	
  	
  	
  switch	
  (item.getItemId())	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  case	
  R.id.acPon_call:	
  
	
  	
  	
  	
  	
  	
  	
  	
  Toast.makeText(getBaseContext(),	
  "Call	
  to	
  "+name,	
  Toast.LENGTH_SHORT).show();	
  
	
  	
  	
  	
  	
  	
  	
  	
  return	
  true;	
  
...	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

81	
  
Using	
  Contextual	
  AcDon	
  Mode	
  
•  In	
  specific	
  view	
  
–  implement	
  AcDonMode.Callback	
  
interface	
  
•  specify	
  acDons	
  with	
  XML	
  resource	
  
(menu)	
  
•  respond	
  click	
  events	
  on	
  acDon	
  
items	
  
•  handle	
  prepare	
  and	
  destroy	
  events	
  

–  call	
  startAcDonMode	
  to	
  show	
  
the	
  acDon	
  

•  Example	
  in	
  source	
  codes	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

82	
  
Using	
  PopUp	
  Menu 	
  	
  
•  Define	
  menu	
  with	
  XML	
  
•  Call	
  PopUp	
  constructor	
  and	
  
set	
  menu	
  item	
  click	
  listener	
  
•  Show	
  PopUp	
  menu	
  
•  Handle	
  selecDon	
  with	
  
onMenuItemClick	
  
•  Example	
  in	
  source	
  codes	
  
Note:	
  This	
  is	
  not	
  the	
  same	
  as	
  a	
  context	
  menu,	
  which	
  is	
  
generally	
  for	
  acPons	
  that	
  affect	
  selected	
  content.	
  For	
  acPons	
  
that	
  affect	
  selected	
  content,	
  use	
  the	
  contextual	
  acPon	
  mode	
  
or	
  floaPng	
  context	
  menu.	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

83	
  
NoDfying	
  the	
  User	
  
•  It	
  is	
  good	
  to	
  noDfy	
  the	
  user	
  about	
  an	
  event	
  that	
  
occurs	
  in	
  your	
  applicaDon	
  
–  Saving	
  a	
  file	
  is	
  complete	
  
–  ApplicaDon	
  is	
  running	
  in	
  the	
  background	
  and	
  needs	
  
response	
  from	
  user	
  
–  ApplicaDon	
  is	
  performing	
  work	
  that	
  the	
  user	
  must	
  
wait	
  for	
  

•  Android	
  offers	
  a	
  few	
  basic	
  techniques:	
  
–  Toast	
  NoDficaDon	
  	
  
–  Using	
  NoDficaDon	
  Area	
  
–  Dialog	
  NoDficaDon	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

84	
  
NoDfying	
  the	
  user	
  with	
  Toast	
  
•  A	
  toast	
  noDficaDon	
  is	
  a	
  message	
  that	
  pops	
  up	
  on	
  the	
  
window	
  
•  Fills	
  the	
  amount	
  of	
  space	
  required	
  for	
  the	
  message	
  
•  User's	
  current	
  acDvity	
  remains	
  visible	
  and	
  interac:ve	
  
•  NoDficaDon	
  automaDcally	
  fades	
  in	
  and	
  out	
  
•  Can	
  be	
  	
  
–  fired	
  from	
  background	
  service	
  
–  posiDoned	
  differently	
  
–  customized	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

85	
  
Example:	
  Display	
  Toasts	
  
//	
  get	
  applicaDon	
  context	
  
Context	
  context	
  =	
  getApplicaDonContext();	
  
//	
  toast	
  message	
  
CharSequence	
  text	
  =	
  "Long	
  Toast";	
  
//	
  duraDon	
  
int	
  duraDon	
  =	
  Toast.LENGTH_SHORT;	
  
//	
  create	
  toast	
  
Toast	
  toast	
  =	
  Toast.makeText(context,text,duraPon);	
  
//	
  show	
  toast	
  
toast.show();	
  
	
  	
  
//	
  or	
  
Toast.makeText(context,text,duraDon).show();	
  
	
  
//	
  posiDon	
  top	
  leM	
  corner	
  
toast.setGravity(Gravity.BOTTOM|Gravity.LEFT,	
  0,	
  0);	
  
	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

86	
  
Example:	
  Display	
  Custom	
  Toast	
  
//	
  toast.xml	
  –	
  layout	
  file	
  
<?xml	
  version="1.0"	
  encoding="UTF-­‐8"?>	
  
<LinearLayout	
  …	
  
	
  	
  	
  	
  android:id="@+id/toast”	
  …	
  >	
  
	
  	
  	
  	
  <ImageView	
  android:id="@+id/image”	
  …	
  />	
  
	
  	
  	
  	
  <TextView	
  android:id="@+id/text”	
  …/>	
  
</LinearLayout>	
  

	
   get	
  layout	
  from	
  AcDvity	
  
//	
  

•  Make	
  layout	
  
•  Get	
  layout	
  from	
  
AcDvity	
  
•  Set	
  Image	
  and	
  Text	
  
•  Show	
  toast	
  

LayoutInflater	
  inflater	
  =	
  getLayoutInflater();	
  
//	
  inflate	
  layout	
  from	
  XML	
  (toast.xml)	
  
View	
  layout	
  =	
  inflater.inflate(R.layout.toast,	
  (ViewGroup)	
  findViewById(R.id.toast));	
  
//	
  get	
  image	
  from	
  resource	
  
ImageView	
  image	
  =	
  (ImageView)	
  layout.findViewById(R.id.image);	
  
image.setImageResource(R.drawable.ptm);	
  
//	
  set	
  text	
  
TextView	
  text	
  =	
  (TextView)	
  layout.findViewById(R.id.text);	
  
text.setText("This	
  is	
  a	
  custom	
  PTM	
  toast!");	
  
//	
  create	
  and	
  show	
  toast	
  
Toast	
  toast	
  =	
  new	
  Toast(getApplicaDonContext());	
  
toast.setGravity(Gravity.CENTER_VERTICAL,	
  0,	
  0);	
  
toast.setDuraDon(Toast.LENGTH_LONG);	
  
toast.setView(layout);	
  
toast.show();	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

87	
  
Using	
  NoDficaDon	
  Area	
  
•  Adds	
  an	
  icon	
  to	
  the	
  system's	
  status	
  bar	
  and	
  an	
  expanded	
  
message	
  in	
  the	
  NoDficaDons	
  window	
  
•  Configurable:	
  alert	
  the	
  user	
  with	
  a	
  sound,	
  vibraDon	
  or	
  
flashing	
  light	
  in	
  device	
  
•  Own	
  layout	
  can	
  be	
  used	
  like	
  in	
  Toast	
  NoDficaDons	
  
•  Ideal	
  when	
  applicaDon	
  is	
  working	
  in	
  a	
  background	
  Service	
  
and	
  needs	
  to	
  no:fy	
  the	
  user	
  about	
  an	
  event	
  
•  When	
  the	
  user	
  selects	
  the	
  expanded	
  message,	
  Android	
  
fires	
  an	
  Intent	
  that	
  is	
  defined	
  by	
  the	
  noDficaDon	
  (usually	
  to	
  
launch	
  an	
  AcDvity)	
  
•  Design	
  guidelines	
  
–  h[p://developer.android.com/design/pa[erns/
noDficaDons.html	
  	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

88	
  
Example:	
  NoDficaDon	
  Area	
  
•  Create	
  noDficaDon	
  from	
  a	
  Bu[on	
  
•  Open	
  AcDvity	
  from	
  noDficaDon	
  
•  Programming	
  
–  Use	
  NoDficaDonCombat.Builder	
  to	
  
set	
  icon,	
  Dtle	
  and	
  text	
  of	
  noDficaDon	
  
–  Use	
  TaskStackBuilder	
  and	
  
PendingIntent	
  to	
  start	
  AcDvity	
  when	
  
user	
  clicks	
  noDficaDon	
  
–  Use	
  NoDficaDonManager	
  to	
  lauch	
  
noDficaDon	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

89	
  
NoDfying	
  the	
  user	
  with	
  Dialogs	
  
•  Usually	
  appears	
  in	
  front	
  of	
  the	
  current	
  AcDvity	
  
•  Underlying	
  AcDvity	
  loses	
  focus	
  
•  Ask	
  user	
  to	
  make	
  a	
  decision	
  or	
  enter	
  addiDonal	
  
informaDon	
  
•  How	
  to	
  

–  use	
  DialogFragment	
  as	
  a	
  container	
  of	
  your	
  dialog	
  
–  use	
  AlertDialog	
  as	
  a	
  style	
  and	
  structure	
  of	
  your	
  dialog	
  
–  use	
  DatePickerDialog	
  or	
  TimePickerDialog	
  in	
  special	
  case	
  
–  Dialog	
  class	
  is	
  base	
  class	
  behind	
  all	
  dialogs	
  

•  Design	
  guidelines	
  

–  h[p://developer.android.com/design/building-­‐blocks/
dialogs.html	
  	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

90	
  
CreaDng	
  a	
  Dialog	
  
•  Extend	
  your	
  own	
  class	
  from	
  DialogFragment	
  
•  Create	
  for	
  example	
  AlertDialog	
  in	
  
onCreateDialog()	
  method	
  
•  Create	
  instance	
  of	
  your	
  class	
  and	
  call	
  show()	
  
method	
  
•  AlertDialog’s	
  bu[on	
  touches	
  will	
  automaDcally	
  
dismiss	
  the	
  dialog	
  for	
  you	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

91	
  
Example:	
  AlertDialog	
  with	
  Bu[ons	
  
•  Three	
  regions	
  
–  Title,	
  Content	
  area	
  and	
  AcDon	
  bu[ons	
  
public	
  class	
  ExitDialogFragment	
  extends	
  DialogFragment	
  {	
  	
  
	
  	
  	
  @Override	
  
	
  	
  	
  	
  public	
  Dialog	
  onCreateDialog(Bundle	
  savedInstanceState)	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  AlertDialog.Builder	
  builder	
  =	
  new	
  AlertDialog.Builder(getAcDvity());	
  
	
  	
  	
  	
  	
  	
  	
  	
  builder.setTitle(R.string.dialog_Ptle)	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  .setMessage(R.string.dialog_exit)	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  .setPosiDveBu[on(R.string.dialog_yes,	
  new	
  DialogInterface.OnClickListener()	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  public	
  void	
  onClick(DialogInterface	
  dialog,	
  int	
  id)	
  {	
  //	
  Close	
  applicaDon	
  }	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  })	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  .setNegaDveBu[on(R.string.dialog_cancel,	
  new	
  DialogInterface.OnClickListener()	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  public	
  void	
  onClick(DialogInterface	
  dialog,	
  int	
  id)	
  {	
  //	
  User	
  cancelled	
  the	
  dialog	
  }	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  });	
  
	
  	
  	
  	
  	
  	
  	
  	
  //	
  Create	
  the	
  AlertDialog	
  object	
  and	
  return	
  it	
  
	
  	
  	
  	
  	
  	
  	
  	
  return	
  builder.create();	
  
	
  	
  	
  	
  }	
  
public	
  void	
  exitDialog(View	
  view)	
  {	
  
}	
  
	
  	
  	
  	
  ExitDialogFragment	
  eDialog	
  =	
  new	
  ExitDialogFragment();	
  
	
  	
  	
  	
  eDialog.show(getFragmentManager(),	
  "exit");	
  
}	
  

	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

92	
  
Example:	
  AlertDialog	
  with	
  List	
  
•  Create	
  list	
  in	
  String	
  resources	
  
public	
  class	
  ListDialogFragment	
  extends	
  DialogFragment	
  {	
  
	
  	
  @Override	
  
	
  	
  public	
  Dialog	
  onCreateDialog(Bundle	
  savedInstanceState)	
  {	
  
	
  	
  	
  	
  	
  	
  AlertDialog.Builder	
  builder	
  =	
  new	
  AlertDialog.Builder(getAcDvity());	
  
	
  	
  	
  	
  	
  	
  builder.setTitle(R.string.listdialog_Dtle)	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  .setItems(R.array.messagetypes,	
  new	
  DialogInterface.OnClickListener()	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  public	
  void	
  onClick(DialogInterface	
  dialog,	
  int	
  index)	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  Resources	
  res	
  =	
  getResources();	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  String[]	
  messageTypes	
  =	
  res.getStringArray(R.array.messagetypes);	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  Toast.makeText(getAcDvity(),	
  "Selected	
  messageType	
  =	
  "	
  +	
  messageTypes[index],	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  Toast.LENGTH_SHORT).show();	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  }	
  
	
  	
  	
  	
  	
  	
  });	
  
	
  	
  	
  	
  	
  	
  return	
  builder.create();	
  
	
  	
  	
  	
  	
  }	
  
}	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

93	
  
Custom	
  Dialog	
  Layouts	
  
•  Create	
  your	
  own	
  layout	
  for	
  the	
  dialog	
  window	
  
with	
  layout	
  and	
  widget	
  elements	
  
•  Add	
  layout	
  to	
  AlertDialog	
  with	
  setView()	
  
method	
  
•  Custom	
  layout	
  fills	
  the	
  dialog,	
  use	
  
AlertDialog.builder	
  to	
  add	
  bu[ons	
  	
  
•  Note	
  
–  Normal	
  AcDvity	
  can	
  be	
  used	
  as	
  a	
  Dialog	
  also	
  
<acDvity	
  android:theme="@android:style/Theme.Holo.Dialog"	
  >	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

94	
  
Example:	
  Custom	
  Dialog	
  
public	
  class	
  TeamDialogFragment	
  extends	
  DialogFragment	
  {	
  
	
  	
  @Override	
  
	
  	
  public	
  Dialog	
  onCreateDialog(Bundle	
  savedInstanceState)	
  {	
  
	
  	
  	
  	
  AlertDialog.Builder	
  builder	
  =	
  new	
  AlertDialog.Builder(getAcDvity());	
  
	
  	
  	
  	
  //	
  get	
  the	
  layout	
  inflater	
  
	
  	
  	
  	
  LayoutInflater	
  inflater	
  =	
  getAcDvity().getLayoutInflater();	
  
	
  	
  	
  	
  //	
  Inflate	
  and	
  set	
  the	
  layout	
  for	
  the	
  dialog	
  
	
  	
  	
  	
  builder.setView(inflater.inflate(R.layout.add_team_dialog,	
  null))	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  .setTitle("Add	
  a	
  new	
  Team")	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  .setPosiDveBu[on("Add",	
  new	
  DialogInterface.OnClickListener()	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  @Override	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  public	
  void	
  onClick(DialogInterface	
  dialog,	
  int	
  id)	
  {	
  //	
  Add	
  a	
  team…	
  }	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  })	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  .setNegaDveBu[on("Cancel",	
  new	
  DialogInterface.OnClickListener()	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  public	
  void	
  onClick(DialogInterface	
  dialog,	
  int	
  id)	
  {	
  	
  }	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  });	
  	
  	
  	
  	
  	
  	
  
	
  	
  	
  	
  	
  return	
  builder.create();	
  
	
  	
  	
  	
  	
  }	
  
}	
  
//	
  add_team_dialog.xml	
  
<RelaDveLayout…	
  
	
  	
  	
  <TextView…	
  
	
  	
  	
  <EditText…	
  

	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

95	
  
Passing	
  events	
  back	
  to	
  dialog’s	
  host	
  
•  Dialog	
  might	
  perform	
  the	
  necessary	
  
acDon	
  itself	
  (oMen	
  you	
  want	
  to	
  send	
  
informaDon	
  back	
  to	
  the	
  AcDvity	
  which	
  
opened	
  the	
  dialog)	
  
–  Define	
  an	
  interface	
  with	
  a	
  method	
  for	
  
each	
  type	
  of	
  click	
  event	
  
–  Implement	
  interface	
  in	
  the	
  host	
  
component	
  that	
  will	
  receive	
  the	
  acDon	
  
events	
  from	
  the	
  dialog	
  
–  Override	
  DialogFrament’s	
  onA[ach()-­‐
method	
  to	
  instanDate	
  your	
  listener	
  
(send	
  events	
  to	
  host)	
  

	
  

//	
  look	
  DialogsExample	
  in	
  CustomDialog	
  project	
  (Custom	
  Dialog,	
  Add	
  a	
  new	
  Team)	
  

	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

96	
  
ProgressDialog	
  
•  ProgressDialog	
  is	
  an	
  extension	
  of	
  AlertDialog	
  class	
  
•  Use	
  in	
  following	
  cases:	
  

–  display	
  a	
  progress	
  animaDon	
  (a	
  spinning	
  wheel	
  or	
  a	
  
progress	
  bar)	
  

•  ProgressDialog	
  can	
  also	
  provide	
  bu[ons,	
  such	
  as	
  
one	
  to	
  cancel	
  a	
  download	
  
•  To	
  show	
  ProgressDialog	
  call	
  simply	
  
ProgressDialog.show()	
  
	
  
Note!	
  

–  Android	
  Progress	
  &	
  AcDvity	
  guidelines	
  says	
  ”avoid”	
  
for	
  ProgressDialog	
  use	
  
–  You	
  should	
  use	
  ProgressBar	
  instead	
  (inside	
  your	
  
acDvity)	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

97	
  
ProgressBar	
  and	
  Indicators	
  
•  Use	
  Progress	
  bars	
  and	
  acDvity	
  indicators	
  to	
  
signal	
  to	
  users	
  that	
  something	
  is	
  happening	
  
•  A	
  progress	
  bar	
  should	
  always	
  fill	
  from	
  0%	
  to	
  
100%	
  and	
  never	
  move	
  backwards	
  to	
  a	
  lower	
  
value	
  
•  Design	
  guidelines:	
  
–  h[p://developer.android.com/design/building-­‐
blocks/progress.html	
  	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

98	
  
Example:	
  ProgressBar	
  
•  Sample	
  layout	
  with	
  ProgressBar	
  
•  Show	
  also	
  systems	
  ProgressBar	
  (circle)	
  
•  Simulate	
  loading	
  with	
  Thread	
  
@Override	
  
protected	
  void	
  onCreate(Bundle	
  savedInstanceState)	
  {	
  
	
  	
  super.onCreate(savedInstanceState);	
  
	
  	
  //	
  acDvate	
  indeterminate	
  progress	
  
	
  	
  getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS);	
  
	
  	
  //	
  set	
  layout	
  for	
  the	
  acDvity	
  
	
  	
  setContentView(R.layout.acDvity_main);
	
  
	
  	
  
	
  	
  //	
  get	
  progressbar	
  
	
  	
  progressBar	
  =	
  (ProgressBar)	
  findViewById(R.id.progressBar1);	
  
}	
  
//	
  start	
  "loading...”	
  
	
  
public	
  void	
  startBu[onPressed(View	
  view)	
  {	
  
	
  	
  MainAcDvity.this.setProgressBarIndeterminateVisibility(true);	
  
	
  	
  startLoading();	
  
}	
  

	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

99	
  
8.	
  Understanding	
  of	
  the	
  Intents	
  
•  Intents	
  
•  Intent	
  Object	
  

–  AcDons,	
  Data,	
  Category,	
  Extras	
  

• 
• 
• 
• 
• 
• 
• 
• 
• 

Intent	
  ResoluDon,	
  Filters,	
  Matching	
  
Intent	
  AcDons,	
  Data	
  and	
  Category	
  test	
  
Example:	
  Intent	
  Filters	
  in	
  Manifest	
  
Using	
  Explicit	
  Intent	
  to	
  start	
  a	
  new	
  AcDvity	
  
Data	
  transfer	
  between	
  AcDviDes	
  
ExpecDng	
  a	
  result	
  from	
  an	
  AcDvity	
  
Using	
  Implicit	
  Intents	
  
Custom	
  and	
  Broadcast	
  Intents	
  with	
  Receivers	
  
Examples	
  and	
  Exercises	
  with	
  AcDviDes	
  and	
  Intents	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

100	
  
Intents	
  
•  The	
  most	
  unique	
  and	
  important	
  concept	
  in	
  
Android	
  Development	
  
•  Intent	
  can	
  be	
  used	
  to	
  
–  broadcast	
  data	
  between	
  applicaDons	
  and	
  
applicaDon	
  components	
  
–  start	
  AcDviDes	
  and	
  Services	
  
–  broadcast	
  that	
  an	
  event	
  or	
  acDon	
  has	
  occured	
  

•  Intent	
  is	
  like	
  a	
  ”message”	
  to	
  someone	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

101	
  
Intent	
  Object	
  
•  An	
  Intent	
  object	
  is	
  a	
  bundle	
  of	
  informaDon	
  
•  Object	
  contains:	
  

–  Component,	
  specifies	
  an	
  explicit	
  name	
  of	
  a	
  component	
  class	
  to	
  use	
  
for	
  the	
  intent	
  
–  Ac:on,	
  acDon	
  to	
  be	
  performed	
  (Androids	
  own	
  or	
  your	
  own	
  defined)	
  
–  Data,	
  the	
  URI	
  of	
  the	
  data	
  to	
  be	
  acted	
  on	
  and	
  the	
  MIME	
  type	
  of	
  that	
  
data	
  
–  Category,	
  a	
  string	
  containing	
  addiDonal	
  informaDon	
  about	
  the	
  kind	
  of	
  
component	
  that	
  should	
  handle	
  the	
  intent	
  
–  Type,	
  specifies	
  an	
  explicit	
  type	
  (a	
  MIME	
  type)	
  of	
  the	
  intent	
  data	
  
–  Extras,	
  key-­‐value	
  pairs	
  for	
  addiDonal	
  informaDon	
  that	
  should	
  be	
  
delivered	
  to	
  the	
  component	
  handling	
  the	
  intent	
  
–  Flags,	
  instruct	
  the	
  Android	
  system	
  how	
  to	
  launch	
  an	
  acDvity	
  and	
  how	
  
to	
  treat	
  it	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

102	
  
Intent	
  Object	
  AcDons	
  
• 
• 
• 
• 
• 
• 
• 
• 
• 
• 
• 
• 
• 
• 
• 

ACTION_MAIN	
  
ACTION_VIEW	
  
ACTION_ATTACH_DATA	
  
ACTION_EDIT	
  
ACTION_PICK	
  
ACTION_CHOOSER	
  
ACTION_GET_CONTENT	
  
ACTION_DIAL	
  
ACTION_CALL	
  
ACTION_SEND	
  
ACTION_SENDTO	
  
ACTION_ANSWER	
  
ACTION_INSERT	
  
ACTION_DELETE	
  
ACTION_RUN	
  

• 
• 
• 
• 
• 
• 
• 
• 
• 
• 
• 
• 
• 
• 
• 

ACTION_TIME_TICK	
  
ACTION_TIME_CHANGED	
  
ACTION_TIMEZONE_CHANGED	
  
ACTION_BOOT_COMPLETED	
  
ACTION_PACKAGE_ADDED	
  
ACTION_PACKAGE_CHANGED	
  
ACTION_PACKAGE_REMOVED	
  
ACTION_PACKAGE_RESTARTED	
  
ACTION_SYNC	
  
ACTION_PICK_ACTIVITY	
  
ACTION_SEARCH	
  
ACTION_WEB_SEARCH	
  
ACTION_FACTORY_TEST	
  
…	
  
fi.ptm.project.SHOW_COLOR	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

103	
  
Intent	
  Object	
  Data	
  
•  Some	
  examples	
  of	
  acDon/data	
  pairs	
  
–  ACTION_VIEW	
  content://contacts/people/1	
  
–  ACTION_DIAL	
  content://contacts/people/1	
  	
  
–  ACTION_VIEW	
  tel:123	
  
–  ACTION_DIAL	
  tel:123	
  
–  ACTION_EDIT	
  content://contacts/people/1	
  
–  ACTION_VIEW	
  content://contacts/people/	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

104	
  
Intent	
  Object	
  Category	
  
•  Any	
  number	
  of	
  category	
  descripDons	
  can	
  be	
  
placed	
  in	
  an	
  Intent	
  object	
  
•  Gives	
  addiDonal	
  informaDon	
  about	
  the	
  acDon	
  to	
  
execute	
  
• 
• 
• 
• 
• 
• 
• 

CATEGORY_DEFAULT	
  
CATEGORY_BROWSABLE	
  
CATEGORY_TAB	
  
CATEGORY_ALTERNATIVE	
  
CATEGORY_SELECTED_ALTERNATIVE	
  
CATEGORY_LAUNCHER	
  
CATEGORY_INFO	
  

• 
• 
• 
• 
• 
• 
• 

CATEGORY_HOME	
  
CATEGORY_PREFERENCE	
  
CATEGORY_TEST	
  
CATEGORY_CAR_DOCK	
  
CATEGORY_DESK_DOCK	
  
CATEGORY_CAR_MODE	
  
…	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

105	
  
Intent	
  Object	
  Extras	
  
•  Extras	
  is	
  a	
  Bundle	
  of	
  any	
  addiDonal	
  
informaDon	
  
•  Used	
  to	
  provide	
  extended	
  informaDon	
  to	
  the	
  
component	
  
Intent	
  intent	
  =	
  new	
  Intent(Intent.ACTION_SENDTO);	
  
intent.setData(Uri.parse("sms://"));	
  	
  	
  	
   	
  	
  
intent.putExtra("address",	
  "04012345678");	
  	
  	
  	
   	
  	
  
intent.putExtra("sms_body",	
  "Your	
  message	
  here");	
  	
  	
  	
  
startAcDvity(intent);	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

	
  	
  

106	
  
Intent	
  ResoluDon	
  
•  Intents	
  can	
  be	
  divided	
  into	
  two	
  groups	
  
–  Explicit	
  intents,	
  are	
  typically	
  used	
  for	
  applicaDon-­‐internal	
  
messages	
  (between	
  AcDviDes)	
  
–  Implicit	
  intents,	
  are	
  oMen	
  used	
  to	
  acDvate	
  components	
  in	
  
other	
  applicaDons	
  

•  Android	
  delivers	
  an	
  explicit	
  intent	
  to	
  an	
  instance	
  of	
  the	
  
designated	
  target	
  class	
  
•  Implicit	
  intents	
  are	
  resolved	
  by	
  Intent	
  Filters	
  by	
  Intent	
  
object’s	
  AcDon,	
  Data	
  and	
  Category	
  	
  
•  In	
  implicit	
  intents,	
  Android	
  system	
  must	
  find	
  the	
  best	
  
component	
  (or	
  components)	
  to	
  handle	
  the	
  intent	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

107	
  
Intent	
  Filters	
  
•  An	
  explicit	
  intent	
  is	
  always	
  delivered	
  to	
  its	
  target,	
  no	
  
ma[er	
  what	
  it	
  contains	
  
•  AcDviDes,	
  services,	
  and	
  broadcast	
  receivers	
  can	
  have	
  
one	
  or	
  more	
  intent	
  filters	
  
•  Each	
  filter	
  describes	
  a	
  capability	
  of	
  the	
  component,	
  a	
  
set	
  of	
  intents	
  that	
  the	
  component	
  is	
  willing	
  to	
  receive	
  
•  Component	
  has	
  separate	
  filters	
  for	
  each	
  job	
  it	
  can	
  do	
  
•  Intent	
  Filters	
  are	
  set	
  up	
  in	
  the	
  applica:on's	
  manifest	
  
file	
  (AndroidManifest.xml)	
  as	
  <intent-­‐filter>	
  elements	
  	
  
•  An	
  implicit	
  intent	
  is	
  tested	
  against	
  acDon,	
  data	
  and	
  
category	
  fields	
  in	
  manifest	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

108	
  
Intent	
  Matching	
  
•  Intents	
  are	
  matched	
  against	
  intent	
  filters	
  not	
  
only	
  to	
  discover	
  a	
  target	
  component	
  to	
  acDvate,	
  
but	
  also	
  to	
  discover	
  something	
  about	
  the	
  set	
  of	
  
components	
  on	
  the	
  device	
  
•  Examples:	
  
•  	
  
AcDvity	
  is	
  shown	
  in	
  applicaDon	
  launcher	
  (”menu	
  in	
  screen”)	
  
–  android.intent.acDon.MAIN	
  
–  android.intent.category.LAUNCHER"	
  

•  AcDvity	
  handles	
  widgets	
  Update	
  (Dme	
  based)	
  and	
  two	
  own	
  
“Receiver”	
  and	
  “Reload”	
  Intents	
  
–  android.appwidget.acDon.APPWIDGET_UPDATE	
  
–  fi.ptm.namedays.ACTION_WIDGET_RECEIVER	
  
–  fi.ptm.namedays.ACTION_WIDGER_RELOAD	
  
	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

109	
  
Intent	
  Filters	
  –	
  AcDon	
  Test	
  
•  To	
  pass	
  this	
  test,	
  the	
  Intent	
  object	
  must	
  match	
  one	
  of	
  
the	
  acDons	
  listed	
  in	
  the	
  filter	
  (in	
  manifest)	
  
•  An	
  Intent	
  object	
  that	
  doesn't	
  specify	
  an	
  acDon	
  
automaDcally	
  passes	
  the	
  acDon	
  test,	
  as	
  long	
  as	
  the	
  
filter	
  contains	
  at	
  least	
  one	
  acDon	
  
•  Intent	
  filter	
  must	
  contain	
  at	
  least	
  one	
  acDon	
  element	
  
<intent-­‐filter	
  .	
  .	
  .	
  >	
  
	
  	
  	
  	
  <acDon	
  android:name="com.example.project.SHOW_CURRENT"	
  />	
  
	
  	
  	
  	
  <acDon	
  android:name="com.example.project.SHOW_RECENT"	
  />	
  
	
  	
  	
  	
  <acDon	
  android:name="com.example.project.SHOW_PENDING"	
  />	
  
	
  	
  	
  	
  .	
  .	
  .	
  
</intent-­‐filter>	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

110	
  
Intent	
  Filters	
  –	
  Category	
  Test	
  
•  To	
  pass	
  the	
  category	
  test,	
  every	
  category	
  in	
  the	
  
Intent	
  object	
  must	
  match	
  a	
  category	
  in	
  the	
  filter	
  
•  AddiDonal	
  categories	
  can	
  be	
  listed,	
  but	
  it	
  cannot	
  
omit	
  any	
  that	
  are	
  in	
  the	
  intent	
  
<intent-­‐filter	
  .	
  .	
  .	
  >	
  
	
  	
  	
  	
  .	
  .	
  .	
  	
  	
  
	
  	
  	
  	
  <category	
  android:name="android.intent.category.DEFAULT"	
  />	
  
	
  	
  	
  	
  <category	
  android:name="android.intent.category.BROWSABLE"	
  />	
  
	
  	
  	
  	
  .	
  .	
  .	
  
</intent-­‐filter>	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

111	
  
Intent	
  Filters	
  –	
  Data	
  test	
  
•  Like	
  the	
  acDon	
  and	
  categories,	
  the	
  data	
  specificaDon	
  for	
  an	
  intent	
  
filter	
  is	
  contained	
  in	
  a	
  subelement,	
  it	
  can	
  appear	
  mulDple	
  Dmes,	
  or	
  
not	
  at	
  all	
  
•  Each	
  <data>	
  element	
  can	
  specify	
  a	
  URI	
  and	
  a	
  data	
  type	
  (MIME	
  
media	
  type)	
  
•  When	
  the	
  URI	
  in	
  an	
  Intent	
  object	
  is	
  compared	
  to	
  a	
  URI	
  specificaDon	
  
in	
  a	
  filter,	
  it's	
  compared	
  only	
  to	
  the	
  parts	
  of	
  the	
  URI	
  actually	
  
menDoned	
  in	
  the	
  filter	
  
<intent-­‐filter>	
  
	
  	
  	
  	
  <acDon	
  android:name="android.intent.acDon.VIEW"	
  />	
  
	
  	
  	
  	
  <category	
  android:name="android.intent.category.DEFAULT"	
  />	
  
	
  	
  	
  	
  <category	
  android:name="android.intent.category.BROWSABLE"	
  />	
  
	
  	
  	
  	
  <data	
  android:scheme="h[p"	
  />	
  
	
  	
  	
  	
  <data	
  android:mimeType="audio/mp3"/>	
  
	
  	
  	
  	
  <data	
  android:mimeType="audio/mp4"/>	
  
...	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

112	
  
Example:	
  Intent	
  Filters	
  in	
  Manifest	
  
<?xml	
  version="1.0"	
  encoding="u/-­‐8"?>	
  
<manifest	
  …>	
  
	
  	
  	
  	
  <applicaDon	
  ...>	
  
	
  	
  	
  	
  	
  	
  	
  	
  <acDvity	
  android:name=".MainAcPvity“	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  
MainAcDvity	
  can	
  be	
  lauched	
  from	
  the	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  android:label="@string/app_name">	
  
menu	
  and	
  it	
  can	
  also	
  handle	
  intent	
  that	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  <intent-­‐filter>	
  
is	
  describe	
  below.	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  <!–	
  start	
  as	
  main	
  entry	
  point	
  -­‐-­‐>	
  
	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  <acDon	
  android:name="android.intent.acPon.MAIN"	
  />	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  <category	
  android:name="android.intent.category.LAUNCHER"	
  />	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  </intent-­‐filter>	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  <intent-­‐filter>	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  <acDon	
  android:name="fi.ptm.PLAY_SOUND"	
  />	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  <data	
  android:mimeType="audio/mp3"	
  android:scheme="hTp"/>	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  <category	
  android:name="android.intent.category.DEFAULT"	
  />	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  </intent-­‐filter>	
  
</applicaDon>	
  
</manifest>	
  

	
  

Intent	
  intent	
  =	
  new	
  Intent(”fi.ptm.PLAY_SOUND”,	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  Uri.parse(”h[p://somedomain.com/music.mp3”));	
  
startAcDvity(intent);	
  
	
  

	
  

Android	
  Programming,	
  Pasi	
  Manninen.	
  

113	
  
Using	
  explicit	
  Intent	
  to	
  start	
  a	
  new	
  
AcDvity	
  
•  startAcDvity(intent)	
  
–  starts	
  a	
  new	
  AcDvity	
  
–  it	
  is	
  possible	
  to	
  send	
  data	
  to	
  with	
  intent	
  
–  acDvity	
  will	
  not	
  be	
  informed	
  when	
  the	
  child	
  
acDvity	
  is	
  complete	
  

•  startAcDvityForResult(intent,	
  REQUEST_CODE)	
  
–  like	
  previous,	
  but	
  it	
  takes	
  unique	
  constant	
  
–  acDvity	
  will	
  be	
  noDfied	
  when	
  the	
  child	
  acDvity	
  is	
  
complete	
  via	
  the	
  onAcDvityResult()	
  callback	
  
Android	
  Programming,	
  Pasi	
  Manninen.	
  

114	
  
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming
Android Programming

Mais conteúdo relacionado

Mais procurados

Basic android development
Basic android developmentBasic android development
Basic android developmentUpanya Singh
 
Android Web app
Android Web app Android Web app
Android Web app Sumit Kumar
 
Android tutorial
Android tutorialAndroid tutorial
Android tutorialmaster760
 
Android Development: The Basics
Android Development: The BasicsAndroid Development: The Basics
Android Development: The BasicsMike Desjardins
 
Learn Android app development in easy steps
Learn Android app development in easy stepsLearn Android app development in easy steps
Learn Android app development in easy stepsMobile Pundits
 
Introduction to Android, Architecture & Components
Introduction to  Android, Architecture & ComponentsIntroduction to  Android, Architecture & Components
Introduction to Android, Architecture & ComponentsVijay Rastogi
 
Introduction to Android
Introduction to AndroidIntroduction to Android
Introduction to AndroidOum Saokosal
 
Introduction to Android and Android Studio
Introduction to Android and Android StudioIntroduction to Android and Android Studio
Introduction to Android and Android StudioSuyash Srijan
 
Android Application Development Using Java
Android Application Development Using JavaAndroid Application Development Using Java
Android Application Development Using Javaamaankhan
 
Android App Development Intro at ESC SV 2012
Android App Development Intro at ESC SV 2012Android App Development Intro at ESC SV 2012
Android App Development Intro at ESC SV 2012Opersys inc.
 
Introduction to Android Development
Introduction to Android DevelopmentIntroduction to Android Development
Introduction to Android DevelopmentAly Abdelkareem
 
Basic android-ppt
Basic android-pptBasic android-ppt
Basic android-pptSrijib Roy
 
Android architecture
Android architectureAndroid architecture
Android architectureHari Krishna
 
Custom Android App Development – Web Animation India
Custom Android App Development – Web Animation IndiaCustom Android App Development – Web Animation India
Custom Android App Development – Web Animation IndiaMarion Welch
 
Android Development Workshop
Android Development WorkshopAndroid Development Workshop
Android Development WorkshopPeter Robinett
 

Mais procurados (20)

Basic android development
Basic android developmentBasic android development
Basic android development
 
Android Web app
Android Web app Android Web app
Android Web app
 
Android tutorial
Android tutorialAndroid tutorial
Android tutorial
 
Android Development: The Basics
Android Development: The BasicsAndroid Development: The Basics
Android Development: The Basics
 
Learn Android app development in easy steps
Learn Android app development in easy stepsLearn Android app development in easy steps
Learn Android app development in easy steps
 
Introduction to Android, Architecture & Components
Introduction to  Android, Architecture & ComponentsIntroduction to  Android, Architecture & Components
Introduction to Android, Architecture & Components
 
Android Platform Architecture
Android Platform ArchitectureAndroid Platform Architecture
Android Platform Architecture
 
Introduction to Android
Introduction to AndroidIntroduction to Android
Introduction to Android
 
Introduction to Android and Android Studio
Introduction to Android and Android StudioIntroduction to Android and Android Studio
Introduction to Android and Android Studio
 
Android Training
Android TrainingAndroid Training
Android Training
 
Android Application Development Using Java
Android Application Development Using JavaAndroid Application Development Using Java
Android Application Development Using Java
 
Android App Development Intro at ESC SV 2012
Android App Development Intro at ESC SV 2012Android App Development Intro at ESC SV 2012
Android App Development Intro at ESC SV 2012
 
Introduction to Android Development
Introduction to Android DevelopmentIntroduction to Android Development
Introduction to Android Development
 
Basic android-ppt
Basic android-pptBasic android-ppt
Basic android-ppt
 
PPT Companion to Android
PPT Companion to AndroidPPT Companion to Android
PPT Companion to Android
 
Android architecture
Android architectureAndroid architecture
Android architecture
 
Custom Android App Development – Web Animation India
Custom Android App Development – Web Animation IndiaCustom Android App Development – Web Animation India
Custom Android App Development – Web Animation India
 
Android ppt
Android pptAndroid ppt
Android ppt
 
Android Development Workshop
Android Development WorkshopAndroid Development Workshop
Android Development Workshop
 
Android overview
Android overviewAndroid overview
Android overview
 

Destaque

Programming Android Application in Scala.
Programming Android Application in Scala.Programming Android Application in Scala.
Programming Android Application in Scala.Brian Hsu
 
Android app development - Java Programming for Android
Android app development - Java Programming for AndroidAndroid app development - Java Programming for Android
Android app development - Java Programming for AndroidOUM SAOKOSAL
 
Android Project Presentation
Android Project PresentationAndroid Project Presentation
Android Project PresentationLaxmi Kant Yadav
 
Open document presentation
Open document presentationOpen document presentation
Open document presentationphpcurious
 
Android Apps for Absolute Beginners
Android Apps for Absolute BeginnersAndroid Apps for Absolute Beginners
Android Apps for Absolute BeginnersVaclav Hnizda
 
Android Coding Standered
Android Coding StanderedAndroid Coding Standered
Android Coding StanderedRavi Shankar
 
App Smashing for Beginners
App Smashing for BeginnersApp Smashing for Beginners
App Smashing for BeginnersDiana Benner
 
Architect Concept - .NET Interview Senior / Mid Level
Architect Concept - .NET Interview Senior / Mid Level Architect Concept - .NET Interview Senior / Mid Level
Architect Concept - .NET Interview Senior / Mid Level Asher Jawad
 
Beginners guide to creating mobile apps
Beginners guide to creating mobile appsBeginners guide to creating mobile apps
Beginners guide to creating mobile appsJames Quick
 
Android Course - Lesson5
 Android Course - Lesson5 Android Course - Lesson5
Android Course - Lesson5Callum Taylor
 
Android App Development Tips for Beginners
Android App Development Tips for BeginnersAndroid App Development Tips for Beginners
Android App Development Tips for BeginnersZoftino
 
First Steps in Android
First Steps in AndroidFirst Steps in Android
First Steps in AndroidRich Helton
 
Advance Android Programming - learning beyond basics
Advance Android Programming - learning beyond basicsAdvance Android Programming - learning beyond basics
Advance Android Programming - learning beyond basicsayman diab
 
Non Conventional Android Programming (English)
Non Conventional Android Programming (English)Non Conventional Android Programming (English)
Non Conventional Android Programming (English)Davide Cerbo
 
Advance Android application development workshop day 1
Advance Android application development workshop day 1Advance Android application development workshop day 1
Advance Android application development workshop day 1cresco
 
Software proposal on android
Software proposal on androidSoftware proposal on android
Software proposal on androidKamrul Chowdhury
 
Memory problems in android programming
Memory problems in android programmingMemory problems in android programming
Memory problems in android programmingAiTi Education
 

Destaque (20)

Programming Android Application in Scala.
Programming Android Application in Scala.Programming Android Application in Scala.
Programming Android Application in Scala.
 
Android app development - Java Programming for Android
Android app development - Java Programming for AndroidAndroid app development - Java Programming for Android
Android app development - Java Programming for Android
 
Android Project Presentation
Android Project PresentationAndroid Project Presentation
Android Project Presentation
 
Android ppt
Android ppt Android ppt
Android ppt
 
Ethical Hacking Tools
Ethical Hacking ToolsEthical Hacking Tools
Ethical Hacking Tools
 
Open document presentation
Open document presentationOpen document presentation
Open document presentation
 
Android Apps for Absolute Beginners
Android Apps for Absolute BeginnersAndroid Apps for Absolute Beginners
Android Apps for Absolute Beginners
 
Android Coding Standered
Android Coding StanderedAndroid Coding Standered
Android Coding Standered
 
Android
AndroidAndroid
Android
 
App Smashing for Beginners
App Smashing for BeginnersApp Smashing for Beginners
App Smashing for Beginners
 
Architect Concept - .NET Interview Senior / Mid Level
Architect Concept - .NET Interview Senior / Mid Level Architect Concept - .NET Interview Senior / Mid Level
Architect Concept - .NET Interview Senior / Mid Level
 
Beginners guide to creating mobile apps
Beginners guide to creating mobile appsBeginners guide to creating mobile apps
Beginners guide to creating mobile apps
 
Android Course - Lesson5
 Android Course - Lesson5 Android Course - Lesson5
Android Course - Lesson5
 
Android App Development Tips for Beginners
Android App Development Tips for BeginnersAndroid App Development Tips for Beginners
Android App Development Tips for Beginners
 
First Steps in Android
First Steps in AndroidFirst Steps in Android
First Steps in Android
 
Advance Android Programming - learning beyond basics
Advance Android Programming - learning beyond basicsAdvance Android Programming - learning beyond basics
Advance Android Programming - learning beyond basics
 
Non Conventional Android Programming (English)
Non Conventional Android Programming (English)Non Conventional Android Programming (English)
Non Conventional Android Programming (English)
 
Advance Android application development workshop day 1
Advance Android application development workshop day 1Advance Android application development workshop day 1
Advance Android application development workshop day 1
 
Software proposal on android
Software proposal on androidSoftware proposal on android
Software proposal on android
 
Memory problems in android programming
Memory problems in android programmingMemory problems in android programming
Memory problems in android programming
 

Semelhante a Android Programming

Introduction to android sessions new
Introduction to android   sessions newIntroduction to android   sessions new
Introduction to android sessions newJoe Jacob
 
Developing for Android-Types of Android Application
Developing for Android-Types of Android ApplicationDeveloping for Android-Types of Android Application
Developing for Android-Types of Android ApplicationNandini Prabhu
 
Android Seminar BY Suleman Khan.pdf
Android Seminar BY Suleman Khan.pdfAndroid Seminar BY Suleman Khan.pdf
Android Seminar BY Suleman Khan.pdfNomanKhan869872
 
Introduction_to_android_and_android_studio
Introduction_to_android_and_android_studioIntroduction_to_android_and_android_studio
Introduction_to_android_and_android_studioAbdul Basit
 
Mobile Application Development powerpoint
Mobile Application Development powerpointMobile Application Development powerpoint
Mobile Application Development powerpointJohnLagman3
 
Android dev o_auth
Android dev o_authAndroid dev o_auth
Android dev o_authlzongren
 
Android Application Development Training by NITIN GUPTA
Android Application Development Training by NITIN GUPTA Android Application Development Training by NITIN GUPTA
Android Application Development Training by NITIN GUPTA NITIN GUPTA
 
Android - Application Framework
Android - Application FrameworkAndroid - Application Framework
Android - Application FrameworkYong Heui Cho
 
Android application developement
Android application developementAndroid application developement
Android application developementSANJAY0830
 
Android 130923124440-phpapp01
Android 130923124440-phpapp01Android 130923124440-phpapp01
Android 130923124440-phpapp01rajesh kumar
 
Android app development by abhi android
Android app development by abhi androidAndroid app development by abhi android
Android app development by abhi androidsusijanny
 
Android app development
Android app developmentAndroid app development
Android app developmentAbhishek Saini
 

Semelhante a Android Programming (20)

Introduction to android sessions new
Introduction to android   sessions newIntroduction to android   sessions new
Introduction to android sessions new
 
Developing for Android-Types of Android Application
Developing for Android-Types of Android ApplicationDeveloping for Android-Types of Android Application
Developing for Android-Types of Android Application
 
My androidpresentation
My androidpresentationMy androidpresentation
My androidpresentation
 
Android Applications
Android ApplicationsAndroid Applications
Android Applications
 
Android ppt
Android pptAndroid ppt
Android ppt
 
Android Seminar BY Suleman Khan.pdf
Android Seminar BY Suleman Khan.pdfAndroid Seminar BY Suleman Khan.pdf
Android Seminar BY Suleman Khan.pdf
 
Introduction to android
Introduction to androidIntroduction to android
Introduction to android
 
Introduction_to_android_and_android_studio
Introduction_to_android_and_android_studioIntroduction_to_android_and_android_studio
Introduction_to_android_and_android_studio
 
Introduction to android
Introduction to androidIntroduction to android
Introduction to android
 
Mobile Application Development powerpoint
Mobile Application Development powerpointMobile Application Development powerpoint
Mobile Application Development powerpoint
 
Google android os
Google android osGoogle android os
Google android os
 
Android dev o_auth
Android dev o_authAndroid dev o_auth
Android dev o_auth
 
Android Application Development Training by NITIN GUPTA
Android Application Development Training by NITIN GUPTA Android Application Development Training by NITIN GUPTA
Android Application Development Training by NITIN GUPTA
 
Android
AndroidAndroid
Android
 
Android technology
Android technology Android technology
Android technology
 
Android - Application Framework
Android - Application FrameworkAndroid - Application Framework
Android - Application Framework
 
Android application developement
Android application developementAndroid application developement
Android application developement
 
Android 130923124440-phpapp01
Android 130923124440-phpapp01Android 130923124440-phpapp01
Android 130923124440-phpapp01
 
Android app development by abhi android
Android app development by abhi androidAndroid app development by abhi android
Android app development by abhi android
 
Android app development
Android app developmentAndroid app development
Android app development
 

Último

Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 

Último (20)

Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 

Android Programming

  • 1. Android  Programming   Pasi  Manninen     JAMK  University  of  Applied  Sciences   Slide  version  Nov  20th,  2013    
  • 2. 1.  Android  IntroducDon   What  is  Android?   Android  Version  History   Features   Development  Framework  and   Android  SoMware  Stack   •  A  few  important  Android  terms  to   understand   •  Android  ApplicaDon  Fundamentals   •  •  •  •  Android  Programming,  Pasi  Manninen.   2  
  • 3. What  is  Android?   •  1.  A  free  open-­‐source  operaDng  system  for  mobile  devices   •  2.  An  open-­‐source  development  pla.orm  for  creaDng   mobile  and  applicaDons   •  3.  Devices  that  runs  Android  applicaDons   •  Allows  coding  with  Java  language   •  Based  on  the  Linux  kernel   •  Open  (all  features  are  open  to  developer)   •  All  applicaDons  are  equal  (core  and  3th  party)   •  Really  fast  and  easy  applicaDon  development   •  Developed  by  Google  and  the  Open  Handset  Alliance,  OHA   •  Announced  5th  Nov,  2007   •  h[p://en.wikipedia.org/wiki/Android_(operaDng_system)       Android  Programming,  Pasi  Manninen.   3  
  • 4. Open  Handset  Alliance     •  OHA,  Group  of  technology  and  mobile   companies   •  Android  is  the  first  complete,  open,  and  free   mobile  pla`orm  provided  by  OHA   •  The  goal  is  offer  richer  experience  with  mobile   devices  (with  open  standards)   •  Handset  manufacturers  don’t  need  to  pay  any   lisencing  fees  to  load  Android  on  their  devices   •  h[p://www.openhandsetalliance.com   Android  Programming,  Pasi  Manninen.   4  
  • 5. Android  Version  History   •  Android  has  seen  a  number  of  updates  since   its  original  release  (July  22nd,  2013):                   h[p://en.wikipedia.org/wiki/Ice_Cream_Sandwich_(operaDng_system)#Android_4.0.x_Ice_Cream_Sandwich     Android  Programming,  Pasi  Manninen.   5  
  • 6. NaDve  Android  ApplicaDons   •  •  •  •  •  •  •  •  •  •  •  •  •  An  e-­‐mail  client   An  SMS  management  applicaDon   A  full  PIM  (Calendar,  Contacts,  ...)   A  WebKit-­‐based  wed  browser   A  music  player  and  picture  gallery   A  Camera  and  video  recording  apps   The  Home  Screen   The  Android  Market  (Google  mobile)   Google  Maps  (Google  mobile)   Gmail  Client  (Google  Mobile)   Google  Talk  (Google  Mobile)   YouTube  video  Player  (Google  Mobile)   …   Android  Programming,  Pasi  Manninen.   6  
  • 7. Features  (Android  SDK)   •  GSM,  EDGE,  3G,  Wi-­‐Fi  networks  for  telephony  or  data  transfer   •  LocaDon  based  services  as  GPS,  Google  Maps,  Geocoding   •  Full  mulDmedia  control  (playback  and  recording  with  camera  and   microphone),  formats  MPEG4,  H.264,MP3,AAC,AMR,JPG,PNG,GIF...   •  Sensors,  accelerometers,  compass   •  Bluetooth   •  Shared  data  stores  (SQLite,  Preferenses,  Content  Providers)   •  Home-­‐Screen  widgets,  Live  Folders,  Live  Wallpapers   •  An  integrated  WebKit-­‐based  browser   •  Hardware  accelerated  graphics  (OpenGL  ES  2.0)   •  Background  applicaDons  and  processes   •  No  licensing,  distribuDon  or  development  fees   •  …   Android  Programming,  Pasi  Manninen.   7  
  • 8. Development  Framework   •  Android  applicaDons  are  wri[en  with  Java   •  Programs  are  executed  in  custom  virtual  machine   called  Dalvik   •  Each  applicaDon  runs  its  own  process  in  Dalvik   •  SDK  gives  all  what  you  need  start  developing,  tesDng   and  debugging  your  applicaDon   –  Android  API   –  Development  tools   –  Android  Virtual  Device  Manager  and  Emulator   –  Full  documentaDon  with  sample  codes   –  Online  support  h[p://developer.android.com     Android  Programming,  Pasi  Manninen.   8  
  • 9. Android  SoMware  Stack   Android  Programming,  Pasi  Manninen.   9  
  • 10. A  few  important  Android  “terms”   •  AcDvity   –  •  ApplicaDon  Class   –  •  A  component  that  does  nothing  but  receive  and  react  to  broadcast  announcements   Views   –  –  •  ”intenDon”  to  do  some  work  in  your  applicaDon   Asynchronous  messages  which  holds  the  content  of  the  message   IntentReceiver   –  •  Makes  a  specific  set  of  the  applicaDon's  data  available  to  other  applicaDons   Intent   –  –  •  Doesn’t  have  visual  interface,  runs  background  for  an  indefinite  period  of  Dme   ContentProvider   –  •  Differences  to  AcDvity  based  applicaDons:  maintain  applicaDon  state  and  it  is  easy  to  transfer  objects  and   use  resources  between  components   Service   –  •  A  single  visual  user  interface,  applicaDon  is  made  up  of  one  or  more  AcDviDes   User  interface  elements  like  a  Bu[on  or  a  Label  or  lots  of  other  UI  elements   Hierarchical  structure   Home  Screen  Widgets   –   ApplicaDons  that  can  be  run  in  Android  devices  home  screen  (can  be  an  entry  point  to  real  applicaDon  also)   Android  Programming,  Pasi  Manninen.   10  
  • 11. Android  ApplicaDon  Fundamentals   •  Android  applicaDon  lives  in  its  own  world   –  Every  applicaDon  runs  in  its  own  process   –  Each  applicaDon  is  assigned  a  unique  Linux  user   ID,  applicaDon  files  are  visible  only  that  user  and   applicaDon  (permissions)   •  ApplicaDon  can  make  use  of  elements  of  other   applicaDons   •  System  must  be  able  to  start  an  applicaDon   process  when  any  part  of  it  is  needed   Android  Programming,  Pasi  Manninen.   11  
  • 12. 2.  Set  up  a  complete  Android   development  environment   •  •  •  •  System  requirements   Installing  Java     Serng  up  the  ADT  Bundle   Hands-­‐on  training:     –  Install  and  configure  Android  development   environment   Android  Programming,  Pasi  Manninen.   12  
  • 13. System  Requirements   •  OperaDng  systems   –  Windows  XP  (32-­‐bit),  Vista  (32-­‐  or  64-­‐bit),  or   Windows  7  (32-­‐  or  64-­‐bit)   –  Mac  OS  X  10.5.8  or  later  (x86  only)   –  Linux  (tested  on  Ubuntu  Linux,  Lucid  Lynx)   •  Eclipse  IDE  3.6.2  or  later,  JDT-­‐plugin   •  JDK  6  (or  later)   Android  Programming,  Pasi  Manninen.   13  
  • 14. Installing  Java     •  Needed  Java  JDK  can  be  download  from   Oracle  site:   –  h[p://www.oracle.com/technetwork/java/javase/ downloads/index.html     •  Remember  download  right  version  of  Java  (32   or  64  bit)   Android  Programming,  Pasi  Manninen.   14  
  • 15. Serng  up  the  ADT  Bundle   •  To  get  started  go  to   –  h[p://developer.android.com/sdk/index.html   •  Download  the  SDK  and  unpack  the  ZIP  file  to   appropriate  locaDon   •  Open  Eclipse  from  eclipse  folder   •  Install  addiDonal  version  of  Android  or  other   needed  packages  with  SDK  Manager   Android  Programming,  Pasi  Manninen.   15  
  • 16. Install  and  Configure  Android   development  environment   •  Hands-­‐on  training:   –  Download  and  install  Java  JDK   –  Download  and  install  the  Android  SDK  (it  installs   eclipse  too)   –  Start  eclipse  and  Android  SDK  Manager   –  Add  needed  Android  packages  and  other  extras   Android  Programming,  Pasi  Manninen.   16  
  • 17. 3.  CreaDng  a  first  Android  ApplicaDon   •  •  •  •  CreaDng  Android  Project  in  Eclipse   Running  applicaDon  in  Emulator   Running  applicaDon  in  Device   Debugging  applicaDon   –  Eclipse  debug  perspecDve   –  Dalvik  Debug  Monitor  Service  (DDMS)   •  Hands-­‐on  training:     –  Hello  Android!   Android  Programming,  Pasi  Manninen.   17  
  • 18. CreaDng  an  Android  Project  in  Eclipse   •  Android  project  contains   all  the  files  that  comprise   the  source  code  for  your   Android  app   •  Android  SDK  tools  make  it   easy  to  start  a  new   Android  project  with  a  set   of  default  project   directories  and  files   •  Select  File  >  New  >   Android  ApplicaDon   Project  from  Eclipse   •  A  few  dialogs  will  be  shown  to  configure  your   applicaDon  default  serngs   Android  Programming,  Pasi  Manninen.   18  
  • 19. Running  applicaDon  in  Emulator   •  With  ADT  running  your  applicaDon  does   following:   –  Current  project  compiles  and  converts  to  Android   executable  (.dex)   –  Executable  and  resources  will  be  packed  into  an   Android  package  (.apk)   –  Selected  virtual  device  will  be  started  and  your   applicaDon  will  be  installed  and  started   •  Needed  emulators  (virtual  devices)  can  be   created  with  Android  Virtual  Device  Manager   (AVD)     Android  Programming,  Pasi  Manninen.   19  
  • 20. Running  applicaDon  in  device   •  Device  has  to  be  plugged   to  development  machine   with  USB  cable   •  USB  debugging  has  to  be   enabled  in  device   •  Select  Run   ConfiguraDons…  and   choose  device  from   Android  Device  Chooser   Android  Programming,  Pasi  Manninen.   20  
  • 21. Debugging  applicaDon   •  The  Debug  PerspecDve  in  Eclipse   –  Use  Debug,  Variables,  Breakpoints  and  LogCat   tabs  in  Eclipse   •  The  DDMS  (Dalvik  Debug  Monitor  Service)   PerspecDve   –  Show  the  AVDs  and  real  devices  list   –  Emulator  controls  (locaDon,  phone  call,  …)   –  Processes,  Threads,  heap,  memory  allocaDon,   files,  …   Android  Programming,  Pasi  Manninen.   21  
  • 22. CreaDng  a  first  Android  ApplicaDon   •  Hands-­‐on  training  :  Hello  Android!   –  Create  a  new  Android  Project   –  Create  first  Android  ApplicaDon   –  Run,  debug  and  test  in  an     emulator  and  real  device   –  DDMS,  Dalvik  Debug   Monitor  Service   Android  Programming,  Pasi  Manninen.   22  
  • 23. 4.  Publishing  ApplicaDon  to  Google   Play   •  ApplicaDon  Requirements   •  Signing  the  ApplicaDon   –  Strategy   –  Debug  and  Public  Release   •  Google  Play   –  ApplicaDon  LisDng   –  Uploading  a  new  ApplicaDon   Android  Programming,  Pasi  Manninen.   23  
  • 24. ApplicaDon  Requirements   •  ApplicaDon  must  be  signed  with  private  key,  validity  period   ends  aMer  22  October  2033   •  ApplicaDon  must  define  both  an  android:versionCode  and   an  android:versionName  a[ribute  in  the  manifest  file   •  ApplicaDon  must  define  both  an  android:icon  and  an   android:label  a[ribute  in  the  <applica:on>  element  of  its   manifest   <manifest  xmlns:android="h[p://schemas.android.com/apk/res/android"                                        package="fi.ptm.nameday"                                            android:versionCode="8"                                              android:versionName="1.0.6">                                                <applicaDon  android:icon="@drawable/icon"                                                                                        android:label="@string/app_name">   ...   Android  Programming,  Pasi  Manninen.       24  
  • 25. Signing  the  ApplicaDon   •  All  Android  apps  must  be  signed   –  the  system  will  not  install  an  applicaDon  that  is  not  signed   •  You  can  use  self-­‐signed  cerDficates  to  sign  your  applicaDons   –  no  cerDficate  authority  is  needed   •  ApplicaDon  must  be  signed  it  with  a  suitable  private  key  before   publishing  to  Android  Market   –  By  default  applicaDon  is  signed  with  debug  key  (generated  with  SDK   Tools  in  Eclipse)   •  The  system  tests  a  signer  cerDficate's  expiraDon  date  only  at  install   Dme   •  You  can  use  standard  tools  to  generate  keys  and  sign  your   applicaDon  .apk  files   –  Keytool  and  Jarsigner  (in  command  line)   –  ADT  Export  Wizard  in  Eclipse   Android  Programming,  Pasi  Manninen.   25  
  • 26. Signing  Strategy   •  Use  same  cerDficate  for  all  applicaDons   –  applicaDon  upgrade  (seamless)   –  applicaDon  modularity  (same  process)   –  code  and  data  sharing  (through  permissions)   •  Validity  period  of  cerDficate  exceeds  the   expected  lifespan  of  applicaDon   –  validity  period  of  25  years  or  more  is  recommended   (no  upgrades)   –  Android  Market  (validity  period  ending  aMer  22   October  2033)   Android  Programming,  Pasi  Manninen.   26  
  • 27. Signing  in  Debug  Mode   •  The  Android  build  tools  provide  a  debug  signing  mode   •  ADT  generates  a  debug  cerDficate  automaDcally  for   emulator  and  device     –  Keystore  name:  "debug.keystore"   –  Keystore  password:  "android"   –  Key  alias:  "androiddebugkey"   –  Key  password:  "android"   –  CN:  "CN=Android  Debug,O=Android,C=US“   –  Keystore  is  located  in  your  home  directory’s  .android  dir   •  Debug  cerDficates  expire  aMer  365  days  aMer  creaDon   Android  Programming,  Pasi  Manninen.   27  
  • 28. Signing  Public  Release   •  •  •  •  •  Make  sure  that  your  applicaDon  is  ready   Obtain  a  suitable  private  key   Compile  applicaDon  in  release  mode   Sign  applicaDon  with  private  key   On  Eclipse  right-­‐click  on  your  project  folder   –  select  Android  Tools  >  Export  Unsigned   ApplicaDon  Package   –  select  File  >  Export  to  export  signed  APK,  select   Export  Android  ApplicaDon   Android  Programming,  Pasi  Manninen.   28  
  • 29. Publish  -­‐  KeyStore   •  Create  a  new  KeyStore  if  needed   •  Remember  your   password   Android  Programming,  Pasi  Manninen.   29  
  • 30. Publish  –  Key  CreaDon   •  KeyStore  Alias  for  later  use   •  Validity  period   •  Personal,  corporate  or   organizaDon   idenDficaDon   Android  Programming,  Pasi  Manninen.   30  
  • 31. Publish  –  Signed  APK  File   •  Enter  desDnaDon  for  the  APK  file   •  APK  file  is  ready   to  publish  in     Android  Market   Android  Programming,  Pasi  Manninen.   31  
  • 32. Google  Play   •  Google  Play  is  a  hosted  service   –  easy  for  users  to  find  and  download  Android  applicaDons   –  easy  for  developers  to  publish  their  applicaDons   •  To  publish  your  applicaDon  on  Google  Play   –  register  with  the  service  using  your  Google  account   –  agree  to  the  terms  of  service   –  developer  fee  is  $25  (Iphone  developer  cost  is  $99)   •  Once  published,  users  can  see  your  applicaDon,   download  it,  and  rate  it  using  the  Play  applicaDon   installed  on  their  Android-­‐powered  devices   •  h[ps://play.google.com/apps/publish/     Android  Programming,  Pasi  Manninen.   32  
  • 33. ApplicaDon  LisDngs   •  All  applicaDons  are  listed  nicely  with  basic   informaDon   –  Name,  download  counts,  stars  and  so  on   Android  Programming,  Pasi  Manninen.   33  
  • 34. Uploading  applicaDon   •  ApplicaDon  uploading   is  easy   •  You  can  add  a   screenshots  and   promoDonal     graphics   Android  Programming,  Pasi  Manninen.   34  
  • 35. ApplicaDon  Details   •  Select:   –  languages   –  descripDon   –  app  type  and   category   –  price   –  protecDon   Android  Programming,  Pasi  Manninen.   35  
  • 36. Contact  InformaDons   •  Select  Copy   protecDon   •  Give  contact   InformaDon   •  And     Publish!   Android  Programming,  Pasi  Manninen.   36  
  • 37. 5.  Directory  Structure  of  an  Android   ApplicaDon  Project   •  Android  ApplicaDon  Project  folder   •  Android  ApplicaDon  Resources   •  Resource  examples:   –  strings.xml   –  colors.xml   •  AndroidManifest.xml   •  AndroidManifest.xml  example:   –  HelloAndroid  project   •  SupporDng  different  languages  and  hardwares   •  Exercise:  Texts,  colors  and  localizaDon   Android  Programming,  Pasi  Manninen.   37  
  • 38. Android  ApplicaDon  Project  folder   •  Android  project  holds  all  the  code  and  resources  in   different  folders.   •  Some  of  the  folders  are  generated  by  default  and     some  has  to  be  done  by  self  if  needed     src    (all  source  codes)   gen  (Java  files  generated  by  ADT)   assets  (used  for  example  store  raw  asset  files)   bin  (output  directory,  apk  is  here)   res  (resources  of  the  applicaDon)   res/drawable  (image  and  image-­‐descriptor  files)   res/layout  (views  of  the  applicaDon)   res/menu  (applicaDon  menus)   res/values  (other  resources  of  the  applicaDon)   …   –  Strings,  styles,  colors  and  so  on...  (xml  based)   Android  Programming,  Pasi  Manninen.   38  
  • 39. Android  ApplicaDon  Resources   •  It  is  good  to  keep  non-­‐code  resources  (images,  strings,  animaDon,  themes,   layouts)  external  to  code   •  Easy  to  maintain,  update  and  manage   •  Supports  different  devices  and  localizaDon   •  Android  selects  resources  dynamically  in  runDme  (no  need  to  code)   •  ApplicaDon  resources  are  stored  under  res/  folder   –  –  –  –  –  res/anim  (frame  by  frame  animaDons)   res/drawable  (bitmaps,  other  types  drawable  types)   res/layout  (UI  layouts)   res/menu  (menu  layouts)   res/values  (different  resources)  for  example   •  arrays.xml,  colors.xml,  dimens.xml,  strings.xml,  styles.xml,  themes.xml   –  res/xml  (arbitrary  XML  files)   •  •  Resources  can  be  used  from  code  or  from  other  recources   System  Resources  are  defined  under  android.R   Android  Programming,  Pasi  Manninen.   39  
  • 40. Resource  example:  strings.xml   •  Easy  to  update  later,  localizaDon   <?xml  version="1.0"  encoding="u/-­‐8"?>   <resources>          <string  name="app_name">NameDay</string>          <string  name="widget_header">Nameday's</string>          <string  name="widget_footer">(c)  2013  PTM</string>   </resources>   Used  in  main.xml  layout  (for  example):   <TextView          android:id=”@+id/text”          android:text=”@string/widget_header”  />   //  Used  in  code:   CharSequence  str  =  getString(R.string.widget_header);   TextView  tv  =  (TextView)  findViewById(R.id.text);   tv.setText(str);   Android  Programming,  Pasi  Manninen.   40  
  • 41. Resource  example:  colors.xml   •  Stored  in  res/values  folder   •  It  is  good  pracDce  to  keep  all  color  values  in  same  file   •  Used  same  way  as  strings  in  earlier  example   <?xml  version="1.0"  encoding="u/-­‐8"?>   <resources>          <color  name="opaque_blue">#00F</color>          <color  name="transparent_green">#7700FF00</color>          <color  name="Ptle_color_dark">#FF860000</color>   </resources>   Android  Programming,  Pasi  Manninen.   41  
  • 42. AndroidManifest.xml   •  ApplicaDon  descriptor  file   •  Defines  your  applicaDon   –  AcDviDes   –  ContentProviders   –  Services   –  Intent  Receivers   –  Permissions   –  Version  number   –   and  a  lots  of  more...   Android  Programming,  Pasi  Manninen.   42  
  • 43. AndroidManifest.xml   example:  HelloAndroid  project   <?xml  version="1.0"  encoding="u/-­‐8"?>   <manifest  xmlns:android="hTp://schemas.android.com/apk/res/android"          package="fi.ptm.helloandroid"          android:versionCode="1"          android:versionName="1.0"  >            <uses-­‐sdk  android:minSdkVersion="16"  android:targetSdkVersion="17"  />            <applicaDon                  android:allowBackup="true"                  android:icon="@drawable/ic_launcher"                  android:label="@string/app_name"                  android:theme="@style/AppTheme"  >                  <acDvity                          android:name="fi.ptm.helloandroid.MainAcPvity"                          android:label="@string/app_name"  >                          <intent-­‐filter>                                  <acDon  android:name="android.intent.acPon.MAIN"  />                                  <category  android:name="android.intent.category.LAUNCHER"  />                          </intent-­‐filter>                  </acDvity>          </applicaDon>     </manifest>   Android  Programming,  Pasi  Manninen.   43  
  • 44. Different  languages  and  hardware   •  Like  said  Android  has  dynamic  resource  selecDon   mechanism   •  All  is  done  with  using  directory  structure   •  Spesific  languages,  locaDons  and  hardware   •  AlternaDves  are  described  with  –  mark     For  example  project:   -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐   res/layout/main.xml   res/layout-­‐land/main.xml   res/values-­‐fi/strings.xml   res/values-­‐en/strings.xml   Android  Programming,  Pasi  Manninen.   44  
  • 45. Support  different  screen  sizes   •  Portrait  and  lanscape  modes   can  be  easily  done  with   resources   •  Portrait  mode  is  created   automaDcally  when  project  is   done  (layout)   •  Create  a  new  folder  (layout-­‐ land)  for  landscape  acDviDes   •  Android  switches  layouts   automaDcally  when  new   mode  is  detected   Android  Programming,  Pasi  Manninen.   45  
  • 46. Support  different  languages   •  Language  support  is  done   via  resources  also   •  Default  language  strings   are  stored  to  values  folder   •  Create  a  new  values-­‐ (language  code)  folder  for   all  supported  languages   –  For  example  values-­‐fi  for   Finnish   •  Android  automaDcally  uses   right  values  folder  if  device   switches  language   Android  Programming,  Pasi  Manninen.   46  
  • 47. Exercise:  Texts,  Colors  and  LocalizaDon   •  Create  an  applicaDon  with  a  few  TextViews   with  different  colors  and  texts  (use  XML  files).   Make  a  few  localizaDon  test  (for  example  fi)   and  test  localizaDon  in  emulator.   Android  Programming,  Pasi  Manninen.   47  
  • 48. 6.  AcDviDes  and  User  Interface   •  •  •  •  •  •  •  •  •  •  •  What  is  AcDvity?   AcDviDes  and  tasks   StarDng  process  and  threads   AcDvity  lifecycle   Declaring  acDvity  in  the  manifest   Basics  of  the  Views  and  Layouts   Defining  layouts  with  XML   How  Android  draws  a  view  (AcDvity)   Common  Layouts  and  Controls  with  examples   Handling  UI  events   Exercise:  Basic  UI  Controls   Android  Programming,  Pasi  Manninen.   48  
  • 49. What  is  AcDvity?   •  ApplicaDon  component  (user  interface)  that  provides  a   screen  which  user  can  interact  to  do  something   •  ApplicaDon  usually  contains  a  mulDple  acDviDes  which   makes  the  whole  applicaDon   •  Typically,  one  acDvity  in  an  applicaDon  is  specified  as  the   main  acDvity,  which  is  presented  to  the  user  when   launching  the  applicaDon  for  the  first  Dme   •  Each  acDvity  can  then  start  another  acDvity  in  order  to   perform  different  acDons   •  AcDviDes  are  subclasses  from  AcDvity  class   •  Developer  implements  callback  methods  that  the  system   calls  different  states  of  the  applicaDon  (AcDvity)   Android  Programming,  Pasi  Manninen.   49  
  • 50. AcDviDes  and  tasks   •  A  task  is  what  the  user  experiences  as  an  applicaDon   •  Task  is  like  a  collecDon  of  AcDvites  what  runs  in  same   applicaDon   •  All  acDviDes  are  set  in  the  stack   •  The  root  acDvity  in  the  stack  is  the  one  that  began  the  task   (the  applicaDon  launcher)   •  AcDvity  at  the  top  of  the  stack  is  one  that's  currently   running   •  The  previous  acDviDes  remains  in  the  stack  and  are  paused/ stopped  (acDvated  when  user  presses  back  bu[on)   •  By  default,  all  the  acDviDes  in  an  applicaDon  have  an   affinity  for  each  other  (name  of  the  .apk  package  name)   Android  Programming,  Pasi  Manninen.   50  
  • 51. StarDng  process  and  threads   •  When  the  first  of  an  applicaDon's  components  needs  to  be   run,  Android  starts  a  Linux  process  for  it  with  a  single   thread  of  execuDon   •  The  process  where  a  component  runs  is  controlled  by  the   manifest  file     •    Note!   •  Android  may  decide  to  shut  down  a  process  at  some  point   •  Everything  runs  in  the  main  thread  (including  UI),  avoid   long  lasDng  operaDons   –  Maintain  UI  responsive   –  Use  another  thread  (AsyncTask)  for  the  long  operaDons   Android  Programming,  Pasi  Manninen.   51  
  • 52. AcDvity  lifecycle   •  Three  main  states:  running,     paused  and  stopped   •  If  an  acDvity  is  paused  or     stopped,  the  system  can     drop  it  from  memory   •  Do  your  applicaDon  UI,   bind  Data  sources  and  event     handlers  in  onCreate()   •  AMer  onResume()  acDvity   is  visible  to  end  user   •  In  onPause()  save  criDcal   data  to  applicaDon’s  data  store   Note:  This  will  be  discussed  more  deeply  later…   Android  Programming,  Pasi  Manninen.   52  
  • 53. Declaring  acDvity  in  the  manifest   •  AcDvity  has  to  be  declared  in  the  manifest  file   •  Use  android:name  a[ribute  to  specify  the  class   name  of  the  acDvity   •  Use  other  a[ributes  to  add  addiDonal   informaDon  of  the  acDvity  (label,  icon,  theme,  …)   •  Use  Intent  filters  if  you  want  to  make  your   acDvity  available  to  system  or  other  applicaDons   <acDvity                          android:name="fi.ptm.helloandroid.MainAcPvity”                          android:label="@string/app_name"  >                          <intent-­‐filter>                                  <acDon  android:name="android.intent.acPon.MAIN"  />                                  <category  android:name="android.intent.category.LAUNCHER"  />                          </intent-­‐filter>   </acDvity>   Android  Programming,  Pasi  Manninen.   53  
  • 54. Basics  of  the  Views  and  Layouts   •  Android  applicaDon  UI  is  built   using  View  and  ViewGroup   objects   •  Bu[on,  Text  (and  so  on  UI   objects)  are  View’s  subclasses   and  they  are  called  as   ”widgets”  or  ”Views”   •  Layout  architectures  like   linear,  tabular  and  relaDve  are   ViewGroup’s  subclasses  and   they  are  called  as  “Layouts”   Android  Programming,  Pasi  Manninen.   54  
  • 55. Defining  layouts  with  XML   •  Most  common  way  is  to  use  XML  layout  for   declaring  AcDvity  layout   •  Each  element  in  XML  is  either  a  View  or   ViewGroup  object     •  View  objects  are  leaves  in  the  tree,  ViewGroup   objects  are  branches  in  the  tree   •  The  name  of  an  XML  element  is  respecDve  to  the   Java  class  that  it  represents  like:  <TextView>   element  creates  a  TextView  in  your  UI   •  Layout  and  its  elements  can  be  instanDate  at   runDme  programmaDcally  also   Android  Programming,  Pasi  Manninen.   55  
  • 56. How  Android  draws  a  view   •  A[ach  the  view  hierarchy  tree  to  the  screen,  AcDvity  must  call  the   setContentView()  method  and  pass  a  reference  to  the  root  node   object   •  The  draw()  method  of  a  View  is  called   •  Two  process:  measure  and  layout  pass   •  Mostly  used  layout  params:   –  Exact  number   –  match_parent,  view  wants  to  be  as  big  as  its  parent   –  wrap_content,  view  wants  to  be  just  big  enough  to  enclose  its  content   •  Size,  Padding,  Margins  is  used  like  in  HTML   •  Dimensions:  dp,  sp,  pt,  px,  mm  and  in   •  Different  Views  and  Layouts  has  its  own  subclass  of  Layout  Params   Android  Programming,  Pasi  Manninen.   56  
  • 57. Common  Layouts   •  FrameLayout   –  a  blank  space  on  your  screen  that  you  can  later  fill  with  a  single  object   •  LinearLayout   –  aligns  all  children  in  a  single  direcDon  (verDcally  or  horizontally)   –  all  children  are  stacked  one  aMer  the  other   •  TableLayout   –  posiDons  its  children  into  rows  and  columns   –  doesn’t  display  border  lines  for  their  rows,  columns,  or  cells   •  RelaDveLayout   –  child  views  specify  their  posiDon  relaDve  to  the  parent  view  or  to  each   other   •  More:     –  Gallery,  GridView,  ListView,  ScrollView,  Spinner,  SurfaceView,   TabHost,  ViewFlipper,  ViewSwitcher,  Fragments,  WebView,…   Android  Programming,  Pasi  Manninen.   57  
  • 58. LinearLayout  example   <!–  ac:vity_main.xml-­‐-­‐>   <LinearLayout  xmlns:android="hTp://schemas.android.com/apk/res/android"          xmlns:tools="hTp://schemas.android.com/tools”>          <TextView                  android:id="@+id/textView1"                  android:layout_width="wrap_content"                  android:layout_height="wrap_content"                  android:text="@string/hello_world"  />          <Bu[on                  android:id="@+id/buTon1"                  android:layout_width="wrap_content"                  android:layout_height="wrap_content”                  android:text="@string/buTon_string"  />   </LinearLayout>   //  load  acDvity_main.xml   public  void  onCreate(Bundle  savedInstanceState)  {          super.onCreate(savedInstanceState);          setContentView(R.layout.ac:vity_main);   }   Android  Programming,  Pasi  Manninen.   58  
  • 59. RelaDveLayout  example   <RelaDveLayout  xmlns:android="hTp://schemas.android.com/apk/res/android"          xmlns:tools="hTp://schemas.android.com/tools"          xmlns:ads="hTp://schemas.android.com/apk/lib/com.google.ads"          android:layout_width="match_parent"          android:layout_height="match_parent"          tools:context=".MainAcPvity"  >            <com.google.ads.AdView                    android:id="@+id/adView"                  android:layout_width="match_parent"                  android:layout_height="50dip"                  ads:adUnitId=”xxxxxxx"                  ads:adSize="BANNER"                  ads:testDevices="TEST_EMULATOR,  TEST_DEVICE_ID"                  ads:loadAdOnCreate="true"                  android:layout_alignParentBoOom="true"                  />     …   Android  Programming,  Pasi  Manninen.   59  
  • 60. RelaDveLayout  example   …   <TableLayout          android:id="@+id/keypad"      android:orientaDon="verPcal"      android:layout_width="fill_parent"      android:layout_height="wrap_content”    android:stretchColumns="*”    android:layout_above="@+id/adView”>      <TableRow>    <Bu[on  android:id="@+id/keypad_1"  android:text="1"      android:onClick="buTonClicked"></BuTon>    <Bu[on  android:id="@+id/keypad_2"  android:text=“2"      android:onClick="buTonClicked"></BuTon>    …    </TableRow>      …   </TableLayout>   …     Android  Programming,  Pasi  Manninen.   60  
  • 61. RelaDveLayout  example   …   <HorizontalScrollView                android:id="@+id/scrollView"                android:layout_width="fill_parent"                android:layout_height="75dp"                android:layout_above="@+id/keypad"                android:paddingTop="2dp"                android:paddingBo[om="2dp"                >                <LinearLayout  android:id="@+id/linearLayoutOfImages"                          android:layout_width="fill_parent"                          android:layout_height="fill_parent"                          android:orientaDon="horizontal">                                            </LinearLayout>            </HorizontalScrollView>   …   Android  Programming,  Pasi  Manninen.   61  
  • 62. RelaDveLayout  example   …   <fi.ptm.teksDtv.AspectRaDoImageView                android:layout_centerHorizontal="true"              android:id="@+id/imageView"              android:src="@drawable/ladataan"                android:layout_alignParentTop="true"                android:layout_height="wrap_content"              android:layout_width="match_parent"                android:adjustViewBounds="true"/>   <TextView                android:id="@+id/pageTitle"              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:layout_alignParentLeR="true"              android:text="100/01"              android:background="#FFFFFF"              android:textColor="#000000"/>    </RelaDveLayout>   Android  Programming,  Pasi  Manninen.   62  
  • 63. Common  Input  Controls   •  Input  controls  are  the  interacDve   components  in  your  applicaDon’s  user   interface   •  Android  provides  a  wide  variety  of   controls  to  use  in  your  UI   –  Bu[ons,  text  fields,  seek  bars,  checkboxes,   spinners,  pickers  and  so  on…   •  All  controls  can  be  found  in  layout   pale[e   •  Use  drag  and  drop  to  set  controls  to   layout  or  edit  your  XML  file  directly   Android  Programming,  Pasi  Manninen.   63  
  • 64. Handling  UI  Events   •  Many  ways  to  intercept  the  events  from  a   user's  interacDon  (depends  the  SDK  version)   •  The  approach  is  to  capture  the  events  from   the  specific  View  object  that  the  user  interacts   with   •  Use  different  EventListeners  with  code  or  in   XML   –  onClick,  onLongClick,  onFocusChange,  …   Android  Programming,  Pasi  Manninen.   64  
  • 65. Example:  Handling  UI  Events   •  Only  with  coding   //  Create  an  implementaDon  of  OnClickListener   private  OnClickListener  buOon1Listener  =  new  OnClickListener()  {          public  void  onClick(View  v)  {                  //  Do  something  when  the  bu[on  is  clicked                  Toast.makeText(getApplicaDonContext(),  "Bu[on  1  clicked",                            Toast.LENGTH_SHORT).show();          }   };     @Override   protected  void  onCreate(Bundle  savedInstanceState)  {      super.onCreate(savedInstanceState);      setContentView(R.layout.acDvity_main);      //  Capture  our  bu[on  from  layout      Bu[on  buOon1  =  (Bu[on)findViewById(R.id.bu[on1);      //  Register  the  onClick  listener  with  the  implementaDon  above      buOon1.setOnClickListener(buOon1Listener);   }   •  With  XML  and  code   <Bu[on                  android:id="@+id/bu[on2"                  android:layout_width="wrap_content"                  android:layout_height="wrap_content"                  android:layout_below="@+id/bu[on1"                  android:layout_centerHorizontal="true"                  android:layout_marginTop="19dp"                  android:onClick="buOon2Clicked"                  android:text="OnClick  with  XML"  />   public  void  buOon2Clicked(View  view)  {      Toast.makeText(getApplicaDonContext(),              "Bu[on  2  clicked",  Toast.LENGTH_SHORT).show();   }   Android  Programming,  Pasi  Manninen.   65  
  • 66. Bu[ons  Example   •  Different  bu[on  types   –  Bu[on  with  text   –  Bu[on  with  text  and  image   –  ImageBu[on  with  image   •  Responding  Click  Events   –  onClick   –  OnClickListener   Android  Programming,  Pasi  Manninen.   66  
  • 67. Text  Fields  Example   •  Use  TextView  to  show  text   •  Use  EditText  to  ask  text  from  the  end  user   –  automaDcally  displays  the  keyboard   –  different  input  types  (android:inputType)   •  text,  textEmailAddress,  textUri,  number,  phone   •  textCapSentences,  textCapWords,   textAutoCorrecDon,  textPassword,     textMulDline   –  text  selecDon   •  cut,  copy,  paste   –  auto-­‐compleDon   •  use  AutoCompleteTextView   •  define  the  array  that  contains  all  text   suggesDons  in  strings  resource   •  create  String  based  Adapter  in  code  and   and  use  it  in  your  AutoCompleteTextView   Android  Programming,  Pasi  Manninen.   67  
  • 68. CheckBoxes,  Radio  and  Toggle  Bu[ons   Example   •  Checkboxes  allow  the  user  to   select  one  or  more  opDons   from  a  set   •  Radio  bu[ons  allow  the  user   to  select  one  opDon  from  a  set   •  A  toggle  bu[on  or  switch   allows  the  user  to  change  a   serng  between  two  states   Android  Programming,  Pasi  Manninen.   68  
  • 69. Spinner  Example   •  Provides  a  quick  way  to   select  one  value  from  a  set   •  Touching  the  spinner  displays   a  dropdown  menu  with  all   other  available  value  (user   can  select  one)   •  Define  String  resource  and   create  String  based  Adapter   in  code  and  use  it  to  your   Spinner   Android  Programming,  Pasi  Manninen.   69  
  • 70. Pickers  Example   •  Android  provides  controls  for   the  user  to  pick  a  Dme  or  pick   a  date  as  ready-­‐to-­‐use  dialogs   •  Using  these  pickers  helps   ensure  that  your  users  can   pick  a  Dme  or  date  that  is   valid,  forma[ed  correctly,   and  adjusted  to  the  user's   locale   •  It  is  recommended  to  use   DialogFragment  to  display   Dme  or  date  picker     Android  Programming,  Pasi  Manninen.   70  
  • 71. Exercise:  Basic  UI  Controls   •  RadioBu[ons  and  selecDon   •  Different  texts  controls   Android  Programming,  Pasi  Manninen.   71  
  • 72. 7.  Menus,  NoDfying  and  Dialogs   •  Working  with  Menus   –  –  –  –  –  –  –  Using  OpDons  Menu   Example:  OpDons  Menu  with  XML   Contextual  Menus   Using  Context  Menu   Examples  with  Context  Menu   Using  Contextual  AcDon  Mode   Using  PopUp  Menu   •  NoDfying  the  User   •  NoDfying  the  user  with  Dialogs   –  NoDfying  with  the  Toast   –  Example:  Display  Toasts   –  Using  NoDficaDon  Area   –  –  –  –  –  –  –  CreaDng  a  Dialog   Examples:  AlertDialog  with  Bu[ons  and  List   Custom  Dialog  Layouts   Examples:  Custom  Dialog   Passing  Events  back  to  Dialog’s  Host   ProgressDialog,  ProgressBar  and  Indicators   Example:  ProgressBar   Android  Programming,  Pasi  Manninen.   72  
  • 73. Working  with  Menus   •  OpDons  Menu   –  primary  set  of  menu  items  for  an  AcDvity   –  opened  by  pressing  the  device  MENU  key   –  two  groups  of  menu  items:     •  Icon  Menu   •  Expanded  Menu   •  Context  Menu   –  floaDng  list  of  menu  items     –  appear  when  you  perform  a  long-­‐press  on  a  View  (like  right  click  in  PC)   •  PopUp   –  a  modal  menu  anchored  to  a  View   –  appears  below  the  anchor  view  if  there  is  room,  or  above  the  view   otherwise   Android  Programming,  Pasi  Manninen.   73  
  • 74. Using  OpDons  Menu   •  Implemented  by  onCreateOp:onsMenu()  callback,  which  receives   an  instance  of  Menu   •  Menu  can  be  structured  with  coding  or  using  XML  resource  (save  to   menu  folder)   •  Use  XML  to  get   –  easier  to  visualize  the  menu  structure     –  separates  the  content  for  the  menu  from  your  applicaDon's  behavioral   code   –  allows  you  to  create  alternaDve  menu  configuraDons  for  different   pla`orm     •  Menu  selecDon  will  call  onOp:onsItemSelected(MenuItem)   funcDon   •  Android  3.0  (API  level  11)  and  higher,  items  from  the  opDons  menu   can  be  available  in  the  acDons  bar   (android:showAsAcDon=“ifRoom”)   Android  Programming,  Pasi  Manninen.   74  
  • 75. Example:  OpDons  Menu  with  XML   <!–  res/menu/main.xml  -­‐-­‐>   <menu          xmlns:android="hTp://schemas.android.com/apk/res/android"  >          <item                  android:id="@+id/acPon_video"                  android:orderInCategory="100"                  android:showAsAcDon="ifRoom"                  android:icon="@drawable/video"                  android:Dtle="@string/acPon_video"/>        <item  …        <item                  android:id="@+id/acPon_quit"                  android:orderInCategory="100"                  android:showAsAcDon="never"                  android:Dtle="@string/acPon_quit"/>   /*  Create  menu  items  */   public  boolean  onCreateOpDonsMenu(Menu  menu)  {            //  load  menu  from  resources            MenuInflater  inflater  =  getMenuInflater();            inflater.inflate(R.menu.main,  menu);            return  true;   }   Android  Programming,  Pasi  Manninen.   75  
  • 76. Example:  OpDons  Menu  with  XML   •  Use  Resource  Id’s  in  onOpDonsItemSelected()        /**  Handle  item  selecDons  */          public  boolean  onOpDonsItemSelected(MenuItem  item)  {                  switch  (item.getItemId())  {                          case  R.id.ac:on_music:                                  Toast.makeText(getBaseContext(),  "Music",  Toast.LENGTH_SHORT).show();                                  return  true;                          case  R.id.ac:on_video:                                  Toast.makeText(getBaseContext(),  “Video",  Toast.LENGTH_SHORT).show();                                  return  true;                          case  R.id.ac:on_quit:                                  Toast.makeText(getBaseContext(),  "Quit",  Toast.LENGTH_SHORT).show();                                  return  true;                  }                  return  false;          }   Android  Programming,  Pasi  Manninen.   76  
  • 77. Contextual  Menus   •  Offers  acDons  that  affect  a  specific  item  or   context  frame  in  the  UI   •  OMen  used  with  List  or  Grid  Views   •  Two  different  ways  to  use   –  floaDng  context  menu  (list  of  menu  items  showed   when  user  performs  a  long  press),  one  selecDon   –  contextual  acDon  mode    (Android  3.0  or  higher),     with  mulDple  selecDon   Android  Programming,  Pasi  Manninen.   77  
  • 78. Using  Context  Menu   •  Override  the  AcDvity's  context  menu  callback   methods:   –  onCreateContextMenu(ContextMenu,View,Conte xtMenuInfo)   –  onContextItemSelected(MenuItem)   •  Use  registerForContextMenu()  method  to   register  ContextMenu  to  View   •  Menu  can  be  structured  with  coding  or  using   XML  resource  (save  to  menu  folder)   Android  Programming,  Pasi  Manninen.   78  
  • 79. Example:  Context  Menu   •  Populate  ListView  with  ArrayAdapter   •  AcDvity  should  extend  ListAcDvity   ListView  in  Layout   //  create  string  array   private  String  []  items=  {"John  Koch","Peter  Michell","Ina  kudson”};     @Override   public  void  onCreate(Bundle  savedInstanceState)  {          setListAdapter(new  ArrayAdapter<String>  (                  this,                  android.R.layout.simple_list_item_1,                  items));                  //  register  contextMenu  for  this  ListAcDvity                  registerForContextMenu(getListView());          }   Android  Programming,  Pasi  Manninen.   79  
  • 80. Example:  Context  Menu   •  Context  Menu  menu  items  can  be  added  like   in  OpDons  Menu  (coding  or  XML)   Long  press  here   <?xml  version="1.0"  encoding="u/-­‐8"?>   <menu  …>          <item                  android:id="@+id/acPon_call"                  android:orderInCategory="100"                  android:Dtle="@string/acPon_call"/>          <item    ….               </menu>   @Override   public  void  onCreateContextMenu(ContextMenu  menu,  View  v,                                            ContextMenuInfo  menuInfo)  {          super.onCreateContextMenu(menu,  v,  menuInfo);          MenuInflater  inflater  =  getMenuInflater();          inflater.inflate(R.menu.context_menu,  menu);   }   Android  Programming,  Pasi  Manninen.   80  
  • 81. Example  Context  Menu   •  Get  the  selecDon  with  onContextItemSelected   @Override   public  boolean  onContextItemSelected(MenuItem  item)  {          //  context  menu  item          AdapterContextMenuInfo  info  =  (AdapterContextMenuInfo)                    item.getMenuInfo();          //  text  view  in  list          TextView  textView  =  (TextView)  info.targetView;          //  name  in  textview          String  name  =  textView.getText().toString();          //  acDon          switch  (item.getItemId())  {                  case  R.id.acPon_call:                  Toast.makeText(getBaseContext(),  "Call  to  "+name,  Toast.LENGTH_SHORT).show();                  return  true;   ...   Android  Programming,  Pasi  Manninen.   81  
  • 82. Using  Contextual  AcDon  Mode   •  In  specific  view   –  implement  AcDonMode.Callback   interface   •  specify  acDons  with  XML  resource   (menu)   •  respond  click  events  on  acDon   items   •  handle  prepare  and  destroy  events   –  call  startAcDonMode  to  show   the  acDon   •  Example  in  source  codes   Android  Programming,  Pasi  Manninen.   82  
  • 83. Using  PopUp  Menu     •  Define  menu  with  XML   •  Call  PopUp  constructor  and   set  menu  item  click  listener   •  Show  PopUp  menu   •  Handle  selecDon  with   onMenuItemClick   •  Example  in  source  codes   Note:  This  is  not  the  same  as  a  context  menu,  which  is   generally  for  acPons  that  affect  selected  content.  For  acPons   that  affect  selected  content,  use  the  contextual  acPon  mode   or  floaPng  context  menu.   Android  Programming,  Pasi  Manninen.   83  
  • 84. NoDfying  the  User   •  It  is  good  to  noDfy  the  user  about  an  event  that   occurs  in  your  applicaDon   –  Saving  a  file  is  complete   –  ApplicaDon  is  running  in  the  background  and  needs   response  from  user   –  ApplicaDon  is  performing  work  that  the  user  must   wait  for   •  Android  offers  a  few  basic  techniques:   –  Toast  NoDficaDon     –  Using  NoDficaDon  Area   –  Dialog  NoDficaDon   Android  Programming,  Pasi  Manninen.   84  
  • 85. NoDfying  the  user  with  Toast   •  A  toast  noDficaDon  is  a  message  that  pops  up  on  the   window   •  Fills  the  amount  of  space  required  for  the  message   •  User's  current  acDvity  remains  visible  and  interac:ve   •  NoDficaDon  automaDcally  fades  in  and  out   •  Can  be     –  fired  from  background  service   –  posiDoned  differently   –  customized   Android  Programming,  Pasi  Manninen.   85  
  • 86. Example:  Display  Toasts   //  get  applicaDon  context   Context  context  =  getApplicaDonContext();   //  toast  message   CharSequence  text  =  "Long  Toast";   //  duraDon   int  duraDon  =  Toast.LENGTH_SHORT;   //  create  toast   Toast  toast  =  Toast.makeText(context,text,duraPon);   //  show  toast   toast.show();       //  or   Toast.makeText(context,text,duraDon).show();     //  posiDon  top  leM  corner   toast.setGravity(Gravity.BOTTOM|Gravity.LEFT,  0,  0);     Android  Programming,  Pasi  Manninen.   86  
  • 87. Example:  Display  Custom  Toast   //  toast.xml  –  layout  file   <?xml  version="1.0"  encoding="UTF-­‐8"?>   <LinearLayout  …          android:id="@+id/toast”  …  >          <ImageView  android:id="@+id/image”  …  />          <TextView  android:id="@+id/text”  …/>   </LinearLayout>     get  layout  from  AcDvity   //   •  Make  layout   •  Get  layout  from   AcDvity   •  Set  Image  and  Text   •  Show  toast   LayoutInflater  inflater  =  getLayoutInflater();   //  inflate  layout  from  XML  (toast.xml)   View  layout  =  inflater.inflate(R.layout.toast,  (ViewGroup)  findViewById(R.id.toast));   //  get  image  from  resource   ImageView  image  =  (ImageView)  layout.findViewById(R.id.image);   image.setImageResource(R.drawable.ptm);   //  set  text   TextView  text  =  (TextView)  layout.findViewById(R.id.text);   text.setText("This  is  a  custom  PTM  toast!");   //  create  and  show  toast   Toast  toast  =  new  Toast(getApplicaDonContext());   toast.setGravity(Gravity.CENTER_VERTICAL,  0,  0);   toast.setDuraDon(Toast.LENGTH_LONG);   toast.setView(layout);   toast.show();   Android  Programming,  Pasi  Manninen.   87  
  • 88. Using  NoDficaDon  Area   •  Adds  an  icon  to  the  system's  status  bar  and  an  expanded   message  in  the  NoDficaDons  window   •  Configurable:  alert  the  user  with  a  sound,  vibraDon  or   flashing  light  in  device   •  Own  layout  can  be  used  like  in  Toast  NoDficaDons   •  Ideal  when  applicaDon  is  working  in  a  background  Service   and  needs  to  no:fy  the  user  about  an  event   •  When  the  user  selects  the  expanded  message,  Android   fires  an  Intent  that  is  defined  by  the  noDficaDon  (usually  to   launch  an  AcDvity)   •  Design  guidelines   –  h[p://developer.android.com/design/pa[erns/ noDficaDons.html     Android  Programming,  Pasi  Manninen.   88  
  • 89. Example:  NoDficaDon  Area   •  Create  noDficaDon  from  a  Bu[on   •  Open  AcDvity  from  noDficaDon   •  Programming   –  Use  NoDficaDonCombat.Builder  to   set  icon,  Dtle  and  text  of  noDficaDon   –  Use  TaskStackBuilder  and   PendingIntent  to  start  AcDvity  when   user  clicks  noDficaDon   –  Use  NoDficaDonManager  to  lauch   noDficaDon   Android  Programming,  Pasi  Manninen.   89  
  • 90. NoDfying  the  user  with  Dialogs   •  Usually  appears  in  front  of  the  current  AcDvity   •  Underlying  AcDvity  loses  focus   •  Ask  user  to  make  a  decision  or  enter  addiDonal   informaDon   •  How  to   –  use  DialogFragment  as  a  container  of  your  dialog   –  use  AlertDialog  as  a  style  and  structure  of  your  dialog   –  use  DatePickerDialog  or  TimePickerDialog  in  special  case   –  Dialog  class  is  base  class  behind  all  dialogs   •  Design  guidelines   –  h[p://developer.android.com/design/building-­‐blocks/ dialogs.html     Android  Programming,  Pasi  Manninen.   90  
  • 91. CreaDng  a  Dialog   •  Extend  your  own  class  from  DialogFragment   •  Create  for  example  AlertDialog  in   onCreateDialog()  method   •  Create  instance  of  your  class  and  call  show()   method   •  AlertDialog’s  bu[on  touches  will  automaDcally   dismiss  the  dialog  for  you   Android  Programming,  Pasi  Manninen.   91  
  • 92. Example:  AlertDialog  with  Bu[ons   •  Three  regions   –  Title,  Content  area  and  AcDon  bu[ons   public  class  ExitDialogFragment  extends  DialogFragment  {          @Override          public  Dialog  onCreateDialog(Bundle  savedInstanceState)  {                  AlertDialog.Builder  builder  =  new  AlertDialog.Builder(getAcDvity());                  builder.setTitle(R.string.dialog_Ptle)                                          .setMessage(R.string.dialog_exit)                                          .setPosiDveBu[on(R.string.dialog_yes,  new  DialogInterface.OnClickListener()  {                                              public  void  onClick(DialogInterface  dialog,  int  id)  {  //  Close  applicaDon  }                                          })                                          .setNegaDveBu[on(R.string.dialog_cancel,  new  DialogInterface.OnClickListener()  {                                            public  void  onClick(DialogInterface  dialog,  int  id)  {  //  User  cancelled  the  dialog  }                                });                  //  Create  the  AlertDialog  object  and  return  it                  return  builder.create();          }   public  void  exitDialog(View  view)  {   }          ExitDialogFragment  eDialog  =  new  ExitDialogFragment();          eDialog.show(getFragmentManager(),  "exit");   }     Android  Programming,  Pasi  Manninen.   92  
  • 93. Example:  AlertDialog  with  List   •  Create  list  in  String  resources   public  class  ListDialogFragment  extends  DialogFragment  {      @Override      public  Dialog  onCreateDialog(Bundle  savedInstanceState)  {              AlertDialog.Builder  builder  =  new  AlertDialog.Builder(getAcDvity());              builder.setTitle(R.string.listdialog_Dtle)                      .setItems(R.array.messagetypes,  new  DialogInterface.OnClickListener()  {                              public  void  onClick(DialogInterface  dialog,  int  index)  {                                      Resources  res  =  getResources();                                      String[]  messageTypes  =  res.getStringArray(R.array.messagetypes);                                      Toast.makeText(getAcDvity(),  "Selected  messageType  =  "  +  messageTypes[index],                                                      Toast.LENGTH_SHORT).show();                              }              });              return  builder.create();            }   }   Android  Programming,  Pasi  Manninen.   93  
  • 94. Custom  Dialog  Layouts   •  Create  your  own  layout  for  the  dialog  window   with  layout  and  widget  elements   •  Add  layout  to  AlertDialog  with  setView()   method   •  Custom  layout  fills  the  dialog,  use   AlertDialog.builder  to  add  bu[ons     •  Note   –  Normal  AcDvity  can  be  used  as  a  Dialog  also   <acDvity  android:theme="@android:style/Theme.Holo.Dialog"  >   Android  Programming,  Pasi  Manninen.   94  
  • 95. Example:  Custom  Dialog   public  class  TeamDialogFragment  extends  DialogFragment  {      @Override      public  Dialog  onCreateDialog(Bundle  savedInstanceState)  {          AlertDialog.Builder  builder  =  new  AlertDialog.Builder(getAcDvity());          //  get  the  layout  inflater          LayoutInflater  inflater  =  getAcDvity().getLayoutInflater();          //  Inflate  and  set  the  layout  for  the  dialog          builder.setView(inflater.inflate(R.layout.add_team_dialog,  null))                                    .setTitle("Add  a  new  Team")                                  .setPosiDveBu[on("Add",  new  DialogInterface.OnClickListener()  {                                          @Override                                          public  void  onClick(DialogInterface  dialog,  int  id)  {  //  Add  a  team…  }                                            })                                .setNegaDveBu[on("Cancel",  new  DialogInterface.OnClickListener()  {                                          public  void  onClick(DialogInterface  dialog,  int  id)  {    }                                            });                        return  builder.create();            }   }   //  add_team_dialog.xml   <RelaDveLayout…        <TextView…        <EditText…     Android  Programming,  Pasi  Manninen.   95  
  • 96. Passing  events  back  to  dialog’s  host   •  Dialog  might  perform  the  necessary   acDon  itself  (oMen  you  want  to  send   informaDon  back  to  the  AcDvity  which   opened  the  dialog)   –  Define  an  interface  with  a  method  for   each  type  of  click  event   –  Implement  interface  in  the  host   component  that  will  receive  the  acDon   events  from  the  dialog   –  Override  DialogFrament’s  onA[ach()-­‐ method  to  instanDate  your  listener   (send  events  to  host)     //  look  DialogsExample  in  CustomDialog  project  (Custom  Dialog,  Add  a  new  Team)     Android  Programming,  Pasi  Manninen.   96  
  • 97. ProgressDialog   •  ProgressDialog  is  an  extension  of  AlertDialog  class   •  Use  in  following  cases:   –  display  a  progress  animaDon  (a  spinning  wheel  or  a   progress  bar)   •  ProgressDialog  can  also  provide  bu[ons,  such  as   one  to  cancel  a  download   •  To  show  ProgressDialog  call  simply   ProgressDialog.show()     Note!   –  Android  Progress  &  AcDvity  guidelines  says  ”avoid”   for  ProgressDialog  use   –  You  should  use  ProgressBar  instead  (inside  your   acDvity)   Android  Programming,  Pasi  Manninen.   97  
  • 98. ProgressBar  and  Indicators   •  Use  Progress  bars  and  acDvity  indicators  to   signal  to  users  that  something  is  happening   •  A  progress  bar  should  always  fill  from  0%  to   100%  and  never  move  backwards  to  a  lower   value   •  Design  guidelines:   –  h[p://developer.android.com/design/building-­‐ blocks/progress.html     Android  Programming,  Pasi  Manninen.   98  
  • 99. Example:  ProgressBar   •  Sample  layout  with  ProgressBar   •  Show  also  systems  ProgressBar  (circle)   •  Simulate  loading  with  Thread   @Override   protected  void  onCreate(Bundle  savedInstanceState)  {      super.onCreate(savedInstanceState);      //  acDvate  indeterminate  progress      getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS);      //  set  layout  for  the  acDvity      setContentView(R.layout.acDvity_main);          //  get  progressbar      progressBar  =  (ProgressBar)  findViewById(R.id.progressBar1);   }   //  start  "loading...”     public  void  startBu[onPressed(View  view)  {      MainAcDvity.this.setProgressBarIndeterminateVisibility(true);      startLoading();   }     Android  Programming,  Pasi  Manninen.   99  
  • 100. 8.  Understanding  of  the  Intents   •  Intents   •  Intent  Object   –  AcDons,  Data,  Category,  Extras   •  •  •  •  •  •  •  •  •  Intent  ResoluDon,  Filters,  Matching   Intent  AcDons,  Data  and  Category  test   Example:  Intent  Filters  in  Manifest   Using  Explicit  Intent  to  start  a  new  AcDvity   Data  transfer  between  AcDviDes   ExpecDng  a  result  from  an  AcDvity   Using  Implicit  Intents   Custom  and  Broadcast  Intents  with  Receivers   Examples  and  Exercises  with  AcDviDes  and  Intents   Android  Programming,  Pasi  Manninen.   100  
  • 101. Intents   •  The  most  unique  and  important  concept  in   Android  Development   •  Intent  can  be  used  to   –  broadcast  data  between  applicaDons  and   applicaDon  components   –  start  AcDviDes  and  Services   –  broadcast  that  an  event  or  acDon  has  occured   •  Intent  is  like  a  ”message”  to  someone   Android  Programming,  Pasi  Manninen.   101  
  • 102. Intent  Object   •  An  Intent  object  is  a  bundle  of  informaDon   •  Object  contains:   –  Component,  specifies  an  explicit  name  of  a  component  class  to  use   for  the  intent   –  Ac:on,  acDon  to  be  performed  (Androids  own  or  your  own  defined)   –  Data,  the  URI  of  the  data  to  be  acted  on  and  the  MIME  type  of  that   data   –  Category,  a  string  containing  addiDonal  informaDon  about  the  kind  of   component  that  should  handle  the  intent   –  Type,  specifies  an  explicit  type  (a  MIME  type)  of  the  intent  data   –  Extras,  key-­‐value  pairs  for  addiDonal  informaDon  that  should  be   delivered  to  the  component  handling  the  intent   –  Flags,  instruct  the  Android  system  how  to  launch  an  acDvity  and  how   to  treat  it   Android  Programming,  Pasi  Manninen.   102  
  • 103. Intent  Object  AcDons   •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  ACTION_MAIN   ACTION_VIEW   ACTION_ATTACH_DATA   ACTION_EDIT   ACTION_PICK   ACTION_CHOOSER   ACTION_GET_CONTENT   ACTION_DIAL   ACTION_CALL   ACTION_SEND   ACTION_SENDTO   ACTION_ANSWER   ACTION_INSERT   ACTION_DELETE   ACTION_RUN   •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  ACTION_TIME_TICK   ACTION_TIME_CHANGED   ACTION_TIMEZONE_CHANGED   ACTION_BOOT_COMPLETED   ACTION_PACKAGE_ADDED   ACTION_PACKAGE_CHANGED   ACTION_PACKAGE_REMOVED   ACTION_PACKAGE_RESTARTED   ACTION_SYNC   ACTION_PICK_ACTIVITY   ACTION_SEARCH   ACTION_WEB_SEARCH   ACTION_FACTORY_TEST   …   fi.ptm.project.SHOW_COLOR   Android  Programming,  Pasi  Manninen.   103  
  • 104. Intent  Object  Data   •  Some  examples  of  acDon/data  pairs   –  ACTION_VIEW  content://contacts/people/1   –  ACTION_DIAL  content://contacts/people/1     –  ACTION_VIEW  tel:123   –  ACTION_DIAL  tel:123   –  ACTION_EDIT  content://contacts/people/1   –  ACTION_VIEW  content://contacts/people/   Android  Programming,  Pasi  Manninen.   104  
  • 105. Intent  Object  Category   •  Any  number  of  category  descripDons  can  be   placed  in  an  Intent  object   •  Gives  addiDonal  informaDon  about  the  acDon  to   execute   •  •  •  •  •  •  •  CATEGORY_DEFAULT   CATEGORY_BROWSABLE   CATEGORY_TAB   CATEGORY_ALTERNATIVE   CATEGORY_SELECTED_ALTERNATIVE   CATEGORY_LAUNCHER   CATEGORY_INFO   •  •  •  •  •  •  •  CATEGORY_HOME   CATEGORY_PREFERENCE   CATEGORY_TEST   CATEGORY_CAR_DOCK   CATEGORY_DESK_DOCK   CATEGORY_CAR_MODE   …   Android  Programming,  Pasi  Manninen.   105  
  • 106. Intent  Object  Extras   •  Extras  is  a  Bundle  of  any  addiDonal   informaDon   •  Used  to  provide  extended  informaDon  to  the   component   Intent  intent  =  new  Intent(Intent.ACTION_SENDTO);   intent.setData(Uri.parse("sms://"));             intent.putExtra("address",  "04012345678");             intent.putExtra("sms_body",  "Your  message  here");         startAcDvity(intent);   Android  Programming,  Pasi  Manninen.       106  
  • 107. Intent  ResoluDon   •  Intents  can  be  divided  into  two  groups   –  Explicit  intents,  are  typically  used  for  applicaDon-­‐internal   messages  (between  AcDviDes)   –  Implicit  intents,  are  oMen  used  to  acDvate  components  in   other  applicaDons   •  Android  delivers  an  explicit  intent  to  an  instance  of  the   designated  target  class   •  Implicit  intents  are  resolved  by  Intent  Filters  by  Intent   object’s  AcDon,  Data  and  Category     •  In  implicit  intents,  Android  system  must  find  the  best   component  (or  components)  to  handle  the  intent   Android  Programming,  Pasi  Manninen.   107  
  • 108. Intent  Filters   •  An  explicit  intent  is  always  delivered  to  its  target,  no   ma[er  what  it  contains   •  AcDviDes,  services,  and  broadcast  receivers  can  have   one  or  more  intent  filters   •  Each  filter  describes  a  capability  of  the  component,  a   set  of  intents  that  the  component  is  willing  to  receive   •  Component  has  separate  filters  for  each  job  it  can  do   •  Intent  Filters  are  set  up  in  the  applica:on's  manifest   file  (AndroidManifest.xml)  as  <intent-­‐filter>  elements     •  An  implicit  intent  is  tested  against  acDon,  data  and   category  fields  in  manifest   Android  Programming,  Pasi  Manninen.   108  
  • 109. Intent  Matching   •  Intents  are  matched  against  intent  filters  not   only  to  discover  a  target  component  to  acDvate,   but  also  to  discover  something  about  the  set  of   components  on  the  device   •  Examples:   •    AcDvity  is  shown  in  applicaDon  launcher  (”menu  in  screen”)   –  android.intent.acDon.MAIN   –  android.intent.category.LAUNCHER"   •  AcDvity  handles  widgets  Update  (Dme  based)  and  two  own   “Receiver”  and  “Reload”  Intents   –  android.appwidget.acDon.APPWIDGET_UPDATE   –  fi.ptm.namedays.ACTION_WIDGET_RECEIVER   –  fi.ptm.namedays.ACTION_WIDGER_RELOAD     Android  Programming,  Pasi  Manninen.   109  
  • 110. Intent  Filters  –  AcDon  Test   •  To  pass  this  test,  the  Intent  object  must  match  one  of   the  acDons  listed  in  the  filter  (in  manifest)   •  An  Intent  object  that  doesn't  specify  an  acDon   automaDcally  passes  the  acDon  test,  as  long  as  the   filter  contains  at  least  one  acDon   •  Intent  filter  must  contain  at  least  one  acDon  element   <intent-­‐filter  .  .  .  >          <acDon  android:name="com.example.project.SHOW_CURRENT"  />          <acDon  android:name="com.example.project.SHOW_RECENT"  />          <acDon  android:name="com.example.project.SHOW_PENDING"  />          .  .  .   </intent-­‐filter>   Android  Programming,  Pasi  Manninen.   110  
  • 111. Intent  Filters  –  Category  Test   •  To  pass  the  category  test,  every  category  in  the   Intent  object  must  match  a  category  in  the  filter   •  AddiDonal  categories  can  be  listed,  but  it  cannot   omit  any  that  are  in  the  intent   <intent-­‐filter  .  .  .  >          .  .  .              <category  android:name="android.intent.category.DEFAULT"  />          <category  android:name="android.intent.category.BROWSABLE"  />          .  .  .   </intent-­‐filter>   Android  Programming,  Pasi  Manninen.   111  
  • 112. Intent  Filters  –  Data  test   •  Like  the  acDon  and  categories,  the  data  specificaDon  for  an  intent   filter  is  contained  in  a  subelement,  it  can  appear  mulDple  Dmes,  or   not  at  all   •  Each  <data>  element  can  specify  a  URI  and  a  data  type  (MIME   media  type)   •  When  the  URI  in  an  Intent  object  is  compared  to  a  URI  specificaDon   in  a  filter,  it's  compared  only  to  the  parts  of  the  URI  actually   menDoned  in  the  filter   <intent-­‐filter>          <acDon  android:name="android.intent.acDon.VIEW"  />          <category  android:name="android.intent.category.DEFAULT"  />          <category  android:name="android.intent.category.BROWSABLE"  />          <data  android:scheme="h[p"  />          <data  android:mimeType="audio/mp3"/>          <data  android:mimeType="audio/mp4"/>   ...   Android  Programming,  Pasi  Manninen.   112  
  • 113. Example:  Intent  Filters  in  Manifest   <?xml  version="1.0"  encoding="u/-­‐8"?>   <manifest  …>          <applicaDon  ...>                  <acDvity  android:name=".MainAcPvity“                                                     MainAcDvity  can  be  lauched  from  the                                                  android:label="@string/app_name">   menu  and  it  can  also  handle  intent  that                          <intent-­‐filter>   is  describe  below.                                  <!–  start  as  main  entry  point  -­‐-­‐>                                    <acDon  android:name="android.intent.acPon.MAIN"  />                                  <category  android:name="android.intent.category.LAUNCHER"  />                          </intent-­‐filter>                          <intent-­‐filter>                                  <acDon  android:name="fi.ptm.PLAY_SOUND"  />                                  <data  android:mimeType="audio/mp3"  android:scheme="hTp"/>                                    <category  android:name="android.intent.category.DEFAULT"  />                                                            </intent-­‐filter>   </applicaDon>   </manifest>     Intent  intent  =  new  Intent(”fi.ptm.PLAY_SOUND”,                            Uri.parse(”h[p://somedomain.com/music.mp3”));   startAcDvity(intent);       Android  Programming,  Pasi  Manninen.   113  
  • 114. Using  explicit  Intent  to  start  a  new   AcDvity   •  startAcDvity(intent)   –  starts  a  new  AcDvity   –  it  is  possible  to  send  data  to  with  intent   –  acDvity  will  not  be  informed  when  the  child   acDvity  is  complete   •  startAcDvityForResult(intent,  REQUEST_CODE)   –  like  previous,  but  it  takes  unique  constant   –  acDvity  will  be  noDfied  when  the  child  acDvity  is   complete  via  the  onAcDvityResult()  callback   Android  Programming,  Pasi  Manninen.   114