An introduction to the Android platform and how to develop for it with Java and the provided tools. Also covers best practices and some resources to aid in the development process.
3. ABOUT ME
• Live here in Boise - jim@mckeeth.org
• Lead Developer Evangelist for EmbarcaderoTechnologies
• www.embarcadero.com - Delphi for Android coming soon
• Google Developer Group - www.gdgb.org
• Boise Software Developers Group - www.bsdg.org
• Boise Code Camp - www.BoiseCodeCamp.com
Tuesday, August 20, 13
5. GOOD PLACESTO KNOW
• developer.android.com - Official developer site
• developers.google.com/android/ - Google on Android
• developers.google.com/events/io/ - Google I/O Replays
• stackoverflow.com - Great Q&A community
• android.stackexchange.com - End user Q&A
• vogella.com/android.html - Detailed tutorials
5
Tuesday, August 20, 13
7. ANDROID STACK
• Tweaked Linux 2.6
• Tweaked Java (Dalvik)
• Android OS
• Android Apps
• (Mostly open source)
7
Tuesday, August 20, 13
8. VERSION HISTORY
• November 5th, 2007 is Android’s Birthday
• Each version has a version number, name and API level
• Version number is most specific
• Name is group of versions and API levels
• Latest:Android 4.3 Jelly Bean (API 18)
http://en.wikipedia.org/wiki/Android_version_history#Version_history_by_API_level
Tuesday, August 20, 13
9. VERSION POPULARITY
As of August 2013
http://sn.im/platform-versions9
40% = Jelly Bean
63% >= ICS
96% >= Gingerbread
40%
23%
33%
Tuesday, August 20, 13
13. CORE APPLICATIONS
• Phone
• eMail and SMS clients
• Calendar
• Contacts
• Web Browser
• Geographic Support via Google Maps
13
Tuesday, August 20, 13
24. MULTIPLE ACTIVITIES
• Each Activity must be defined in manifest by class name and
“action”
• Activities reside on a “task stack” - visible activity is “top of
stack”
• Transition to an Activity using Intent
• Return from Activity using Activity.finish() which pops off task
stack
Tuesday, August 20, 13
25. “RES” - RESOURCES
• XML Files
• drawable
• layout
• values
• etc.
• Graphics
Tuesday, August 20, 13
26. RES/LAYOUT
• Each Activity should have dedicated layout XML file
• Layout file specifies Activity appearance
• Several layout types
• Default LinearLayout provides sequential placement of widgets
Tuesday, August 20, 13
28. • Application/Task is a stack of Activities
• Visible activity = top of stack
• Activities maintain state, even when notTOS
• “Back” button pops stack.
• “Home” key would move stack to background
• Selecting application brings to foreground or launches
new instance
APPLICATION AKATASK
Tuesday, August 20, 13
29. ANDROID.APP.ACTIVITY
• Activity extends Context
• Context contains application environment
• “a single focused thing a user can do”
• An application consists of 1 or more Activities
• Provides a default window to draw in, might be smaller than
screen or float on top of others
• UI elements are “View(s)” or widgets
Tuesday, August 20, 13
30. ANDROID.VIEW.VIEW
• Extends java.lang.Object
• Basic building block for UI components
• Rectangular area on display
• Responsible for drawing and event handling
• View is the base class for widgets
Tuesday, August 20, 13
34. ANDROID.CONTENT.INTENT
• Interprocess Communication messaging
• Can launch an Activity or start a Service
• new Intent(String action, Uri uri);
• new Intent(Context packageCtx, Class class);
• Intent.putExtra()
• Intent.setAction()
http://sn.im/android-intent
Tuesday, August 20, 13
35. INTENT (CONTINUED)
• android.intent.action.MAIN (receiver on main activity)
• many options:ACTION_VIEW, etc
• category = extra information
• MIME type
• extras (i.e. getAction(), getExtra(), etc)
Tuesday, August 20, 13
36. INTENT USAGE
• Activity Switch:
• new Intent(this, NextActivity.class);
• Web Browser:
• Uri uri = Uri.parse(“http://foobar.com”);
• new Intent(Intent.ACTION_VIEW, uri);
• Phone Dialer:
• Uri uri = Uri.parse(“tel:4085551212”);
• new Intent(Intent.ACTION_VIEW, uri);
http://sn.im/android-intent
Tuesday, August 20, 13
40. VIEW SIZE
• FILL_PARENT = parent sized (minus pad)
• Now “MATCH_PARENT” w/API Level 8
• WRAP_CONTENT = just big enough (plus pad)
Tuesday, August 20, 13
41. LAYING OUTVIEWS
• Gravity = position (i.e. left, right, center)
• Weight = extra/empty space allocation
• priority for expansion
• default weight is zero
Tuesday, August 20, 13
43. DENSITY INDEPENDENT
PIXELS
• An abstract unit that is based on the physical density of the screen.These units
are relative to a 160 dpi (dots per inch) screen, so 160dp is always one inch
regardless of the screen density.
• The ratio of dp-to-pixel will change with the screen density, but not necessarily
in direct proportion.
• You should use these units when specifying view dimensions in your layout, so
the UI properly scales to render at the same actual size on different screens.
• The compiler accepts both "dip" and "dp", though "dp" is more consistent with
"sp".
Tuesday, August 20, 13
44. SCALE-INDEPENDENT PIXELS
• This is like the dp unit, but it is also scaled by the user's font
size preference.
• Recommend you use this unit when specifying font sizes, so
they will be adjusted for both the screen density and the
user's preference.
Recom
m
ended
for
Fonts
Tuesday, August 20, 13
45. COLORS
• #RGB
• #ARGB ( Alpha / Red / Green / Blue )
• #RRGGBB (i.e. #3d1ec4)
• #AARRGGBB
Tuesday, August 20, 13
54. BEST PRACTICES
• Support multiple screen sizes and densities
• Use the Action Bar pattern - Good-bye menu button
• Build Dynamic UI with Fragments
• Know when to persist and restore state
• Don’t block main thread - use AsyncTask -Test with
StrictMode
http://sn.im/android-practices
http://sn.im/android-performance
Tuesday, August 20, 13
55. SUPPORT MULTIPLE SCREENS
SIZES & DENSITIES
• Use wrap_content, fill_parent, or dp units when
specifying dimensions in an XML layout file
• Do not use hard coded pixel values in your
application code
• Do not use AbsoluteLayout (it's deprecated)
• Supply alternative bitmap drawables for
different screen densities
• ldpi, mdpi, hdpi, xhdpi
http://sn.im/android-screens
Tuesday, August 20, 13
57. ACTION BAR
1. App Icon - optional “up” affordance
2. View control - Navigation
3. Dynamic number of “important” action buttons
4. Action overflow
http://sn.im/android-actionbar
http://actionbarsherlock.com/
Tuesday, August 20, 13
58. ASYNCTASK
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));
// Escape early if cancel() is called
if (isCancelled()) break;
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
}
http://sn.im/android-asynctask
Tuesday, August 20, 13
59. STRICT MODE
• Place a policy on a thread - provides feedback when blocked
public void onCreate() {
if (DEVELOPER_MODE) {
StrictMode.enableDefaults(); // Or configure
}
super.onCreate();
}
• http://sn.im/android-strictmode
• http://sn.im/android-strictmode-blog
Tuesday, August 20, 13
61. GOOD PLACESTO KNOW
• developer.android.com - Official developer site
• developers.google.com/android/ - Google on Android
• developers.google.com/events/io/ - Google I/O Replays
• stackoverflow.com - Great Q&A community
• android.stackexchange.com - End user Q&A
• vogella.com/android.html - Detailed tutorials
61
Tuesday, August 20, 13
62. ABOUT ME
• Live here in Boise - jim@mckeeth.org
• Lead Developer Evangelist for EmbarcaderoTechnologies
• www.embarcadero.com - Delphi for Android coming soon
• Google Developer Group - www.gdgb.org
• Boise Software Developers Group - www.bsdg.org
• Boise Code Camp - www.BoiseCodeCamp.com
Tuesday, August 20, 13