MobileDevelopmentWorkshopDAY3:BASICANDROIDAPPLICATIONS
OverviewMorningsession◦ Views,layouts,andactivities◦ Binding toviews◦ Android intentsandinformation passing
ViewsThebasicbuildingblocksofaUserInterface
Responsiblefordrawing theinterfaceandresponding toevents
Somecommonviews
LayoutsSpecialtypesofviewsthatholdotherviews
Invisible
Eachlayouttypehavedifferentrulesandpropertiesforpositioningtheirchildviews
Layoutsformthevisibleaspectofactivities(moreonthatlater)
CreatingalayoutfileDescribethelayoutinXML
Describeitschildren
LoadingalayoutfileInyouractivity,setthelayoutfiletobetheview◦ setContentView(R.layout.layout_file_name)
Somecommonlayouts
Organizesitschildrenintoasinglehorizontalorverticalrow
Allowsyoutospecifythelocationofachildobjectrelativetoothersortotheparent
Displayswebpages
LinearLayout RelativeLayout WebView
ViewparametersViewsinsidealayoutwillhaveparametersthattellthemhowtheylook,behave,etc.◦ Examples◦ ID◦ Visibility◦ Size,paddingmargins◦ Position
Exercisepart1/2DesignthelayoutforanAndroidversionoftheSlotMachineJavaprogram
UseamixtureofLayouts
ActivitiesAnAndroidapplicationisacollectionofcomponents
AnActivityisoneofthemaincomponentsofAndroid◦ containsapplication logic(code)
Applicationsgenerallyconsistofoneormoreactivities◦ OneactivityisdelegatedastheMain activity◦ Themainactivityisstartedautomaticallywhentheapplicationisstarted
DeclaringactivitiesActivities,likeallothercomponents,aredeclaredintheAndroidManifest
Themanifestdescribesyourapplicationtotheoperatingsystem◦ Examples◦ Application name◦ Version◦ Permissions
Androidstudiowilldeclarecomponentsautomaticallywhentheyarecreated,butyoucanchangepropertiesmanually
ActivitylifecycleAnactivity’slifecycleistheseriesofstatesthatitpassesthroughfromcreationtodestruction
Understandingthelifecycleallowustobuildflexible,responsiveapps
Activitylifecycle
EventsEventsaretriggeredwhenauserinteractswithaview
Eventlistenersareusedtospecifywhatshouldhappenwhenaparticulareventtakesplace
Manystandardeventlistenersarefoundinside theView class
EventlistenercallbacksAndroidprovidesmanyView.On*Listenerclasseswithappropriatecallbackmethods
onClick◦ Calledwhentheusertouchestheview
onLongClick◦ Calledwhentheusertouchesandholds theview
onFocusChange◦ Calledwhenausernavigateto,orawayfromaview
Exercisepart2/2Inanactivity’sonCreate()method◦ Wireupthethelayoutfrom part1 toanActivity◦ Linkstheviewsinthelayouttoobjectvariablesinouractivity
◦ ExecutethegamelogicwhentheuserclicksaPlay button
WorkingwithmultipleactivitiesInAndroid,asingleactivityshouldbeusedforasinglejob
Assuch,anappwithmultiplejobsshouldutilizemultipleactivities
Fromoneactivity(suchasMain)youcanlaunchotheractivities
StartinganactivitystartActivity(intent);
Theintent isanobjectthatspecifiestheactivitytostart
IntotointentsAmessagingobjectusedtorequestthatactionbetaken
GenerallyusedtoStartcomponents such asactivitiesSendinformation fromonecomponent toanother
Intentsinaction
TocreateanIntentTwotypes◦ Explicit◦ Providecontext◦ Specify arecipient
◦ Implicit◦ Specify anaction
ContextContextprovidesinformationaboutacomponentaswellastheapplication’senvironment
Contextcanberetrievedfrommanyplaces,buttheinformationandcapabilitieswillvarybasedonsource
AcommonsourceofcontextisActivityName.this
ExerciseCreateatextmagnifier◦ Collecttextinformation inoneactivity◦ Passthatinformation toanotheractivitytobedisplayedinalargerfont