24
Mobile Development Workshop DAY 3: BASIC ANDROID APPLICATIONS

Mobile Development Workshop · 2016-11-12 · Declaring activities Activities, like all other components, are declared in the Android Manifest The manifest describes your application

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Mobile Development Workshop · 2016-11-12 · Declaring activities Activities, like all other components, are declared in the Android Manifest The manifest describes your application

MobileDevelopmentWorkshopDAY3:BASICANDROIDAPPLICATIONS

Page 2: Mobile Development Workshop · 2016-11-12 · Declaring activities Activities, like all other components, are declared in the Android Manifest The manifest describes your application

OverviewMorningsession◦ Views,layouts,andactivities◦ Binding toviews◦ Android intentsandinformation passing

Page 3: Mobile Development Workshop · 2016-11-12 · Declaring activities Activities, like all other components, are declared in the Android Manifest The manifest describes your application

ViewsThebasicbuildingblocksofaUserInterface

Responsiblefordrawing theinterfaceandresponding toevents

Page 4: Mobile Development Workshop · 2016-11-12 · Declaring activities Activities, like all other components, are declared in the Android Manifest The manifest describes your application

Somecommonviews

Page 5: Mobile Development Workshop · 2016-11-12 · Declaring activities Activities, like all other components, are declared in the Android Manifest The manifest describes your application

LayoutsSpecialtypesofviewsthatholdotherviews

Invisible

Eachlayouttypehavedifferentrulesandpropertiesforpositioningtheirchildviews

Layoutsformthevisibleaspectofactivities(moreonthatlater)

Page 6: Mobile Development Workshop · 2016-11-12 · Declaring activities Activities, like all other components, are declared in the Android Manifest The manifest describes your application

CreatingalayoutfileDescribethelayoutinXML

Describeitschildren

Page 7: Mobile Development Workshop · 2016-11-12 · Declaring activities Activities, like all other components, are declared in the Android Manifest The manifest describes your application

LoadingalayoutfileInyouractivity,setthelayoutfiletobetheview◦ setContentView(R.layout.layout_file_name)

Page 8: Mobile Development Workshop · 2016-11-12 · Declaring activities Activities, like all other components, are declared in the Android Manifest The manifest describes your application

Somecommonlayouts

Organizesitschildrenintoasinglehorizontalorverticalrow

Allowsyoutospecifythelocationofachildobjectrelativetoothersortotheparent

Displayswebpages

LinearLayout RelativeLayout WebView

Page 9: Mobile Development Workshop · 2016-11-12 · Declaring activities Activities, like all other components, are declared in the Android Manifest The manifest describes your application

ViewparametersViewsinsidealayoutwillhaveparametersthattellthemhowtheylook,behave,etc.◦ Examples◦ ID◦ Visibility◦ Size,paddingmargins◦ Position

Page 10: Mobile Development Workshop · 2016-11-12 · Declaring activities Activities, like all other components, are declared in the Android Manifest The manifest describes your application

Exercisepart1/2DesignthelayoutforanAndroidversionoftheSlotMachineJavaprogram

UseamixtureofLayouts

Page 11: Mobile Development Workshop · 2016-11-12 · Declaring activities Activities, like all other components, are declared in the Android Manifest The manifest describes your application

ActivitiesAnAndroidapplicationisacollectionofcomponents

AnActivityisoneofthemaincomponentsofAndroid◦ containsapplication logic(code)

Applicationsgenerallyconsistofoneormoreactivities◦ OneactivityisdelegatedastheMain activity◦ Themainactivityisstartedautomaticallywhentheapplicationisstarted

Page 12: Mobile Development Workshop · 2016-11-12 · Declaring activities Activities, like all other components, are declared in the Android Manifest The manifest describes your application

DeclaringactivitiesActivities,likeallothercomponents,aredeclaredintheAndroidManifest

Themanifestdescribesyourapplicationtotheoperatingsystem◦ Examples◦ Application name◦ Version◦ Permissions

