SlideShare una empresa de Scribd logo
1 de 22
http://www.imt.com.vn   20 Feb, 2011 [email_address] Hello Android
Key Achievements 1. Android Life Cycle 2. Layout. 3. Layout optimize. 4. Custom component 5. SQLite 6. Memory Management. 7. Troubleshooting & Tips 5. Reference 6. Question & Answer
Requirement 1.  Create an simple Application using a XML Layout 2.  Do the same with a Java-based Layout 3.  Run Android Applications. 4. Using the LogCat for debugging purpose 5. Use Intents to start (Sub)Activities 6. Find Views defined in XML to use them in Java Code 7. Handle Clicks to Views 8. Return values from SubActivities 9. Pass data to SubActivities using Bundles Read andbook.pdf file
Android Life Cycle
onSaveInstanceState(Bundle): This is where an application gets a chance to save instance state. Instance state should be state that is not persisted with an application's data model, such as the state of an indicator or other state that is only part of the Activity object. This method has an implementation in the parent class: It calls the onSaveInstanceState method of each View object in this instance of Activity, which has the result of saving the state of these View objects, and this is, often, the only state you need to store this way. Data that your subclass needs to store is saved using the "put" methods of the Bundle class. onRestoreInstanceState(Bundle): The onRestoreInstanceState method is called when there is instance state to be restored. If this method is called, it is called after onStart and before onPostCreate, which is a minor lifecycle method Android Life Cycle (cont)
Layout ** ViewGroup
Layout (cont) LinearLayout  aligns all children in a single direction — vertically or horizontally, depending on how you define the orientation attribute. All children are stacked one after the other, so a vertical list will only have one child per row, no matter how wide they are, and a horizontal list will only be one row high (the height of the tallest child, plus padding). A  LinearLayout  respects margins between children and the gravity (right, center, or left alignment) of each child.  1. LinearLayout:
Layout (cont) 2. RelativeLayout: RelativeLayout lets child views specify their position relative to the parent view or to each other (specified by ID). So you can align two elements by right border, or make one below another, centered in the screen, centered left, and so on. Elements are rendered in the order given, so if the first element is centered in the screen, other elements aligning themselves to that element will be aligned relative to screen center. Also, because of this ordering, if using XML to specify this layout, the element that you will reference (in order to position other view objects) must be listed in the XML file before you refer to it from the other views via its reference ID
Layout (cont) 3. FrameLayout: It's basically a blank space on your screen that you can later fill with a single object — for example, a picture that you'll swap in and out. All child elements of the FrameLayout are pinned to the top left corner of the screen; you cannot specify a different location for a child view. Absolute layout is deprecated!!!!
Layout (cont) ViewGroups are Views that contain child Views. Each ViewGroup class embodies a different set of assumptions about how to display its child Views. All ViewGroups descend from the android.view.ViewGroup class.
Layout (cont) The Gallery ViewGroup int getCount: Returns the number of items in the data set represented by the Adapter. Object getItem(int position): Returns the object in the Adapter function (Adapter class) at the given position. long getItem(int position): Returns the row ID within the Adapter of the object at the given position. View getView(int position, View convertView, ViewGroup parent): Returns a View object that will display the data in the given position in the data set.
Layout (cont) The GridView ViewGroup
Layout (cont) ListActivity
Layout (cont) ScrollView <ScrollView xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;wrap_content&quot; android:scrollbars=&quot;none&quot;> <LinearLayout android:id=&quot;@+id/layout&quot; android:orientation=&quot;vertical&quot; android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;wrap_content”> <TextView android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;wrap_content&quot; android:text=&quot;@string/scroll_view_2_text_1&quot;/> <Button android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;wrap_content&quot; android:text=&quot;@string/scroll_view_2_button_1&quot;/> </LinearLayout> </ScrollView>
Layout (cont) TabHost ViewGroup
Layout optimize layoutopt is a command-line tool that helps you optimize the layouts and layout hierarchies of your applications. Usage To run layoutopt against a given list of layout resources: layoutopt <file_or_directory> ... For example: $ layoutopt res/layout-land $ layoutopt res/layout/main.xml res/layout-land/main.xml
Custom component The Basic Approach Here is a high level overview of what you need to know to get started in creating your own View components: Extend an existing View class or subclass with your own class. Override some of the methods from the superclass. The superclass methods to override start with 'on', for example, onDraw(), onMeasure(), and onKeyDown(). This is similar to the on... events in Activity or ListActivity that you override for lifecycle and other functionality hooks. Use your new extension class. Once completed, your new extension class can be used in place of the view upon which it was based. Tip: Extension classes can be defined as inner classes inside the activities that use them. This is useful because it controls access to them but isn't necessary (perhaps you want to create a new public View for wider use in your application). Android offers a sophisticated and powerful componentized model for building your UI, based on the fundamental layout classes: View and ViewGroup. To start with, the platform includes a variety of prebuilt View and ViewGroup subclasses — called widgets and layouts, respectively — that you can use to construct your UI.
SQLite SQLiteDatabase Android's Java interface to its relational database, SQLite. It supports an SQL implementation rich enough for anything you're likely to need in a mobile application, including a cursor facility. Cursor A container for the results of a database query that supports an MVC style observation system. Cursors are similar to JDBC result sets and are the return value of a database query in Android. A cursor can represent many objects without requiring an instance for each one. With a cursor, you can move to the start of query results and access each row one at a time as needed. To access cursor data, you call methods named as Cursor.getAs*(int columnNumber) (e.g. getAsString). The values the cursor will return depend on the current cursor index, which you can increment by calling, Cursor.moveToNext or decrement with, Cursor.moveToPrevious as needed. You can think of the current index of the cursor as a pointer to a result object. SQLiteOpenHelper Provides a life cycle framework for creating and upgrading your application database. Its quite helpful to use this class to assist with the critical task of transitioning the data from one version of an application to a possible new set of database tables in a new version of an application. SQLiteQueryBuiler Provides a high level abstraction for creating SQLite queries for use in Android applications. Using this class can simplify the task of writing a query since it saves your from having to fiddle with SQL syntax yourself.
Memory Management In summary, to avoid context-related memory leaks, remember the following: - Do not keep long-lived references to a context-activity (a reference to an activity should have the same life cycle as the activity itself) - Try using the context-application instead of a context-activity - Avoid non-static inner classes in an activity if you don't control their life cycle, use a static inner class and make a weak reference to the activity inside. The solution to this issue is to use a static inner class with a WeakReference to the outer class, as done in ViewRoot and its W inner class for instance - A garbage collector is not an insurance against memory leaks
Troubleshooting & Tips ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Reference http://developer.android.com/
Question & Answer

