lab – android development environment...10. as the emulator starts up, you will see a progress...

34
Lab – Android Development Environment Setting up the ADT, Creating, Running and Debugging Your First Application Objectives: Familiarize yourself with the Android Development Environment Important Note: This class has many students with a wide range of previous experience. Some students are fairly new to object-oriented programming (OOP). Some have OOP experience, but are new to Android. Still others have some Android experience already, and want to just freshen up their knowledge. Because of this, I’m not expecting that everyone can finish this entire lab. I suggest that you set a time limit for yourself, say 1 hour. Work through what you can in that time and then stop and take a break. If you later feel that you have some more time for this Lab, then repeat the process. Again – don’t feel that you need to finish everything in this lab. That’s not the goal here. Specifically, if you are fairly new to programming, you should try to complete Parts 1 – 4 below. If you are familiar with programming and programming environments, you should try to complete parts 1 – 6 below. This lab contains the following Parts. 1. Set up Android Studio. 2. Create a new Android application. 3. Create an Android Virtual Device and start the Android Emulator. 4. Run the application you created in Part 2. 5. Import an application project. 6. Debug an Android application. Additional helpful information can be found on the Android Developer website: https://developer.android.com/studio/index.html https://developer.android.com/training/basics/firstapp/creating-project.html https://developer.android.com/studio/run/managing-avds.html https://developer.android.com/training/basics/firstapp/running-app.html

Upload: others

Post on 09-Sep-2021

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lab – Android Development Environment...10. As the emulator starts up, you will see a progress dialog appear in Android Studio. 11. Next, the emulator will appear and start its boot

Lab–AndroidDevelopmentEnvironmentSettinguptheADT,Creating,RunningandDebuggingYourFirstApplication

Objectives:FamiliarizeyourselfwiththeAndroidDevelopmentEnvironment

ImportantNote:Thisclasshasmanystudentswithawiderangeofpreviousexperience.Somestudentsarefairlynewtoobject-orientedprogramming(OOP).SomehaveOOPexperience,butarenewtoAndroid.StillothershavesomeAndroidexperiencealready,andwanttojustfreshenuptheirknowledge.

Becauseofthis,I’mnotexpectingthateveryonecanfinishthisentirelab.Isuggestthatyousetatimelimitforyourself,say1hour.Workthroughwhatyoucaninthattimeandthenstopandtakeabreak.IfyoulaterfeelthatyouhavesomemoretimeforthisLab,thenrepeattheprocess.Again–don’tfeelthatyouneedtofinisheverythinginthislab.That’snotthegoalhere.

Specifically,ifyouarefairlynewtoprogramming,youshouldtrytocompleteParts1–4below.Ifyouarefamiliarwithprogrammingandprogrammingenvironments,youshouldtrytocompleteparts1–6below.

ThislabcontainsthefollowingParts.

1. SetupAndroidStudio.2. CreateanewAndroidapplication.3. CreateanAndroidVirtualDeviceandstarttheAndroidEmulator.4. RuntheapplicationyoucreatedinPart2.5. Importanapplicationproject.6. DebuganAndroidapplication.

AdditionalhelpfulinformationcanbefoundontheAndroidDeveloperwebsite:

• https://developer.android.com/studio/index.html• https://developer.android.com/training/basics/firstapp/creating-project.html• https://developer.android.com/studio/run/managing-avds.html• https://developer.android.com/training/basics/firstapp/running-app.html

Page 2: Lab – Android Development Environment...10. As the emulator starts up, you will see a progress dialog appear in Android Studio. 11. Next, the emulator will appear and start its boot

Part1–SettingUpAndroidStudio.InthispartyouwilldownloadandinstallAndroidStudiowhichwillbetheIntegratedDevelopmentEnvironment(IDE)usedforthiscourse.Forthepurposesofthisdocument,weinstalledAndroidStudioversion2.3.3(thecurrentlateststablereleaseasof9/1/2017)onaMacrunningSierra.Allscreenshotscorrespondtothatenvironment.

1. DownloadAndroidStudiofromhttps://developer.android.com/studio/index.html.Clickon‘DownloadAndroidStudio’.

2. Opentheexecutablefileandroid-studio-<xxx>.3. Oncethesetuploads,youwillseetheWelcomeScreen.

