Professional Documents
Culture Documents
amsterdam,november2008
diegotorresmilano
diego@codtech.com
copyright2008codtechnologiesltdwww.codtech.com
agenda
introductiontoandroid
afterthissectionyouwill...
copyright2008codtechnologiesltdwww.codtech.com
whatisandroid?
copyright2008codtechnologiesltdwww.codtech.com
isandroidlinux?
NO,androidisnotlinux!
androidisbasedonalinuxkernel butit'snotGNU/Linux
copyright2008codtechnologiesltdwww.codtech.com
soisandroidjava?
NO,androidisnotjava!
androidisnotanimplementation ofanyoftheJavavariants
androidlinuxkernel
androidisbasedonalinux2.6kernel,providing rnel,p
kernelsource:source.android.com
copyright2008codtechnologiesltdwww.codtech.com
linuxkernelenhancements
androidintroducessomelinuxkernelpatches
copyright2008codtechnologiesltdwww.codtech.com
uniqueplatformcharacteristics
androidcharacteristicsnotfoundonotherplatforms
copyright2008codtechnologiesltdwww.codtech.com
othercharacteristics
interestingfeaturesaswell,buttheyaremore commonacrossothermobileplatforms
copyright2008codtechnologiesltdwww.codtech.com
androidarchitecture
courtesyofGoogle
copyright2008codtechnologiesltdwww.codtech.com
androidbuildingblocks
afterthissectionyouwill...
copyright2008codtechnologiesltdwww.codtech.com
buildingblocks
copyright2008codtechnologiesltdwww.codtech.com
Activities
copyright2008codtechnologiesltdwww.codtech.com
Activitieslifecycle
Activitiesstates
active
paused
stopped
dropped
Views
copyright2008codtechnologiesltdwww.codtech.com
pattern:loadlayout
androidcompilestheXMLlayoutcodethatis laterloadedincodeusuallyby
public void onCreate(Bundle savedInstanceState) { ... setContentView(R.layout.filename); ... }
copyright2008codtechnologiesltdwww.codtech.com
ViewsandViewgroups
copyright2008codtechnologiesltdwww.codtech.com
pattern:ids
usingauniqueidinaXMLViewdefinition permitslocatingitlaterinJavacode
private View name; public void onCreate(Bundle savedInstanceState) { ... name = (View) findViewById(R.id.name); ... }
copyright2008codtechnologiesltdwww.codtech.com
Intents
copyright2008codtechnologiesltdwww.codtech.com
intentsplayground
http://codtech.com/android/IntentPlayground.apk
copyright2008codtechnologiesltdwww.codtech.com
Services
isstarted hasconnections
copyright2008codtechnologiesltdwww.codtech.com
Notifications
ConentProviders
ContentProvidersareobjectsthatcan
retrievedata storedata
AndroidManifest.xml
controlfilethattells thesystemwhattodo andhowthetoplevel componentsare related it'sthegluethat actuallyspecifies whichIntentsyour Activitiesreceive specifiespermissions
copyright2008codtechnologiesltdwww.codtech.com
yourfirstandroid
afterthissectionyouwill...
copyright2008codtechnologiesltdwww.codtech.com
androidproject
copyright2008codtechnologiesltdwww.codtech.com
defaultapplication
copyright2008codtechnologiesltdwww.codtech.com
designingtheUI
thissimpleUIdesigns contains
copyright2008codtechnologiesltdwww.codtech.com
createthelayout
replaceViewby
com.google.android.m apview
copyright2008codtechnologiesltdwww.codtech.com
runtheapplication
applicationnode
copyright2008codtechnologiesltdwww.codtech.com
GoogleMapsAPIkey
checkingDDMSlogcatwefind
java.lang.IllegalArgumentException: You need to specify an API Key for each MapView.
MapActivy
checkingDDMSlogcatagain
java.lang.IllegalArgumentException: MapViews can only be created inside instances of MapActivity.
copyright2008codtechnologiesltdwww.codtech.com
whereisthemap?
ACCESS_COARSE_LOCATION INTERNET
copyright2008codtechnologiesltdwww.codtech.com
finallyourmap
stillsomeproblems...
spinneriscovered
android:layout_alignPa rentTop="true"
hasnoprompt
prompt: @string/prompt
externalizeresource
copyright2008codtechnologiesltdwww.codtech.com
pattern:adapters
anAdapterobjectactsasabridgebetweenan AdapterViewandtheunderlyingdataforthatview
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( this, R.array.array, android.R.layout.layout); view.setAdapter(adapter); TheAdapterisalsoresponsibleformakingaViewforeachiteminthe dataset.
copyright2008codtechnologiesltdwww.codtech.com
pattern:resources
resourcesareexternalfiles(thatis,noncodefiles) thatareusedbyyourcodeandcompiledintoyour applicationatbuildtime.
<resources> <string-array name=array> <item>item</item> </string-array> </resources> res = getResources().getType(id);
copyright2008codtechnologiesltdwww.codtech.com
arrays.xml
<?xml version="1.0" encoding="UTF-8"?> <resources> <!-- No support for multidimensional arrays or complex objects yet (1.0r1) --> <string-array name="location_names"> <item>Mediamatic Duintjer</item> <item>NH Hotel</item> <item>Airport</item> </string-array> <string-array name="locations"> <item>52.363125,4.892070,18</item> <item>37.244832,-115.811434,9</item> <item>-34.560047,-58.44924,16</item> </string-array> </resources>
copyright2008codtechnologiesltdwww.codtech.com
completetheclass
createthelocationsarray
locations = getResources().getStringArray(R.array.locations);
gettheviews(idspattern)
spinner = (Spinner) findViewById(R.id.Spinner01); mapView = (MapView) findViewById(R.id.mapview);
createtheadapter
ArrayAdapter<CharSequence> adapter = ArrayAdapter. createFromResource(this, R.array.location_names, android.R.layout.simple_spinner_item); spinner.setAdapter(adapter)
copyright2008codtechnologiesltdwww.codtech.com
almostthere
copyright2008codtechnologiesltdwww.codtech.com
respondtoevents
invoke
goToSelectedLocation(ar g2);
copyright2008codtechnologiesltdwww.codtech.com
goToSelectedLocation
protected void goToSelectedLocation(int position) { String[] loc = locations[position].split(","); double lat = Double.parseDouble(loc[0]); double lon = Double.parseDouble(loc[1]); int zoom = Integer.parseInt(loc[2]); GeoPoint p = new GeoPoint((int)(lat * 1E6), (int)(lon * 1E6)); Log.d(TAG, "Should go to " + p); mapController.animateTo(p); mapController.setZoom(zoom); }
copyright2008codtechnologiesltdwww.codtech.com
moreevents
turnmapclickable
android:clickable="true
overrideonKeyDown
switch (keyCode) { case KeyEvent.KEYCODE_I: mapController.zoomIn(); break; case KeyEvent.KEYCODE_O: mapController.zoomOut(); break; case KeyEvent.KEYCODE_S: mapView.setSatellite( !mapView.isSatellite()); break; }
copyright2008codtechnologiesltdwww.codtech.com
wedidit!
Somethingstotry
copyright2008codtechnologiesltdwww.codtech.com
Rememberthatthereisno codefasterthannocode
Taligent'sGuidetoDesigningPrograms
copyright2008codtechnologiesltdwww.codtech.com
testingandperformance
afterthissectionyouwill...
copyright2008codtechnologiesltdwww.codtech.com
bestpractices
testing
androidsdk1.0introduces
1000000
1500000
2000000
2500000
3000000
500000
Call String.length()
performance
copyright2008codtechnologiesltdwww.codtech.com
Inflate 1 LinearLayout with 1 TextView Inflate 1 LinearLayout with 6 View Inflate 1 LinearLayout with 6 TextView Launch an empty activity
Time
summary
introductiontoandroid androidbuildingblocks
copyright2008codtechnologiesltdwww.codtech.com
Ifthingsseemundercontrol, you'renotgoingfastenough.
MarioAndretti
copyright2008codtechnologiesltdwww.codtech.com
thankyou androiddevelopmentworkshop
diegotorresmilano
diego@codtech.com
copyright2008codtechnologiesltdwww.codtech.com