Más contenido relacionado

La actualidad más candente

07.3. Android Alert message, List, Dropdown, and Auto Complete
07.3. Android Alert message, List, Dropdown, and Auto Complete07.3. Android Alert message, List, Dropdown, and Auto Complete
07.3. Android Alert message, List, Dropdown, and Auto CompleteOum Saokosal
 
Android Screen Containers & Layouts
Android Screen Containers & LayoutsAndroid Screen Containers & Layouts
Android Screen Containers & LayoutsVijay Rastogi
 
Session 5 Tp5
Session 5 Tp5Session 5 Tp5
Session 5 Tp5phanleson
 
21 ijaprr vol1-3-12-17juni
21 ijaprr vol1-3-12-17juni21 ijaprr vol1-3-12-17juni
21 ijaprr vol1-3-12-17juniijaprr_editor
 
20. Magento Austria meetup - EAV Principles
20. Magento Austria meetup - EAV Principles20. Magento Austria meetup - EAV Principles
20. Magento Austria meetup - EAV Principlesmohammedhsalem
 
OER-Unit 1 authentication -Lecturer Notes
OER-Unit 1 authentication -Lecturer NotesOER-Unit 1 authentication -Lecturer Notes
OER-Unit 1 authentication -Lecturer NotesGirija Muscut
 