4. Click‘Next>’ontheWelcomeScreen.5. Whenchoosingcomponents,ensureallofthecheckboxesarecheckedinforeachcomponentto

install.Onceyouaredone,click‘Next>’.6. AgreetotheAndroidStudioandtheIntelHAXMLicenseAgreementsafterreadingthem.7. Verifytheinstalllocationsmeettheinstallationrequirementsandclick‘Next>’.8. Youmayormaynotseetheemulatorsetupsettings,justclick‘Next>’afterselectingtheRAM

size.

Page 3: Lab – Android Development Environment...10. As the emulator starts up, you will see a progress dialog appear in Android Studio. 11. Next, the emulator will appear and start its boot

9. Finally,click‘Install’.Youwillseewhichoperationsarecurrentlyrunningintheinstallationprocessandaprogressbardisplayingtheirprogress.

10. Oncetheinstallationprocessisfinishedclick‘Next>’.

11. AndroidStudioisnowsetup.Checkon‘StartAndroidStudio’andclick‘Finish’.12. YouwillseetheCompleteInstallationscreenbelow.13. IfyouhadapreviousversionofAndroidStudioinstalledprior,thencheckeitherthefirstof

secondradiobox.Otherwise,checkthelastradioboxandhit‘OK’.

Page 4: Lab – Android Development Environment...10. As the emulator starts up, you will see a progress dialog appear in Android Studio. 11. Next, the emulator will appear and start its boot

14. AsAndroidStudiostarts,thesplashscreenwillappear.

15. Afterthesplashscreenyoumayseesomeadditionalsetupoperationsrun,suchasdownloadingcomponents.

16. Onceitisfinished,click‘Finish’.

Page 5: Lab – Android Development Environment...10. As the emulator starts up, you will see a progress dialog appear in Android Studio. 11. Next, the emulator will appear and start its boot

17. WelcometoAndroidStudio!Inthenextpartwewillstartourfirstproject.

Page 6: Lab – Android Development Environment...10. As the emulator starts up, you will see a progress dialog appear in Android Studio. 11. Next, the emulator will appear and start its boot

Part2–CreatingANewProjectInthispartyouwillcreateasimpleAndroidapplicationthatdisplaysthewords,"HelloWorld!"

1. AttheWelcomeScreen,clickon‘StartanewAndroidStudioproject’.

2. Entertheapplicationname‘MyFirstApp’andnotewheretheprojectislocated.TheAndroidStudioProjectsfolderisthedefaultlocationfornewprojects.

Page 7: Lab – Android Development Environment...10. As the emulator starts up, you will see a progress dialog appear in Android Studio. 11. Next, the emulator will appear and start its boot

3. Selectwhichdevicesyouwouldlikeyourapptorunon.Fornowwewillbeworkingwith‘PhoneandTablet’.MakesuretosettheMinimumSDKversiontoAPI21forthiscourse.

4. Select‘BlankActivity’fromthe‘CreateNewProject’dialogboxandclick‘Next’.

5. Inthenextwindow,leaveallthesettingsasdefault,andthenclickFinish.6. AndroidStudiowillnowcreatetheprojectandbuildit.

Page 8: Lab – Android Development Environment...10. As the emulator starts up, you will see a progress dialog appear in Android Studio. 11. Next, the emulator will appear and start its boot

7. YoumayseeasecurityalertifyouareonWindows,click‘Allowaccess’tocontinue.

8. OncetheAndroidStudioIDEfullyloads,youwillseethescreenbelow.Ifyouseetextratherthanthelayoutdesignermakesurethe‘Design’tabisselected.

Page 9: Lab – Android Development Environment...10. As the emulator starts up, you will see a progress dialog appear in Android Studio. 11. Next, the emulator will appear and start its boot

9. TheopeningscreenistheDesignViewoftheactivity_main.xmlfile.Youcanalreadyseethewords“HelloWorld!”ontheApp’sUserInterface.

10. IfyouclickontheTexttabyoucanseethelayoutfileunderlyingtheuserinterface.

11. Toviewthebackingcodeforthisactivity,doubleclickon‘MainActivity’insideoftheProjectdirectorytree.Thisfileislocatedin:‘java’>‘com.example.<user_name>.myfirstapp’>‘MainActivity’.

