Android App Development 02 : Activity & Layout

  • View
    499

  • Download
    3

Embed Size (px)

DESCRIPTION

 

Text of Android App Development 02 : Activity & Layout

  • 2 Activity & Layout Anuchit Chalothorn anoochit@gmail.comLicensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  • Quote --
  • ActivityAn activity represents the visual representationof an Android application. activities use views, i.e. user interface widgets as for examplebuttons to create the user interface and tointeract with the user. An Android applicationcan have several activities.
  • Activity Life Cycle
  • Created
  • Paused and Resumed
  • Stopped
  • Save and Restore State
  • Workshop: Trace Activity Life CycleYou can use Log or Toast to check life cycle ofactivity on each methods; onCreate onStart onResume onPause onStop onDestroy
  • Activity LayoutThe user interface for Activities is typicallydefined via XML files (layout files).
  • Trick: Single TaskPrevent create instance of Activity, you canidentify launchMode in AndroidManifest.xml force for single instance android:launchMode="singleInstance" force for single task android:launchMode="singleTask" force app to recognize latest state android:alwaysRetainTaskState="true"
  • Trick: Screen OrientationYou can identify the screen orientation byconfig in AndroidManifest.xml Portrait android:screenOrientation="portrait" Landscape android:screenOrientation="landscape" Landscape with No Keyboard android:configChange="orientation|kyboardHidden"
  • Multiple ActivitiesSingle App has at least 1 Activity, complex apphas multiple activity. You can call to anotheractivity using Intent.
  • Workshop: Two ActivitiesCreate App with 2 Activities each activity has abutton for navigate to other activity. UsingIntent to call another activity.Intent i = new Intent(MainActivity.this,SecondActivity);startactivity(i);
  • Workshop: Sent data between activityCreate App with 2 Activities, first activity hastext field and button, after push button itll sentdata in text field to the second activity. UsingputExtra method to create a variable andidentify value;Intent i = new Intent(getApplicationContext(),NewActivity.class);i.putExtra("new_variable_name","value");startActivity(i);
  • Sent a valueUsing putExtra method to create a variable andidentify value;Intent i = new Intent(getApplicationContext(),NewActivity.class);i.putExtra("new_variable_name","value");startActivity(i);
  • Receive a valueUsing getExtras methods to receive value fromvariable;Bundle extras = getIntent().getExtras();if (extras != null) { String value = extras.getString("new_variable_name");}
  • Workshop: Temperature ConverterCreate multiple Activity App, temperatureconverter between Celsius and Fahrenheitusing the following formula C x 9/5 + 32 = F (F - 32) x 5/9 = C
  • LayoutsAndroid has different layouts for place anwidgets Linear Layout Relative Layout Frame Layout
  • Workshop: App with Relative LayoutCreate single Activity App with Label andButton using Relative Layout and see howrelative layout work.
  • Workshop: App with Linear LayoutCreate single Activity App with Label andButton using Linear Layout and see how thelinear layout work.
  • Workshop: App with Frame LayoutCreate single Activity App with Label andButton using Frame Layout and see how theframe layout work.
  • Trick: Dump LayoutYou can use DDMS tool to dump screen to seethe layout design and properties
  • End