Android Layout
Android LayoutAndroid Layout
Android Layoutmcanotes
 
Introduction to JPA (JPA version 2.0)
Introduction to JPA (JPA version 2.0)Introduction to JPA (JPA version 2.0)
Introduction to JPA (JPA version 2.0)ejlp12
 
JPA 2.1 performance tuning tips
JPA 2.1 performance tuning tipsJPA 2.1 performance tuning tips
JPA 2.1 performance tuning tipsosa_ora
 
jpa-hibernate-presentation
jpa-hibernate-presentationjpa-hibernate-presentation
jpa-hibernate-presentationJohn Slick
 
20131191 msbuild properties
20131191 msbuild properties20131191 msbuild properties
20131191 msbuild propertiesLearningTech
 

La actualidad más candente (14)

07.3. Android Alert message, List, Dropdown, and Auto Complete
07.3. Android Alert message, List, Dropdown, and Auto Complete07.3. Android Alert message, List, Dropdown, and Auto Complete
07.3. Android Alert message, List, Dropdown, and Auto Complete
 
Android Screen Containers & Layouts
Android Screen Containers & LayoutsAndroid Screen Containers & Layouts
Android Screen Containers & Layouts
 
Session 5 Tp5
Session 5 Tp5Session 5 Tp5
Session 5 Tp5
 
JPA For Beginner's
JPA For Beginner'sJPA For Beginner's
JPA For Beginner's
 
21 ijaprr vol1-3-12-17juni
21 ijaprr vol1-3-12-17juni21 ijaprr vol1-3-12-17juni
21 ijaprr vol1-3-12-17juni
 
20. Magento Austria meetup - EAV Principles
20. Magento Austria meetup - EAV Principles20. Magento Austria meetup - EAV Principles
20. Magento Austria meetup - EAV Principles
 
OER-Unit 1 authentication -Lecturer Notes
OER-Unit 1 authentication -Lecturer NotesOER-Unit 1 authentication -Lecturer Notes
OER-Unit 1 authentication -Lecturer Notes
 
Android Layout
Android LayoutAndroid Layout
Android Layout
 
Introduction to JPA (JPA version 2.0)
Introduction to JPA (JPA version 2.0)Introduction to JPA (JPA version 2.0)
Introduction to JPA (JPA version 2.0)
 
JPA 2.1 performance tuning tips
JPA 2.1 performance tuning tipsJPA 2.1 performance tuning tips
JPA 2.1 performance tuning tips
 
JPA Best Practices
JPA Best PracticesJPA Best Practices
JPA Best Practices
 
jpa-hibernate-presentation
jpa-hibernate-presentationjpa-hibernate-presentation
jpa-hibernate-presentation
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuery
 
20131191 msbuild properties
20131191 msbuild properties20131191 msbuild properties
20131191 msbuild properties
 

Destacado

Presentatie social media en ondernemen
Presentatie social media en ondernemenPresentatie social media en ondernemen
Presentatie social media en ondernemenJeroen van der Sluis
 
Subversion on .Unix
Subversion on .UnixSubversion on .Unix
Subversion on .UnixTrong Dinh
 
Brianna's connectionreport
Brianna's connectionreportBrianna's connectionreport
Brianna's connectionreportcdnisg6
 
Subversion on .Unix
Subversion on .UnixSubversion on .Unix
Subversion on .UnixTrong Dinh
 
Questioning
QuestioningQuestioning
Questioningcdnisg6
 

Destacado (8)

Presentatie social media en ondernemen
Presentatie social media en ondernemenPresentatie social media en ondernemen
Presentatie social media en ondernemen
 
Subversion on .Unix
Subversion on .UnixSubversion on .Unix
Subversion on .Unix
 