Page 10: Lab – Android Development Environment...10. As the emulator starts up, you will see a progress dialog appear in Android Studio. 11. Next, the emulator will appear and start its boot

InPart4wewillshowyouhowtorunthisappintheAndroidEmulator.

Part3–UsingtheEmulatorInthispartyouwilllearnhowtosetupandusetheAndroidEmulator.

Page 11: Lab – Android Development Environment...10. As the emulator starts up, you will see a progress dialog appear in Android Studio. 11. Next, the emulator will appear and start its boot

1. FirststartuptheAndroidVirtualDeviceManager.YoucandothatbyselectingTools>Android>AVDManagerfromtheAndroidStudiomenubar.

2. Anewdialogboxwillpopup.

3. Click"CreateVirtualDevice"tocreateanewAndroidVirtualDevice(AVD).

Page 12: Lab – Android Development Environment...10. As the emulator starts up, you will see a progress dialog appear in Android Studio. 11. Next, the emulator will appear and start its boot

4. Anotherdialogboxwillpopupdisplayingvariouspre-madeAVDtemplates.Selectwhicheverdeviceyouwouldliketoemulateandclick‘Next’.Forexample,selecttheNexus5.

5. SelecttheappropriateSystemImageforthevirtualmachine.Toallowuserswithlimitedcomputermemorytoparticipate,alloftheclassprojectswillbetestedagainstAPIlevel21.Ifyouhaven’tdownloadedthatalready,makesuretodownloaditnow,byclickingonthe“Download”link.

Page 13: Lab – Android Development Environment...10. As the emulator starts up, you will see a progress dialog appear in Android Studio. 11. Next, the emulator will appear and start its boot
Page 14: Lab – Android Development Environment...10. As the emulator starts up, you will see a progress dialog appear in Android Studio. 11. Next, the emulator will appear and start its boot

6. OncetheDownloadisfinished,click‘Finish’.

7. Click‘Next’onceyouhavereturnedtothepreviousscreen.8. Youcankeepallofthedefaultselectionsinthenextscreenandhit‘Finish’.

Page 15: Lab – Android Development Environment...10. As the emulator starts up, you will see a progress dialog appear in Android Studio. 11. Next, the emulator will appear and start its boot

9. Nowclickonthegreen‘Play’icontostarttheemulator,afterclickingonityoucanclosethe

AndroidVirtualDeviceManager.

10. Astheemulatorstartsup,youwillseeaprogressdialogappearinAndroidStudio.11. Next,theemulatorwillappearandstartitsbootsequence.

Afterthedevicehasbooted,theemulatorwillbereadyforuserinteraction.

Page 16: Lab – Android Development Environment...10. As the emulator starts up, you will see a progress dialog appear in Android Studio. 11. Next, the emulator will appear and start its boot

Part4–RunningYourFirstAppInthispartyouwilllearnhowtoruntheapplicationyoucreatedinPart2intheAndroidEmulator.

1. Therearetwowaystoruntheapp.

Method1:ReturntoAndroidStudioandsimplyclickonthe“Run‘app’”Button(Shortcut:Windows-Shift+F10,Mac-Ctrl+R)

Method2:ReturntoAndroidStudioandselectRun>Run‘app’.

Page 17: Lab – Android Development Environment...10. As the emulator starts up, you will see a progress dialog appear in Android Studio. 11. Next, the emulator will appear and start its boot

2. Nextawindowwillpop-uptoaskyoutoselectwhichofyourpre-configuredAVDdevicesyouwouldliketoruntheappon.IfyoudonothavethecorrectSDKinstalledonyourAVDforyourappyouwillbepromptedtoinstallit.

Page 18: Lab – Android Development Environment...10. As the emulator starts up, you will see a progress dialog appear in Android Studio. 11. Next, the emulator will appear and start its boot

3. IntheGradleConsolepanel,belowtheeditorwindow,youwillseeoutputindicatingthattheapplicationisbeingloadedontotheAndroidEmulator.

Page 19: Lab – Android Development Environment...10. As the emulator starts up, you will see a progress dialog appear in Android Studio. 11. Next, the emulator will appear and start its boot

