Upload
trong-dinh
View
1.883
Download
0
Embed Size (px)
DESCRIPTION
Nothing to say :s
Citation preview
All Rights reserved - © IMT Solutions 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
All Rights reserved - © IMT Solutions 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
All Rights reserved - © IMT Solutions 4
Android Life Cycle
All Rights reserved - © IMT Solutions 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 "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)
All Rights reserved - © IMT Solutions 6
Layout
** ViewGroup
All Rights reserved - © IMT Solutions 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:
All Rights reserved - © IMT Solutions 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
All Rights reserved - © IMT Solutions 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!!!!
All Rights reserved - © IMT Solutions 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.
All Rights reserved - © IMT Solutions 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.
All Rights reserved - © IMT Solutions 12
Layout (cont)
The GridView ViewGroup
All Rights reserved - © IMT Solutions 13
Layout (cont)
ListActivity
All Rights reserved - © IMT Solutions 14
Layout (cont)
ScrollView<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="none">
<LinearLayout
android:id="@+id/layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content”>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/scroll_view_2_text_1"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/scroll_view_2_button_1"/>
</LinearLayout>
</ScrollView>
All Rights reserved - © IMT Solutions 15
Layout (cont)
TabHost ViewGroup
All Rights reserved - © IMT Solutions 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
All Rights reserved - © IMT Solutions 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.
All Rights reserved - © IMT Solutions 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.
All Rights reserved - © IMT Solutions 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
All Rights reserved - © IMT Solutions 20
Troubleshooting & Tips
Troubleshooting:
-See log cat when a crash issue happens.
-Increase memory of emulator if binary file exceeds 30 MB (-partition-size 1024)
-Change extension of file (or split file if it is a database file) to avoid UNCOMPRESS_DATA_MAX.
Tips:
-Use layout opt after designing GUI.
-Use <merge> to optimize layout.
All Rights reserved - © IMT Solutions 21
Reference
http://developer.android.com/
All Rights reserved - © IMT Solutions 22
Question & Answer