Speed Seminar Social Media
Speed Seminar Social MediaSpeed Seminar Social Media
Speed Seminar Social Media
 
Brianna's connectionreport
Brianna's connectionreportBrianna's connectionreport
Brianna's connectionreport
 
Subversion on .Unix
Subversion on .UnixSubversion on .Unix
Subversion on .Unix
 
Questioning
QuestioningQuestioning
Questioning
 
Housekeeper
HousekeeperHousekeeper
Housekeeper
 
Audiosfera audiobooks
Audiosfera audiobooksAudiosfera audiobooks
Audiosfera audiobooks
 

Similar a Hello Android

View groups containers
View groups containersView groups containers
View groups containersMani Selvaraj
 
Android apps development
Android apps developmentAndroid apps development
Android apps developmentMonir Zzaman
 
Beginning Native Android Apps
Beginning Native Android AppsBeginning Native Android Apps
Beginning Native Android AppsGil Irizarry
 
04 user interfaces
04 user interfaces04 user interfaces
04 user interfacesC.o. Nieto
 
Android Tutorial
Android TutorialAndroid Tutorial
Android TutorialFun2Do Labs
 
mobile application development -unit-3-
mobile application development  -unit-3-mobile application development  -unit-3-
mobile application development -unit-3-TejamFandat
 
07_UIAndroid.pdf
07_UIAndroid.pdf07_UIAndroid.pdf
07_UIAndroid.pdfImranS18
 
Metamorphosis from Forms to Java: A technical lead's perspective, part II
Metamorphosis from Forms to Java:  A technical lead's perspective, part IIMetamorphosis from Forms to Java:  A technical lead's perspective, part II
Metamorphosis from Forms to Java: A technical lead's perspective, part IIMichael Fons
 
"Android" mobilių programėlių kūrimo įvadas #2
"Android" mobilių programėlių kūrimo įvadas #2"Android" mobilių programėlių kūrimo įvadas #2
"Android" mobilių programėlių kūrimo įvadas #2Tadas Jurelevičius
 
Tuning and optimizing webcenter spaces application white paper
Tuning and optimizing webcenter spaces application white paperTuning and optimizing webcenter spaces application white paper
Tuning and optimizing webcenter spaces application white paperVinay Kumar
 
Android Training Ahmedabad , Android Course Ahmedabad, Android architecture
Android Training Ahmedabad , Android Course Ahmedabad, Android architectureAndroid Training Ahmedabad , Android Course Ahmedabad, Android architecture
Android Training Ahmedabad , Android Course Ahmedabad, Android architectureNicheTech Com. Solutions Pvt. Ltd.
 

Similar a Hello Android (20)

View groups containers
View groups containersView groups containers
View groups containers
 
Android apps development
Android apps developmentAndroid apps development
Android apps development
 
Beginning Native Android Apps
Beginning Native Android AppsBeginning Native Android Apps
Beginning Native Android Apps
 
Lesson 3
Lesson 3Lesson 3
Lesson 3
 
Android Ui
Android UiAndroid Ui
Android Ui
 
Android UI
Android UI Android UI
Android UI
 
04 user interfaces
04 user interfaces04 user interfaces
04 user interfaces
 
Android Tutorial
Android TutorialAndroid Tutorial
Android Tutorial
 
Model viewviewmodel2
Model viewviewmodel2Model viewviewmodel2
Model viewviewmodel2
 
Lesson 4
Lesson 4Lesson 4
Lesson 4
 
Struts
StrutsStruts
Struts
 
mobile application development -unit-3-
mobile application development  -unit-3-mobile application development  -unit-3-
mobile application development -unit-3-
 
07_UIAndroid.pdf
07_UIAndroid.pdf07_UIAndroid.pdf
07_UIAndroid.pdf
 
Struts 1
Struts 1Struts 1
Struts 1
 