4. ReturntoyourEmulatorinstance.Ifnecessary,dragthelockicontounlockyourdevice.

5. Youshouldnowseeyourapplication,runningintheAndroidEmulator.

Page 20: Lab – Android Development Environment...10. As the emulator starts up, you will see a progress dialog appear in Android Studio. 11. Next, the emulator will appear and start its boot
Page 21: Lab – Android Development Environment...10. As the emulator starts up, you will see a progress dialog appear in Android Studio. 11. Next, the emulator will appear and start its boot

Part5–ImportingandRunninganExistingApplicationInthispartyou’lllearntoimportapre-existingapplicationintoAndroidStudioandthenrunit.

1. TheAnswerapplicationexistsinthecoursesourcecoderepository.

2. ReturntoAndroidStudio.SelectOpenanExistingAndroidStudioProjectfromthemenubar.NoteallthecourseexampleapplicationshavebeenbuiltwithAndroidStudio.

3. Next,inthedialogboxthatappears,browseandselecttheProjectthatyouwantto

import.Forthisexample,select“TheAnswer”fromwhereyouclonedtheCMSC436SampleCoderepositoryinyourlocalenvironment.

ThenpressOKButton

Page 22: Lab – Android Development Environment...10. As the emulator starts up, you will see a progress dialog appear in Android Studio. 11. Next, the emulator will appear and start its boot

KeepalldefaultsettingsandpressFinishButton.

4. Atthispointtheapplicationshouldappearintheprojectwindowontheleftsideof

theIDE.

Page 23: Lab – Android Development Environment...10. As the emulator starts up, you will see a progress dialog appear in Android Studio. 11. Next, the emulator will appear and start its boot

5. SelectRun>Run‘app’fromthetoolbar.

6. TheAndroidEmulatorwillnowopenupandruntheexampleapplication.

Page 24: Lab – Android Development Environment...10. As the emulator starts up, you will see a progress dialog appear in Android Studio. 11. Next, the emulator will appear and start its boot

Part6–DebuggingInthispartofthelabyouwilllearnhowtousetheAndroidStudiodebuggertodebugtheTheAnswerapplicationyouimportedinPart5.

1. Double-clicktheTheAnswer.javafileunderapp>src>main>java>course.examples.theanswer

2. Onthisscreen,clickthehighlightedareanexttotheline:

"intval=findAnswer();"

Page 25: Lab – Android Development Environment...10. As the emulator starts up, you will see a progress dialog appear in Android Studio. 11. Next, the emulator will appear and start its boot

3. Anewbreakpointwillbeplacedatthatline,indicatedbythesmallcirclethatnowappearsinthehighlightedorangeareatotheleftofthetext.

4. Next,presstheDebugbuttonintheToolbartostartdebuggingtheapplication

(Shortcut:Windows-Shift+F9,Mac-CTRL+D).

Page 26: Lab – Android Development Environment...10. As the emulator starts up, you will see a progress dialog appear in Android Studio. 11. Next, the emulator will appear and start its boot

SimilartoStep2inPart4,afteryouseetheBUILDSUCCESSFULmessage,adialogboxwillpopupaskingyoutochooseadevice.

5. Ifyoudonothavearunningdevice,youcanchooseanemulatortolaunch.Thesystemwillstartanemulatorandruntheappinit.

Page 27: Lab – Android Development Environment...10. As the emulator starts up, you will see a progress dialog appear in Android Studio. 11. Next, the emulator will appear and start its boot

6. YourEmulatorshouldloadtheAppandstopbeforethewords,“Theanswertolife……",aredisplayedonthescreen.YoucanseetheDebugWindowappearsnexttoRunnow.ClickonittoshowDebugwindow.

7. Nowthattheappisstopped,youcanexaminetheapp’sstateandstepthroughthe

app’sexecutionusingthefollowingbuttonsappearinginthemenubar.1–ResumeProgram(F9) 7–StepOut(Shift+F8)2–PauseProgram 3–Stop(Ctrl+F2)4–StepOver(F8)5–StepInto(F7)6–ForceStepInto(Alt+Shift+F7)

1

2

3

4 5 6 7

