3. Table of Contents
1.Custom Views
• Default views
• How Android Draws views
• Reason for creating views
• Responsibility of views
• Using new views in your layout files
• Create Screenshot of Views
4. Table of Contents
2.Compound Views
3.Creating Custom Views
• Creating Custom Views
• Measurements
• Defining Custom layout manager
4.Life Cycle
•Life Cycle Events related to Windows
•Traversal life cycle Events
•Activity Life Cycle
6. 1.Custom Views
The Android framework provides several default views but developer can also create
their custom views and use them in their application. The base class a view is the View.
7. How Android Draws Views
• Once an Activity receives the focus, it must provide the root node of its layout
hierarchy to the Android system. Afterwards the Android system starts the drawing
procedure.
• Drawing begins with the root node of the layout. The layout hierarchy is traversed
in the order of declaration, i.e., parents are drawn before their children and children
are drawn in the order of declaration.
• Drawing the layout is a two pass process:
• measuring pass - implemented in the measure(int, int) method and is a top-down traversal
of the view hierarchy. Every view stores its measurements.
• layout pass - implemented in the layout(int, int, int, int) method is also a top-down traversal
of the view hierarchy. During this phase each layout manager is responsible for positioning all
of its children. It uses the the sizes computed in the measure pass.
8. How Android Draws Views
• A view or activity can retrigger the measure and layout pass with a call to the
requestLayout() method.
• After the measure and layout calculation, the views draw themselves. This
operation can be triggered with the invalidate() method from the View class.
9. Reasons for Creating Views
• View are typically created to provide a user interface experience with is not
possible with the default views.
• Using custom view allows the developer allow to do certain performance
optimization
10. Responsibility of Views
• Views are responsible for measuring, layouting and drawing themselves and their
child elements (in case of a ViewGroup)
• Views are also responsible for saving their UI state and handling touch events
11. Ways of creating custom views
• Customs views are typically classified as compound views or custom
views. It is possible to create custom views by:
• Compound views
• Custom Views
– By extending an existing view
• By extending the View class
12. Using new views in layout files
• Custom and compound views can be used in layout files. For this you need
to use the full qualified name in the layout file, e.g. using the package and
class name.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<de.android.ownview.MyDrawView
android:id="@+id/myDrawView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
13. Create screenshots (Images) of views
• Every View class support the creating of an image of its current display.
The following coding shows an example for that.
# Build the Drawing Cache
view.buildDrawingCache();
# Create Bitmap
Bitmap cache = view.getDrawingCache();
# Save Bitmap
saveBitmap(cache);
view.destroyDrawingCache();
14. Compound Views
• Compound views are a very powerful way of reusing code and UI when
programming for Android. A Compound View is essentially a collection of
two or more other UI elements placed together, which you can use as if it
was a single View. For instance, you could use Compound Views to create
a ListView item which contains a small picture and some text. You can also
add custom logic to this Compound View.
15. Creating custom views
• By extending the View class or one of its subclasses you can create your custom
view.
• For drawing view use the onDraw() method. In this method you receive a Canvas
object which allows you to perform drawing operations on it, e.g. draw lines, circle,
text or bitmaps. If the view should be re-drawn you call the invalidate() method
which triggers a call to the onDraw() method of this view.
Defining custom layout managers
• You can implement your custom layout manager by extending the ViewGroup
class. This allows you to implement more efficient layout managers or to
implement effects which are currently missing in the Android platform.
• A custom layout manager can override the onMeasure() and onLayout() method
and specialize the calculation of its children. For example it can leave out the time
consuming support of layout_weight of the LinearLayout class.
16. Life Cycle
Life cycle events related to the window
• A view is displayed if it is attached to a layout hierarchy which is attached to a
window. A view has several life cycle hooks.
• The onAttachedToWindow() is called once the window is available.
• The onDetachedFromWindow() is used when the view is removed from its parent
(and if the parent is attached to a window). This happens for example if the activity
is recycled (e.g. via the finished() method call) or if the view is recycled in a
ListView. The onDetachedFromWindow() method can be used to stop animations
and to clean up resources used by the view.
17. Traversal life cycle events
• Traversals life cycle events consists of Animate, Measure, Layout and Draw.
• All views must know how to measure and layout themselves. The
requestLayout() method call tells the view to measure and layout itself. As
this operation may influence the layout of other views it calls also
requestLayout() of its parent.
• The onMeasure() method determines the size for the view and its children.
It must set the dimension via the setMeasuredDimension() method is this
method call before returning.
• The onLayout() positions the views based on the result of the onMeasure()
method call. This call happens typically once, whichonMeasure() can
happens once.
18. Creating custom views
Creation
Constructor:
There is a form of the constructor that are called when the view is created from code
and a form that is called when the view is inflated from a layout file. The second form
should parse and apply any attributes defined in the layout file.
OnFinishInflate()
Called after a view and all of its children has been inflated from XML.
This is called as the last phase of inflation, after all child views have been added
19. Creating custom views
Layout
onMeasure(int ,int)
Called to determine the size requirements for this view and all of its children.
When overriding this method, you must call setMeasuredDimension(int, int) measured
width and height of this view. Failure to do so will trigger an IllegalStateException
onLayout (boolean changed, int left, int top, int right, int bottom)
Called from layout when this view should assign a size and position to each of its
children. Derived classes with children should override this method and call layout on
each of their children.
protected void onSizeChanged (int w, int h, int oldw, int oldh)
This is called during layout when the size of this view has changed. If you were just
added to the view hierarchy, you're called with the old values of 0.
21. Creating custom views
Event Processing:
onKeyDown(int, KeyEvent)
Called when a new hardware key event occurs.
onKeyUp(int, KeyEvent)
Called when a hardware key up event occurs.
onTrackballEvent(MotionEvent)
Called when a trackball motion event occurs.
onTouchEvent(MotionEvent)
Called when a touch screen motion event occurs.
22. Creating custom views
Focus:
onFocusChanged(boolean, int, android.graphics.Rect)
Called when the view gains or loses focus.
onWindowFocusChanged(boolean)
Called when the window containing the view gains or loses focus.
23. Creating custom views
Attaching:
onAttachedToWindow()
Called when the view is attached to a window.
onDetachedFromWindow()
Called when the view is detached from its window.
onWindowVisibilityChanged(int)
Called when the visibility of the window containing the view has changed.