Metamorphosis from Forms to Java: A technical lead's perspective, part II
Metamorphosis from Forms to Java:  A technical lead's perspective, part IIMetamorphosis from Forms to Java:  A technical lead's perspective, part II
Metamorphosis from Forms to Java: A technical lead's perspective, part II
 
Android views and layouts-chapter4
Android views and layouts-chapter4Android views and layouts-chapter4
Android views and layouts-chapter4
 
"Android" mobilių programėlių kūrimo įvadas #2
"Android" mobilių programėlių kūrimo įvadas #2"Android" mobilių programėlių kūrimo įvadas #2
"Android" mobilių programėlių kūrimo įvadas #2
 
Tuning and optimizing webcenter spaces application white paper
Tuning and optimizing webcenter spaces application white paperTuning and optimizing webcenter spaces application white paper
Tuning and optimizing webcenter spaces application white paper
 
Android Training Ahmedabad , Android Course Ahmedabad, Android architecture
Android Training Ahmedabad , Android Course Ahmedabad, Android architectureAndroid Training Ahmedabad , Android Course Ahmedabad, Android architecture
Android Training Ahmedabad , Android Course Ahmedabad, Android architecture
 
MVC
MVCMVC
MVC
 

Hello Android

  • 1. http://www.imt.com.vn 20 Feb, 2011 [email_address] Hello Android
  • 2. Key Achievements 1. Android Life Cycle 2. Layout. 3. Layout optimize. 4. Custom component 5. SQLite 6. Memory Management. 7. Troubleshooting & Tips 5. Reference 6. Question & Answer
  • 3. Requirement 1. Create an simple Application using a XML Layout 2. Do the same with a Java-based Layout 3. Run Android Applications. 4. Using the LogCat for debugging purpose 5. Use Intents to start (Sub)Activities 6. Find Views defined in XML to use them in Java Code 7. Handle Clicks to Views 8. Return values from SubActivities 9. Pass data to SubActivities using Bundles Read andbook.pdf file
  • 5. onSaveInstanceState(Bundle): This is where an application gets a chance to save instance state. Instance state should be state that is not persisted with an application's data model, such as the state of an indicator or other state that is only part of the Activity object. This method has an implementation in the parent class: It calls the onSaveInstanceState method of each View object in this instance of Activity, which has the result of saving the state of these View objects, and this is, often, the only state you need to store this way. Data that your subclass needs to store is saved using the &quot;put&quot; methods of the Bundle class. onRestoreInstanceState(Bundle): The onRestoreInstanceState method is called when there is instance state to be restored. If this method is called, it is called after onStart and before onPostCreate, which is a minor lifecycle method Android Life Cycle (cont)
  • 7. Layout (cont) LinearLayout aligns all children in a single direction — vertically or horizontally, depending on how you define the orientation attribute. All children are stacked one after the other, so a vertical list will only have one child per row, no matter how wide they are, and a horizontal list will only be one row high (the height of the tallest child, plus padding). A LinearLayout respects margins between children and the gravity (right, center, or left alignment) of each child. 1. LinearLayout:
  • 8. Layout (cont) 2. RelativeLayout: RelativeLayout lets child views specify their position relative to the parent view or to each other (specified by ID). So you can align two elements by right border, or make one below another, centered in the screen, centered left, and so on. Elements are rendered in the order given, so if the first element is centered in the screen, other elements aligning themselves to that element will be aligned relative to screen center. Also, because of this ordering, if using XML to specify this layout, the element that you will reference (in order to position other view objects) must be listed in the XML file before you refer to it from the other views via its reference ID
  • 9. Layout (cont) 3. FrameLayout: It's basically a blank space on your screen that you can later fill with a single object — for example, a picture that you'll swap in and out. All child elements of the FrameLayout are pinned to the top left corner of the screen; you cannot specify a different location for a child view. Absolute layout is deprecated!!!!
  • 10. Layout (cont) ViewGroups are Views that contain child Views. Each ViewGroup class embodies a different set of assumptions about how to display its child Views. All ViewGroups descend from the android.view.ViewGroup class.
  • 11. Layout (cont) The Gallery ViewGroup int getCount: Returns the number of items in the data set represented by the Adapter. Object getItem(int position): Returns the object in the Adapter function (Adapter class) at the given position. long getItem(int position): Returns the row ID within the Adapter of the object at the given position. View getView(int position, View convertView, ViewGroup parent): Returns a View object that will display the data in the given position in the data set.
  • 12. Layout (cont) The GridView ViewGroup
  • 14. Layout (cont) ScrollView <ScrollView xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;wrap_content&quot; android:scrollbars=&quot;none&quot;> <LinearLayout android:id=&quot;@+id/layout&quot; android:orientation=&quot;vertical&quot; android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;wrap_content”> <TextView android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;wrap_content&quot; android:text=&quot;@string/scroll_view_2_text_1&quot;/> <Button android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;wrap_content&quot; android:text=&quot;@string/scroll_view_2_button_1&quot;/> </LinearLayout> </ScrollView>
  • 16. Layout optimize layoutopt is a command-line tool that helps you optimize the layouts and layout hierarchies of your applications. Usage To run layoutopt against a given list of layout resources: layoutopt <file_or_directory> ... For example: $ layoutopt res/layout-land $ layoutopt res/layout/main.xml res/layout-land/main.xml
  • 17. Custom component The Basic Approach Here is a high level overview of what you need to know to get started in creating your own View components: Extend an existing View class or subclass with your own class. Override some of the methods from the superclass. The superclass methods to override start with 'on', for example, onDraw(), onMeasure(), and onKeyDown(). This is similar to the on... events in Activity or ListActivity that you override for lifecycle and other functionality hooks. Use your new extension class. Once completed, your new extension class can be used in place of the view upon which it was based. Tip: Extension classes can be defined as inner classes inside the activities that use them. This is useful because it controls access to them but isn't necessary (perhaps you want to create a new public View for wider use in your application). Android offers a sophisticated and powerful componentized model for building your UI, based on the fundamental layout classes: View and ViewGroup. To start with, the platform includes a variety of prebuilt View and ViewGroup subclasses — called widgets and layouts, respectively — that you can use to construct your UI.
  • 18. SQLite SQLiteDatabase Android's Java interface to its relational database, SQLite. It supports an SQL implementation rich enough for anything you're likely to need in a mobile application, including a cursor facility. Cursor A container for the results of a database query that supports an MVC style observation system. Cursors are similar to JDBC result sets and are the return value of a database query in Android. A cursor can represent many objects without requiring an instance for each one. With a cursor, you can move to the start of query results and access each row one at a time as needed. To access cursor data, you call methods named as Cursor.getAs*(int columnNumber) (e.g. getAsString). The values the cursor will return depend on the current cursor index, which you can increment by calling, Cursor.moveToNext or decrement with, Cursor.moveToPrevious as needed. You can think of the current index of the cursor as a pointer to a result object. SQLiteOpenHelper Provides a life cycle framework for creating and upgrading your application database. Its quite helpful to use this class to assist with the critical task of transitioning the data from one version of an application to a possible new set of database tables in a new version of an application. SQLiteQueryBuiler Provides a high level abstraction for creating SQLite queries for use in Android applications. Using this class can simplify the task of writing a query since it saves your from having to fiddle with SQL syntax yourself.
  • 19. Memory Management In summary, to avoid context-related memory leaks, remember the following: - Do not keep long-lived references to a context-activity (a reference to an activity should have the same life cycle as the activity itself) - Try using the context-application instead of a context-activity - Avoid non-static inner classes in an activity if you don't control their life cycle, use a static inner class and make a weak reference to the activity inside. The solution to this issue is to use a static inner class with a WeakReference to the outer class, as done in ViewRoot and its W inner class for instance - A garbage collector is not an insurance against memory leaks
  • 20.