Page 28: Lab – Android Development Environment...10. As the emulator starts up, you will see a progress dialog appear in Android Studio. 11. Next, the emulator will appear and start its boot

8. Next,presstheResumeicontocontinueexecutingtheapp.Theappwillfinishloadingandwilldisplaythetext.

9. Thenextdebuggingtaskwillhaveyoucreateanddisplayinformationalmessagesto

theLogCatpanel,tohelpyoubetterunderstandtheapplication’sruntimebehavior.Togeneratethesemessages,youwillusemethodsintheandroid.util.Logclass.Youwillalsoneedtoimportthisclassintoyourapplication.SomeLogCatfunctionsinclude:1–Log.i(…,…)–SendsanINFOLogCatmessage2–Log.d(…,…)–SendsaDEBUGLogCatmessage3–Log.e(…,…)–SendsanERRORLogCatmessage4–Log.v(…,…)–SendsaVERBOSELogCatmessageSeehttps://developer.android.com/reference/android/util/Log.htmlformoreinformation.

Page 29: Lab – Android Development Environment...10. As the emulator starts up, you will see a progress dialog appear in Android Studio. 11. Next, the emulator will appear and start its boot

10. Importtheandroid.util.Loglibrarybytyping,"importandroid.util.Log;"nearthebeginningofthecodeforTheAnswer.java.

11. TheLogclass’methodsrequireastringcalledaTag,whichidentifiesthecreatorofthemessageandcanbeusedtosortandfilterthemessageswhentheyaredisplayed.CreateaconstantcalledTAGwithintheTheAnswerclass,bytyping,forexample,"privatestaticfinalStringTAG="TheAnswer";"

Page 30: Lab – Android Development Environment...10. As the emulator starts up, you will see a progress dialog appear in Android Studio. 11. Next, the emulator will appear and start its boot

12. UsetheLog.i()functiontocreateandoutputalogmessage.Justbeforethelinethatstarts,"intval=…"typeinanewline:"Log.i(TAG,"Printingtheanswertolife");"

13. Saveyourchanges.

Page 31: Lab – Android Development Environment...10. As the emulator starts up, you will see a progress dialog appear in Android Studio. 11. Next, the emulator will appear and start its boot

14. Runtheapplication.(SeePart4formoredetailsonRunningApp).

15. Oncetheappisrunning,opentheLogCatpanelatthebottom.LookfordropdownmenuandselectEditFilterConfiguration.

16. Enter"TheAnswer"inLogTagandhitOK.

Page 32: Lab – Android Development Environment...10. As the emulator starts up, you will see a progress dialog appear in Android Studio. 11. Next, the emulator will appear and start its boot

17. YouwillnowseethelogmessagefromtheTheAnswerapplicationintheLogCatpanel.

ExtraChallenge

Ifyoufinishalltheworkaboveinclass,thendothefollowchallengeactivityaswell.

1. ModifiedHelloWorld-Rememberthefirstappyoumade?Let’sreturntothat!

2. Inthispartyou’llmodifytheoriginal"Helloworld!"messageofyourfirstapp.Todothisyouneedtomodifythestringvaluein\res\values\string.xml.

Page 33: Lab – Android Development Environment...10. As the emulator starts up, you will see a progress dialog appear in Android Studio. 11. Next, the emulator will appear and start its boot

3. Addanotherstringelementwiththetext:“Mynameis<your_name>!”.

4. Nowgototheactivity_main.xmlfileinsideofres/layout.EdittheTextViewelement

soitreferencesthehello_worldstringelementyoujustcreatedinthepreviousstep.

Page 34: Lab – Android Development Environment...10. As the emulator starts up, you will see a progress dialog appear in Android Studio. 11. Next, the emulator will appear and start its boot

5. Nowruntheappandseethechange!

Formoreinformation,takealookat:https://developer.android.com/guide/topics/resources/string-resource.html

6. NowaddsupportforanotherlanguagesuchasSpanish!Todothis,you’llneedtocreateanappropriatestringfile,runyourapp,changetheemulatorinstance’sdefaultlanguagetoSpanish,andthenreruntheapp.YourSpanishstring,couldbe:"HolaMundo!Mellamo[yourname]."Formoreinformation,takealookat:https://developer.android.com/training/basics/supporting-devices/languages.html