Synapseindia Android Tutorial

Embed Size (px)

Citation preview

  • 8/10/2019 Synapseindia Android Tutorial

    1/46

    Android Tutorial

  • 8/10/2019 Synapseindia Android Tutorial

    2/46

    Tools

    PhoneEclipse ( http://www.eclipse.org/downloads/ )

    Android Plugin (ADT)

    Android SDK ( http://developer.android.com/sdk/index.html )Install everything except Additional SDKPlatforms, unless you want to

    Windows Users: may need to install MotorolaDriver directly ( http://www.motorola.com/Support/US-EN/Support-Homepage/Software_and_Drivers/USB-and-PC-

    Charging-Drivers )

    http://www.eclipse.org/downloads/http://developer.android.com/sdk/index.htmlhttp://www.motorola.com/Support/US-EN/Support-Homepage/Software_and_Drivers/USB-and-PC-Charging-Drivershttp://www.motorola.com/Support/US-EN/Support-Homepage/Software_and_Drivers/USB-and-PC-Charging-Drivershttp://www.motorola.com/Support/US-EN/Support-Homepage/Software_and_Drivers/USB-and-PC-Charging-Drivershttp://www.motorola.com/Support/US-EN/Support-Homepage/Software_and_Drivers/USB-and-PC-Charging-Drivershttp://www.motorola.com/Support/US-EN/Support-Homepage/Software_and_Drivers/USB-and-PC-Charging-Drivershttp://www.motorola.com/Support/US-EN/Support-Homepage/Software_and_Drivers/USB-and-PC-Charging-Drivershttp://www.motorola.com/Support/US-EN/Support-Homepage/Software_and_Drivers/USB-and-PC-Charging-Drivershttp://www.motorola.com/Support/US-EN/Support-Homepage/Software_and_Drivers/USB-and-PC-Charging-Drivershttp://www.motorola.com/Support/US-EN/Support-Homepage/Software_and_Drivers/USB-and-PC-Charging-Drivershttp://www.motorola.com/Support/US-EN/Support-Homepage/Software_and_Drivers/USB-and-PC-Charging-Drivershttp://www.motorola.com/Support/US-EN/Support-Homepage/Software_and_Drivers/USB-and-PC-Charging-Drivershttp://www.motorola.com/Support/US-EN/Support-Homepage/Software_and_Drivers/USB-and-PC-Charging-Drivershttp://www.motorola.com/Support/US-EN/Support-Homepage/Software_and_Drivers/USB-and-PC-Charging-Drivershttp://www.motorola.com/Support/US-EN/Support-Homepage/Software_and_Drivers/USB-and-PC-Charging-Drivershttp://www.motorola.com/Support/US-EN/Support-Homepage/Software_and_Drivers/USB-and-PC-Charging-Drivershttp://www.motorola.com/Support/US-EN/Support-Homepage/Software_and_Drivers/USB-and-PC-Charging-Drivershttp://developer.android.com/sdk/index.htmlhttp://www.eclipse.org/downloads/
  • 8/10/2019 Synapseindia Android Tutorial

    3/46

    Android SDK

    Once installed open the SDK ManagerInstall the desired packages

    Create an Android Virtual Device (AVD)

  • 8/10/2019 Synapseindia Android Tutorial

    4/46

    SDK Manager

  • 8/10/2019 Synapseindia Android Tutorial

    5/46

    AVD

  • 8/10/2019 Synapseindia Android Tutorial

    6/46

    ADT Plugin (1)

    In Eclipse, go to Help -> Install New SoftwareClick Add in top right Enter:

    Name: ADT PluginLocation: https://dl-ssl.google.com/android/eclipse/

    Click OK, then select Developer Tools, click Next Click Next and then Finish

    Afterwards, restart EclipseSpecify SDK location (next 3 slides)

    Must do this every time start a new project in a newlocation (at least in Windows)

  • 8/10/2019 Synapseindia Android Tutorial

    7/46

    ADT Plugin (2)

  • 8/10/2019 Synapseindia Android Tutorial

    8/46

    ADT Plugin (3)

  • 8/10/2019 Synapseindia Android Tutorial

    9/46

    ADT Plugin (4)

  • 8/10/2019 Synapseindia Android Tutorial

    10/46

    Creating a Project (1)

  • 8/10/2019 Synapseindia Android Tutorial

    11/46

    Creating a Project (2)

    Needtheitemscircled

    ThenclickFinish

  • 8/10/2019 Synapseindia Android Tutorial

    12/46

    Project Components

    src your source codegen auto-generated code (usually just R.java)Included librariesResources

    Drawables (like .png images)LayoutsValues (like strings)

    Manifest file

  • 8/10/2019 Synapseindia Android Tutorial

    13/46

    XML

    Used to define some of the resourcesLayouts (UI)Strings

    Manifest fileShouldnt usually have to edit it directly,Eclipse can do that for you

    Preferred way of creating UIsSeparates the description of the layout from anyactual code that controls itCan easily take a UI from one platform to another

  • 8/10/2019 Synapseindia Android Tutorial

    14/46

    R Class

    Auto- generated: you shouldnt edit it Contains IDs of the project resourcesEnforces good software engineeringUse findViewById and Resources object toget access to the resources

    Ex. Button b = (Button)findViewById(R.id.button1)Ex. getResources().getString(R.string. hello ));

  • 8/10/2019 Synapseindia Android Tutorial

    15/46

    Layouts (1)

    Eclipse has a great UI creatorGenerates the XML for you

    Composed of View objectsCan be specified for portrait and landscapemode

    Use same file name, so can make completely

    different UIs for the orientations without modifyingany code

  • 8/10/2019 Synapseindia Android Tutorial

    16/46

    Layouts (2)

  • 8/10/2019 Synapseindia Android Tutorial

    17/46

    Layouts (3)

    Click Create to make layout modifications When in portrait mode can select Portrait to make ares sub folder for portrait layouts

    Likewise for Landscape layouts while in landscape modeWill create folders titled layout -port and layout -land

    Note: these port and land folders are examples ofalternate layouts, see here for more info

    http://developer.android.com/guide/topics/resources/providing-resources.html

    Avoid errors by making sure components have thesame id in both orientations, and that youve testedeach orientation thoroughly

    http://developer.android.com/guide/topics/resources/providing-resources.htmlhttp://developer.android.com/guide/topics/resources/providing-resources.htmlhttp://developer.android.com/guide/topics/resources/providing-resources.htmlhttp://developer.android.com/guide/topics/resources/providing-resources.html
  • 8/10/2019 Synapseindia Android Tutorial

    18/46

    Layouts (4)

  • 8/10/2019 Synapseindia Android Tutorial

    19/46

    Strings

    In res/valuesstrings.xml

    Application wide available strings

    Promotes good software engineeringUI components made in the UI editor shouldhave text defined in strings.xml

    Strings are just one kind of Value there aremany others

  • 8/10/2019 Synapseindia Android Tutorial

    20/46

    Manifest File (1)

    Contains characteristics about your applicationWhen have more than one Activity in app, NEED tospecify it in manifest file

    Go to graphical view of the manifest file Add an Activity in the bottom rightBrowse for the name of the activity

    Need to specify Services and other components too

    Also important to define permissions and externallibraries, like Google Maps API

  • 8/10/2019 Synapseindia Android Tutorial

    21/46

    Manifest File (2) Adding an Activity

  • 8/10/2019 Synapseindia Android Tutorial

    22/46

    Android Programming Components

    Activityhttp://developer.android.com/guide/topics/fundamentals/activities.html

    Servicehttp://developer.android.com/guide/topics/fundamentals/services.html

    Content ProvidersBroadcast Receivers

    Android in a nutshell:http://developer.android.com/guide/topics/fundamentals.html

    http://developer.android.com/guide/topics/fundamentals/activities.htmlhttp://developer.android.com/guide/topics/fundamentals/services.htmlhttp://developer.android.com/guide/topics/fundamentals.htmlhttp://developer.android.com/guide/topics/fundamentals.htmlhttp://developer.android.com/guide/topics/fundamentals.htmlhttp://developer.android.com/guide/topics/fundamentals/services.htmlhttp://developer.android.com/guide/topics/fundamentals/services.htmlhttp://developer.android.com/guide/topics/fundamentals/activities.htmlhttp://developer.android.com/guide/topics/fundamentals/activities.html
  • 8/10/2019 Synapseindia Android Tutorial

    23/46

    Activities (1)

    The basis of android applications A single Activity defines a single viewablescreen

    the actions, not the layoutCan have multiple per applicationEach is a separate entity

    They have a structured life cycleDifferent events in their life happen either via theuser touching buttons or programmatically

  • 8/10/2019 Synapseindia Android Tutorial

    24/46

    Activities (2)

  • 8/10/2019 Synapseindia Android Tutorial

    25/46

    Services (1)

    Run in the backgroundCan continue even if Activity that started it diesShould be used if something needs to be done while the user is notinteracting with application

    Otherwise, a thread is probably more applicableShould create a new thread in the service to do work in, since theservice runs in the main thread

    Can be bound to an applicationIn which case will terminate when all applications bound to it unbind

    Allows multiple applications to communicate with it via a commoninterface

    Needs to be declared in manifest fileLike Activities, has a structured life cycle

  • 8/10/2019 Synapseindia Android Tutorial

    26/46

    Services (2)

  • 8/10/2019 Synapseindia Android Tutorial

    27/46

    Running in Eclipse (1)

    Similar to launching a regular Java app, usethe launch configurationsSpecify an Android Application and create a

    new oneSpecify activity to be runCan select a manual option, so each timeprogram is run, you are asked whether youwant to use the actual phone or the emulator

    Otherwise, it should be smart and use whicheverone is available

  • 8/10/2019 Synapseindia Android Tutorial

    28/46

    Running in Eclipse (2)

  • 8/10/2019 Synapseindia Android Tutorial

    29/46

    Running in Eclipse (3)

  • 8/10/2019 Synapseindia Android Tutorial

    30/46

    Running in Eclipse (4)

  • 8/10/2019 Synapseindia Android Tutorial

    31/46

    USB Debugging

    Should be enabled on phone to usedeveloper featuresIn the main apps screen select Settings ->

    Applications -> Development -> USBdebugging (it needs to be checked)

  • 8/10/2019 Synapseindia Android Tutorial

    32/46

    Android Debug Bridge

    Used for a wide variety of developer tasksRead from the log fileShow what android devices are available

    Install android applications (.apk files)

    In the platform -tools directory of the mainandroid sdk directory

    Recommend putting this directory and the toolsdirectory on the system path

    adb.exe

  • 8/10/2019 Synapseindia Android Tutorial

    33/46

    Debugging

    Instead of using traditional System.out.println, use the Log classImported with android.util.LogMultiple types of output (debug, warning, error, ) Log.d(,)

    Can be read using logcat.

    Print out the whole log, which auto-updatesadb logcatErase log

    adb logcat cFilter output via tags

    adb logcat : *:Scan have multiple : filters corresponds to debug, warning, error, etc.If use Log.d(), then = D

    Reference

  • 8/10/2019 Synapseindia Android Tutorial

    34/46

    Screen Shots

    Some say you need to root the phone thatis not trueOne option: Android Screen Capture

    http://www.mightypocket.com/2010/08/android-screenshots-screen-capture-screen-cast/Its slow, but fine for screenshots of applicationswhose screens arent changing fast

    Read their installation help, following the extrasteps if need be (I had to copy adb.exe and somedll files, as they explain)

    http://www.mightypocket.com/2010/08/android-screenshots-screen-capture-screen-cast/http://www.mightypocket.com/2010/08/android-screenshots-screen-capture-screen-cast/http://www.mightypocket.com/2010/08/android-screenshots-screen-capture-screen-cast/http://www.mightypocket.com/2010/08/android-screenshots-screen-capture-screen-cast/http://www.mightypocket.com/2010/08/android-screenshots-screen-capture-screen-cast/http://www.mightypocket.com/2010/08/android-screenshots-screen-capture-screen-cast/http://www.mightypocket.com/2010/08/android-screenshots-screen-capture-screen-cast/http://www.mightypocket.com/2010/08/android-screenshots-screen-capture-screen-cast/http://www.mightypocket.com/2010/08/android-screenshots-screen-capture-screen-cast/http://www.mightypocket.com/2010/08/android-screenshots-screen-capture-screen-cast/http://www.mightypocket.com/2010/08/android-screenshots-screen-capture-screen-cast/http://www.mightypocket.com/2010/08/android-screenshots-screen-capture-screen-cast/http://www.mightypocket.com/2010/08/android-screenshots-screen-capture-screen-cast/http://www.mightypocket.com/2010/08/android-screenshots-screen-capture-screen-cast/
  • 8/10/2019 Synapseindia Android Tutorial

    35/46

    Maps Example (1)

    Using Google Maps in your appSetup project to use Google API version Edit Manifest file

    To indicate the app will use maps and the internetGet a maps API keyNote: Google Maps API can display a map and draw overlays,but is not the full Google Maps experience you enjoy on the web

    For example, there does not seem to be inherent support fordrawing routes between points (if you find it let meknow)however, you can draw lines between points and almostany type of overlay, but thats different than street routes The directions API is a web service, which is different, amongseveral other Google web services

    Read the Google API terms of use

  • 8/10/2019 Synapseindia Android Tutorial

    36/46

    Maps Example (2)

  • 8/10/2019 Synapseindia Android Tutorial

    37/46

    Maps Example (3) Manifest (1)

    Open Manifest file Add map library tag

    Add the Uses Library com.google.android.maps Indicate the app will access the internet

    Add the Permission android.permission.lNTERNET End goal is to add the following two lines to XML file,under the and ,respectively

    Under the tag

    Under the tag

    Following is GUI way to add them

  • 8/10/2019 Synapseindia Android Tutorial

    38/46

    Maps Example (4) Manifest (2)

    1

    2

  • 8/10/2019 Synapseindia Android Tutorial

    39/46

    Maps Example (5) Manifest (3)

    Select Add under Uses Library (last slide) Then select Uses Library at this prompt Set name as: com.google.android.maps (nextslide) and save

  • 8/10/2019 Synapseindia Android Tutorial

    40/46

    Maps Example (6) Manifest (4)

  • 8/10/2019 Synapseindia Android Tutorial

    41/46

    Maps Example (7) Manifest (5)

    2

    1

  • 8/10/2019 Synapseindia Android Tutorial

    42/46

    Maps Example (8) Manifest (6)

    Select Permissions and then Add (last slide) Select Uses Permissions at this prompt Set name to: android.permission.INTERNETand save (next slide)

  • 8/10/2019 Synapseindia Android Tutorial

    43/46

    Maps Example (9) Manifest (7)

  • 8/10/2019 Synapseindia Android Tutorial

    44/46

    Maps Example (10) Maps API Key (1)

    All Android applications need to be signedThe debug mode signs for you with special debugcertificate

    All MapView elements in map applicationsneed to have an API key associated withthem

    That key must be registered with the certificate

    used to sign the appWhen releasing app, need to sign with arelease certificate and get a new API Key

  • 8/10/2019 Synapseindia Android Tutorial

    45/46

    Maps Example (11) Maps API Key (2)

    For debug mode, get the MD5 fingerprint of the debug certificateLocate the keystore

    Windows Vista: C:\Users\\.android\debug.keystoreWindows XP: C:\Documents and Settings\\.android\debug.keystore OS X and Linux: ~/.android/debug.keystore

    Use Keytool (comes with Java, in the bin directory with the otherJava tools, should put that dir on system PATH) to get fingerprint

    keytool -list v -alias androiddebugkey -keystore -storepass android -keypass android

    If dont include v option, then will probably get only 1 fingerprint, and if itsnot MD5, then need v (Java 7 needs v)

    Extract the MD5 fingerprint, SHA will not work unfortunatelyGo to https://code.google. com/android/maps-api-signup.html ,agree to terms and paste MD5 fingerprint, you will then be givenan API Key

    http://code.google.com/android/maps-api-signup.htmlhttp://code.google.com/android/maps-api-signup.htmlhttp://code.google.com/android/maps-api-signup.htmlhttp://code.google.com/android/maps-api-signup.htmlhttp://code.google.com/android/maps-api-signup.htmlhttp://code.google.com/android/maps-api-signup.html
  • 8/10/2019 Synapseindia Android Tutorial

    46/46

    Maps Example (12)

    Need to put MapView tag in XMLcom.google.android.maps.MapViewMapView is the basic view that represents a Google MapdisplayMust include API Key in XML, inside a layout

    Maps API Referencehttp://code.google.com/android/add-ons/google-apis/reference/index.html

    http://code.google.com/android/add-ons/google-apis/reference/index.htmlhttp://code.google.com/android/add-ons/google-apis/reference/index.htmlhttp://code.google.com/android/add-ons/google-apis/reference/index.htmlhttp://code.google.com/android/add-ons/google-apis/reference/index.htmlhttp://code.google.com/android/add-ons/google-apis/reference/index.htmlhttp://code.google.com/android/add-ons/google-apis/reference/index.html