Androidstudiowilldeclarecomponentsautomaticallywhentheyarecreated,butyoucanchangepropertiesmanually

Page 13: Mobile Development Workshop · 2016-11-12 · Declaring activities Activities, like all other components, are declared in the Android Manifest The manifest describes your application

ActivitylifecycleAnactivity’slifecycleistheseriesofstatesthatitpassesthroughfromcreationtodestruction

Understandingthelifecycleallowustobuildflexible,responsiveapps

Page 14: Mobile Development Workshop · 2016-11-12 · Declaring activities Activities, like all other components, are declared in the Android Manifest The manifest describes your application

Activitylifecycle

Page 15: Mobile Development Workshop · 2016-11-12 · Declaring activities Activities, like all other components, are declared in the Android Manifest The manifest describes your application

EventsEventsaretriggeredwhenauserinteractswithaview

Eventlistenersareusedtospecifywhatshouldhappenwhenaparticulareventtakesplace

Manystandardeventlistenersarefoundinside theView class

Page 16: Mobile Development Workshop · 2016-11-12 · Declaring activities Activities, like all other components, are declared in the Android Manifest The manifest describes your application

EventlistenercallbacksAndroidprovidesmanyView.On*Listenerclasseswithappropriatecallbackmethods

onClick◦ Calledwhentheusertouchestheview

onLongClick◦ Calledwhentheusertouchesandholds theview

onFocusChange◦ Calledwhenausernavigateto,orawayfromaview

Page 17: Mobile Development Workshop · 2016-11-12 · Declaring activities Activities, like all other components, are declared in the Android Manifest The manifest describes your application

Exercisepart2/2Inanactivity’sonCreate()method◦ Wireupthethelayoutfrom part1 toanActivity◦ Linkstheviewsinthelayouttoobjectvariablesinouractivity

◦ ExecutethegamelogicwhentheuserclicksaPlay button

Page 18: Mobile Development Workshop · 2016-11-12 · Declaring activities Activities, like all other components, are declared in the Android Manifest The manifest describes your application

WorkingwithmultipleactivitiesInAndroid,asingleactivityshouldbeusedforasinglejob

Assuch,anappwithmultiplejobsshouldutilizemultipleactivities

Fromoneactivity(suchasMain)youcanlaunchotheractivities

Page 19: Mobile Development Workshop · 2016-11-12 · Declaring activities Activities, like all other components, are declared in the Android Manifest The manifest describes your application

StartinganactivitystartActivity(intent);

Theintent isanobjectthatspecifiestheactivitytostart

Page 20: Mobile Development Workshop · 2016-11-12 · Declaring activities Activities, like all other components, are declared in the Android Manifest The manifest describes your application

IntotointentsAmessagingobjectusedtorequestthatactionbetaken

GenerallyusedtoStartcomponents such asactivitiesSendinformation fromonecomponent toanother

Page 21: Mobile Development Workshop · 2016-11-12 · Declaring activities Activities, like all other components, are declared in the Android Manifest The manifest describes your application

Intentsinaction

Page 22: Mobile Development Workshop · 2016-11-12 · Declaring activities Activities, like all other components, are declared in the Android Manifest The manifest describes your application

TocreateanIntentTwotypes◦ Explicit◦ Providecontext◦ Specify arecipient

◦ Implicit◦ Specify anaction

Page 23: Mobile Development Workshop · 2016-11-12 · Declaring activities Activities, like all other components, are declared in the Android Manifest The manifest describes your application

ContextContextprovidesinformationaboutacomponentaswellastheapplication’senvironment

Contextcanberetrievedfrommanyplaces,buttheinformationandcapabilitieswillvarybasedonsource

AcommonsourceofcontextisActivityName.this

Page 24: Mobile Development Workshop · 2016-11-12 · Declaring activities Activities, like all other components, are declared in the Android Manifest The manifest describes your application

ExerciseCreateatextmagnifier◦ Collecttextinformation inoneactivity◦ Passthatinformation toanotheractivitytobedisplayedinalargerfont