Android Development
- the basics
Tomáš Kypta
Agenda
• Android platform and ecosystem	

• Android SDK and development tools	

• Hello World	

• building blocks of Andro...
Agenda
• dialogs, toasts, notifications	

• fragments
Android platform
• Linux-based operating system	

• open-source (http://source.android.com/)	

• originally phone OS	

• t...
History
• 2003, Android inc.	

• 2005, acquired by Google	

• Sep 2008, the first Android phone	

• T-Mobile G1	

• May 201...
History
• Oct 2011, Ice Cream Sandwich (Android
4.0)	


• July 2012, Jelly Bean (Android 4.1)	

• July 2013, Jelly Bean (A...
Platform Versions
Android ecosystem
• thousands of devices	

• the most popular mobile platform	

• 1.5 million new devices activated every ...
Google Play
• app can be acquired by app stores	

• Google Play, http://play.google.com	

• other stores	

• > 50 billion ...
Google Play
• purchasing	

• selling	

• Play Music, Play Books, ...
Google Play
• selling apps	

• 15 min return period	

• ads	

• AdMob, ...	

• in-app billing
Android “problems”
• fragmentation	

• manufacturer/carrier enhancements	

• updates & support	

• openness - low quality ...
Android security
• app can be installed directly	

• .apk file	

• user accepts app permissions when
installing or updating...
Android security
• Verify Apps (Android 2.3+)	

• checks every app install	

• Google Play can remotely uninstall harmful
...
Development
• programming in “Java”	

• native apps possible (C++)	

• development tools platform friendly	

• Windows, Li...
Development
• IDE support	

• Android Studio, IntelliJ IDEA, ADT plugin
for Eclispse, Netbeans	


• you can freely develop...
Android SDK
• android - Android SDK and AVD Manager	

• adb - Android Debug Bridge	

• monitor - (ddms & hierarchyviewer)	...
Libraries
• compatibility libraries	

• v4 - backports lots of newer functionality
to Android 1.6+	


• Google Play In-app...
Libraries
• Google Play Services	

• Google Maps	

• Games	

• Google+	

• Authorization
Libraries
• AdMob	

• Google Analytics, Flurry, Crittercism	

• Google Cloud Messaging
Hello World
Build
Android building blocks
• Activity	

• Service	

• Content provider	

• Broadcast receiver	

• AndroidManifest.xml
Activity
• screen with user interface	

• the only visual component	

• example - an email app	

• list of emails	

• deta...
Service
• has no UI	

• long-running tasks	

• examples	

• music playback service	

• download service	

• sync service
Content Provider
• managers and shares application data	

• data storage doesn’t matter (db, web,
filesystem)	


• apps can...
Broadcast Receiver
• responds to broadcasts	

• broadcasts are system wide	

• can be registered statically or dynamically...
AndroidManifest.xml
• defines what parts the app have	

• defines which endpoints are exposed	

• minimum/maximum API level	...
Intent
• asynchronous message	

• binds components together (all except
Content Provider)	


• starting activities	

• sta...
Activity
• a subclass of android.app.Activity	

• app usually has many activities	

• activities managed in activity stack...
Activity Lifecycle
• activity can be in different states during its
lifecycle	


• foreground, visible, stopped, killed	

...
Activity callbacks
• onCreate() - activity created	

• onStart() - activity visible for the user	

• onResume() - activity...
Activity callbacks
• onDestroy() - before activity is
destroyed	


• onRestart() - called if activity was

previously stop...
Configuration changes
• when configuration changes, activities are
destroyed and recreated	


• default behaviour, can be ch...
Intent & Activity
• starting activity explicitly	

•

new Intent(context, MyActivity.class)!

•

new Intent(Intent.ACTION_...
User Interface
• defined by a hierarchy of views	

• layouts = containers	

•

LinearLayout, RelativeLayout, FrameLayout, ....
User Interface
• widgets	

• UI objects	

• Button, TextView, EditText,
RadioButton, ...	


• WebView
User Interface
• list widgets	

• subclasses of AdapterView	

• display a list of items	

• use adapter to bind list do da...
Resources
• drawables	

• bitmaps	

• 9-patch png	

• state lists	

• layer lists	

• shape drawables
Resources
• layout	

• strings	

• colors	

• menus	

• dimensions	

• animations
Resources
• arrays	

• ids	

• raw	

• xml	

• ...
Screen sizes and
densities
Screen sizes and
densities
• How to handle different screen sizes and
densities?
Resources
• resources can be created in several
versions	


• the best version is selected according to
current device con...
Resource qualifiers
• suffixes for resource folders	

• drawables, drawable-mdpi, ...	

• values, values-cs	

• layout, layo...
Resource qualifiers
• screen density - ldpi, mdpi, hdpi, xhdpi, ...	

• screen size - small, normal, large, xlarge	

• scre...
Resource qualifiers
• since Android 3.2	

• w<N>dp - available screen width, w600dp	

• h<N>dp - available screen heights, ...
Resources
• accessed from code via generated R.java file
and resource ids	

• view.findViewById(R.id.txt_name)!
• txtName.s...
Android version
fragmentation
• How to handle different API levels available
on different devices?
Android version
fragmentation
• build target	

• API level the app is compiled against	

• AndroidManifest.xml	

•

<uses-...
Android version
fragmentation
• handling versions in code	

if (Build.VERSION.SDK_INT <
Build.VERSION_CODES.GINGERBREAD) {...
Android version
fragmentation
private boolean functionalitySupported = false;!

!
static {!
try {!
checkFunctionalitySuppo...
Fragments
• a piece of application UI	

• introduced to support more flexible UI	

• phones and tablets together in one app...
Fragments
• since Android 3.0	

• support library v4 backports them to
Android 1.6+
Threads
• main thread = UI thread	

• do not ever block the UI thread!!!	

• use worker threads for time consuming
operati...
Menu
• menu resource	

• Android < 3.0 the whole menu hidden
under menu button	


• ActionBar since Android 3.0	

• items ...
Menu
• behaviour for items that don’t fit in the
action bar	


• hidden under menu button (if the device
has one)	


• syst...
Dialogs
• floating window screen
Dialogs
• standard dialogs	

• custom dialogs	

• activity with dialog style	

• since fragments used via DialogFragment
Dialogs
• might be tedious to create	

• difficult to style	

• StyledDialogs	

•

https://github.com/inmite/android-styled...
Toast
• simple non-modal information	

• displayed for a short period of time	

• doesn’t have user focus
Notifications
• a message that can be displayed to the user
outside your normal UI	


• displayed in notification area
Notifications
• user can open notification drawer to see
the details	


• app can define UI and click action	

•

Notificatio...
Preferences
SharedPreferences prefs = PreferenceManager!
.getDefaultSharedPreferences(context);!

!
SharedPreferences pref...
Sources
• developer.android.com	

• android-developers.blogspot.com	

• source.android.com	

• stackoverflow.com	

• youtub...
THE END
Android development - the basics, MFF UK, 2013
Android development - the basics, MFF UK, 2013
Android development - the basics, MFF UK, 2013
Android development - the basics, MFF UK, 2013
Android development - the basics, MFF UK, 2013
Android development - the basics, MFF UK, 2013
Android development - the basics, MFF UK, 2013
Upcoming SlideShare
Loading in...5
×

Android development - the basics, MFF UK, 2013

765

Published on

Introductory lecture to Android development. Faculty of Mathematics and Physics in Prague, 2013.

Published in: Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
765
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
19
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Android development - the basics, MFF UK, 2013

  1. 1. Android Development - the basics Tomáš Kypta
  2. 2. Agenda • Android platform and ecosystem • Android SDK and development tools • Hello World • building blocks of Android apps & the manifest file • activities, widgets, intents
  3. 3. Agenda • dialogs, toasts, notifications • fragments
  4. 4. Android platform • Linux-based operating system • open-source (http://source.android.com/) • originally phone OS • tablet support (since Honeycomb, Android 3.0) • Google TV
  5. 5. History • 2003, Android inc. • 2005, acquired by Google • Sep 2008, the first Android phone • T-Mobile G1 • May 2010, Froyo (Android 2.2) • Feb 2011, Honeycomb (Android 3.0)
  6. 6. History • Oct 2011, Ice Cream Sandwich (Android 4.0) • July 2012, Jelly Bean (Android 4.1) • July 2013, Jelly Bean (Android 4.3) • Oct 2013, KitKat (Android 4.4)
  7. 7. Platform Versions
  8. 8. Android ecosystem • thousands of devices • the most popular mobile platform • 1.5 million new devices activated every day • September 3, 2013, 1 billion Android devices have been activated • most devices made by Samsung
  9. 9. Google Play • app can be acquired by app stores • Google Play, http://play.google.com • other stores • > 50 billion apps have been installed from Google Play • July 2013, 1 million apps
  10. 10. Google Play • purchasing • selling • Play Music, Play Books, ...
  11. 11. Google Play • selling apps • 15 min return period • ads • AdMob, ... • in-app billing
  12. 12. Android “problems” • fragmentation • manufacturer/carrier enhancements • updates & support • openness - low quality apps in Google Play • malware • users
  13. 13. Android security • app can be installed directly • .apk file • user accepts app permissions when installing or updating the app
  14. 14. Android security • Verify Apps (Android 2.3+) • checks every app install • Google Play can remotely uninstall harmful app
  15. 15. Development • programming in “Java” • native apps possible (C++) • development tools platform friendly • Windows, Linux, Mac OS X
  16. 16. Development • IDE support • Android Studio, IntelliJ IDEA, ADT plugin for Eclispse, Netbeans • you can freely develop on any device
  17. 17. Android SDK • android - Android SDK and AVD Manager • adb - Android Debug Bridge • monitor - (ddms & hierarchyviewer) • emulator • lint, Traceview, ProGuard • docs, samples
  18. 18. Libraries • compatibility libraries • v4 - backports lots of newer functionality to Android 1.6+ • Google Play In-app Billing
  19. 19. Libraries • Google Play Services • Google Maps • Games • Google+ • Authorization
  20. 20. Libraries • AdMob • Google Analytics, Flurry, Crittercism • Google Cloud Messaging
  21. 21. Hello World
  22. 22. Build
  23. 23. Android building blocks • Activity • Service • Content provider • Broadcast receiver • AndroidManifest.xml
  24. 24. Activity • screen with user interface • the only visual component • example - an email app • list of emails • details of an email • email composition
  25. 25. Service • has no UI • long-running tasks • examples • music playback service • download service • sync service
  26. 26. Content Provider • managers and shares application data • data storage doesn’t matter (db, web, filesystem) • apps can query and modify data through content provider • r/w permissions can be defined • examples - all system dbs (SMS, contacts, ...)
  27. 27. Broadcast Receiver • responds to broadcasts • broadcasts are system wide • can be registered statically or dynamically • system or custom messages • examples - incoming SMS, incoming call, screen turned off, low baterry, removed SD card, BT device available, ...
  28. 28. AndroidManifest.xml • defines what parts the app have • defines which endpoints are exposed • minimum/maximum API level • permissions • declare hardware and software features • require configuration
  29. 29. Intent • asynchronous message • binds components together (all except Content Provider) • starting activities • starting services and binding to services • sending broadcasts
  30. 30. Activity • a subclass of android.app.Activity • app usually has many activities • activities managed in activity stack • newly started activity is placed on the top of the stack
  31. 31. Activity Lifecycle • activity can be in different states during its lifecycle • foreground, visible, stopped, killed • when activity state changes a system callback is called
  32. 32. Activity callbacks • onCreate() - activity created • onStart() - activity visible for the user • onResume() - activity gains user focus • onPause() - system resuming another activity • onStop() - activity becoming invisible to the user
  33. 33. Activity callbacks • onDestroy() - before activity is destroyed • onRestart() - called if activity was previously stopped, called prior to onStart()
  34. 34. Configuration changes • when configuration changes, activities are destroyed and recreated • default behaviour, can be changed • properly handle config changes • onSaveInstanceState(Bundle)
  35. 35. Intent & Activity • starting activity explicitly • new Intent(context, MyActivity.class)! • new Intent(Intent.ACTION_VIEW, Uri.parse(“http:// developer.android.com”))! • starting activity implicitly • starting activity for result
  36. 36. User Interface • defined by a hierarchy of views • layouts = containers • LinearLayout, RelativeLayout, FrameLayout, ...
  37. 37. User Interface • widgets • UI objects • Button, TextView, EditText, RadioButton, ... • WebView
  38. 38. User Interface • list widgets • subclasses of AdapterView • display a list of items • use adapter to bind list do data • ListView, GridView, Spinner, ...
  39. 39. Resources • drawables • bitmaps • 9-patch png • state lists • layer lists • shape drawables
  40. 40. Resources • layout • strings • colors • menus • dimensions • animations
  41. 41. Resources • arrays • ids • raw • xml • ...
  42. 42. Screen sizes and densities
  43. 43. Screen sizes and densities • How to handle different screen sizes and densities?
  44. 44. Resources • resources can be created in several versions • the best version is selected according to current device configuration in runtime • resource units • dp - density-independent pixel • sp - scale-independent pixel (for fonts)
  45. 45. Resource qualifiers • suffixes for resource folders • drawables, drawable-mdpi, ... • values, values-cs • layout, layout-sw640dp • drawable-hdpi-v11
  46. 46. Resource qualifiers • screen density - ldpi, mdpi, hdpi, xhdpi, ... • screen size - small, normal, large, xlarge • screen orientation - port, land • language - en, cs, sk, ... • version - v11, v14, ...
  47. 47. Resource qualifiers • since Android 3.2 • w<N>dp - available screen width, w600dp • h<N>dp - available screen heights, h720dp • sw<N>dp - smallest width (does not change with orientation)
  48. 48. Resources • accessed from code via generated R.java file and resource ids • view.findViewById(R.id.txt_name)! • txtName.setText(R.string.txt_name_label)
  49. 49. Android version fragmentation • How to handle different API levels available on different devices?
  50. 50. Android version fragmentation • build target • API level the app is compiled against • AndroidManifest.xml • <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" />
  51. 51. Android version fragmentation • handling versions in code if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {! // code for Android < 2.3! }
  52. 52. Android version fragmentation private boolean functionalitySupported = false;! ! static {! try {! checkFunctionalitySupported();! } catch (NoClassDefFoundError e) {! ! functionalitySupported = false;! }! }! ! private static void checkFunctionalitySupported() throws ! ! ! ! ! ! ! NoClassDefFoundError {! ! functionalitySupported = android.app.Fragment.class != null;! }!
  53. 53. Fragments • a piece of application UI • introduced to support more flexible UI • phones and tablets together in one app • fragment != activity • fragments are used within activities
  54. 54. Fragments • since Android 3.0 • support library v4 backports them to Android 1.6+
  55. 55. Threads • main thread = UI thread • do not ever block the UI thread!!! • use worker threads for time consuming operations • UI toolkit not thread safe - never manipulate UI from a worker thread
  56. 56. Menu • menu resource • Android < 3.0 the whole menu hidden under menu button • ActionBar since Android 3.0 • items can be displayed in the action bar
  57. 57. Menu • behaviour for items that don’t fit in the action bar • hidden under menu button (if the device has one) • system overflow icon in the action bar
  58. 58. Dialogs • floating window screen
  59. 59. Dialogs • standard dialogs • custom dialogs • activity with dialog style • since fragments used via DialogFragment
  60. 60. Dialogs • might be tedious to create • difficult to style • StyledDialogs • https://github.com/inmite/android-styled-dialogs • the library makes styling and using dialogs a piece of cake
  61. 61. Toast • simple non-modal information • displayed for a short period of time • doesn’t have user focus
  62. 62. Notifications • a message that can be displayed to the user outside your normal UI • displayed in notification area
  63. 63. Notifications • user can open notification drawer to see the details • app can define UI and click action • NotificationCompat.Builder
  64. 64. Preferences SharedPreferences prefs = PreferenceManager! .getDefaultSharedPreferences(context);! ! SharedPreferences prefs = ! config.getSharedPreferences(PREFS_FILE_NAME,! Activity.MODE_PRIVATE);! ! int storedValue = prefs.getInt(SOME_KEY, defaultValue);! ! SharedPreferences.Editor editor = prefs.edit();! editor.putInt(SOME_KEY, storedValue);! editor.commit();
  65. 65. Sources • developer.android.com • android-developers.blogspot.com • source.android.com • stackoverflow.com • youtube.com/androiddevelopers • svetandroida.cz
  66. 66. THE END
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×