Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Day 3: Getting Active Through Activities


Published on

Published in: Education, Technology
  • Login to see the comments

Day 3: Getting Active Through Activities

  1. 1. Android Application Development Being Active Through Activities Ahsanul Karim [email_address] Sentinel Solutions Ltd.
  2. 2. Today We’re Covering… <ul><li>Application Structure </li></ul><ul><li>Android Application Anatomy </li></ul><ul><li>Activity </li></ul><ul><li>Layout </li></ul><ul><li>Using Layouts from Activity </li></ul><ul><li>Activity Lifecycle </li></ul><ul><li>Exercise </li></ul>
  3. 3. Project Structure… Created Project has the following structure Project Structure -Source ( src ) -Generated Class ( gen ) -Android 1.6 library -Assets ( assets ) -Resource( res ) - drawable-hdpi - drawable-ldpi - drawable-mdpi - layout - values - AndroidMenifest.xml -
  4. 4. Project Structure (Contd.) -Source ( src ) We have used only one class here which is an Activity named HalloActivity. We’ll describe about Activity in detail with lifecycle shortly. For now we can consider Activity as Android analogue for the window or dialog in a desktop application. It can load view from xml layout (here main.xml under res/layout folder) In the HelloActivity class the view of the Activity is set from main.xml given below
  5. 5. Project Structure (Contd.) -res/layout/main.xml <ul><li>UI Layout can be defined from source code using View or by layout xmls. </li></ul><ul><li>The layout xml can be generated by visual tool given by ADT </li></ul>
  6. 6. Project Structure (Contd.) -res/drawable <ul><li>From Android 1.6 to support different screen sizes and screen densities graphic files are kept in 3 different folders drawable-hdpi , drawable-ldpi and drawable-mdpi </li></ul><ul><li>In our current project, they contain only default icon file with different dimensions to support devices with different screen resolution. </li></ul>-assets Holds other static files you wish packaged with the application for deployment onto the device. In this project, we have none -gen/ -values/strings.xml
  7. 7. Project Structure (Contd.) -AndroidMenifest.xml XML file describing the application being built and what components – activities, services, etc. – are being supplied by that application
  8. 8. Let’s Build Something Useful… Objective: To get hands-on experience of building something useful more than just “Hallo World”. Plan: We’ll create a project to show how the basic building block Activity and some UI elements work. Output: User will push a button and see current time. <ul><li>We’ll learn how to: </li></ul><ul><li>Design UI from layout XML </li></ul><ul><li>Set the layout in an Activity </li></ul><ul><li>And make UI elements in action </li></ul><ul><li>Steps: </li></ul><ul><li>Creating project </li></ul><ul><li>Design UI </li></ul><ul><li>Add functionality to UI </li></ul><ul><li>Run the application </li></ul>
  9. 9. Let’s Build Something Useful(Contd.) Creating the Project: Start Eclipse and go to New > Project > Android Projec t
  10. 10. Let’s Build Something Useful(Contd.) Designing Layout (1) Edit layout/main.xml using the visual tool given by ADT by adding a TextView and a Button in a LinearLayout (more on layouts will be covered later)
  11. 11. Let’s Build Something Useful(Contd.) Designing Layout (2) The output xml is like below. We can directly edit layout xml to design the UI. Properties were set from the visual tool.
  12. 12. Let’s Build Something Useful(Contd.) Let’s infuse life to UI Activity with UI elements declared Initializing UI elements Auto-generated <ul><li>Now: </li></ul><ul><li>Adding Button action listener </li></ul><ul><li>A method for getting time from Date class </li></ul>
  13. 13. Let’s Build Something Useful(Contd.) Complete Activity
  14. 14. Let’s Build Something Useful(Contd.) Complete Activity
  15. 15. Let’s Build Something Useful(Contd.) Creating Run Configuration and Run So, you can now create your own application
  16. 16. Android Activities Activity Activity provides a user generally with an interactive screen to do something like: Dialing the phone, View a map List of something for user to select or Anything you want your user to do <ul><li>An application usually consists of multiple activities. </li></ul><ul><li>Typically, one activity in an application is specified as the &quot; main &quot; activity, which is presented to the user when launching the application for the first time. (which is specified in AndroidMenifest.xml )  </li></ul>The   <action>  element specifies that this is the &quot;main&quot; entry point to the application. The  <category>  element specifies that this activity should be listed in the system's application launcher (to allow users to launch this activity).
  17. 17. Android Activities (Contd..) Creating Activity We have already created Activities. But how did we create it? Let’s revisit… <ul><li>We created subclass of Activity base class </li></ul><ul><li>We implemented one callback method onCreate </li></ul><ul><li>What is creating subclass??? </li></ul><ul><li>What is callback methods??? </li></ul>Open Questions:
  18. 18. Android Activities (Contd..) Next Step: Implementing User Interface <ul><li>Design res/layout/yourlayout.xml </li></ul><ul><li>Use Views from Activity class </li></ul>Next Step: Implementing User Interface <manifest ... >   <application ... >       <activity android:name=&quot;.ExampleActivity&quot; />       ...   </application ... >   ... </manifest > Next Step: Starting Activity Intent intent = new Intent(this, ToActivity.class); startActivity(intent); We can start another activity by calling  startActivity() , passing it an  Intent  that describes the activity you want to start.
  19. 19. Android Application Anatomy (Contd.) Application= Set of Android Components <ul><li>Activities </li></ul><ul><li>Provides User Interface </li></ul><ul><li>Usually represents a Single Screen </li></ul><ul><li>Can contain one/more Views </li></ul><ul><li>Extends the Activity Base class </li></ul><ul><li>Services </li></ul><ul><li>No User Interface </li></ul><ul><li>Runs in Background </li></ul><ul><li>Extends the Service Base Class </li></ul><ul><li>Content Provider </li></ul><ul><li>Makes application data available to other apps </li></ul><ul><li>Data stored in SQLite database </li></ul><ul><li>Extends the ContentProvider Base class </li></ul><ul><li>Intent/Broadcast Receiver </li></ul><ul><li>Receives and Reacts to broadcast Intents </li></ul><ul><li>No UI but can start an Activity </li></ul><ul><li>Extends the BroadcastReceiver Base Class </li></ul>
  20. 20. Activity Lifecycle