Android View Lifecycle

Android View Lifecycle

[android_view_hierarcandroid_view_hierarchy

In Android, View class is the base for widgets, which are used to create interactive UI components(Button, TextView, EditText,…). Android’s widgets are sufficient for the needs of most apps. However, there may be occasions on which you feel the need to implement a custom user interface for a project you are working on.

All of the view class defined in Android framework extend View. Our custom view can also extend View directly, or we can save time by extending one of the existing view subclasses, such as Button, TextView. We must understand View before we create a custom view.

What is View?

Same as above, View is the base class for widgets, which are used to create interactive UI components (buttons, text fields, etc.). The [ViewGroup](http://developer.android.com/reference/android/view/ViewGroup.html" target="_blank">) subclass is the base class for layouts, which are invisible containers that hold other Views (or other ViewGroups) and define their layout properties.

So, what about Android View lifecycle?

View Lifecycle

[android_view_lifecycandroid_view_lifecycle

To implement a custom view, my custom view class must extend Android View class. My custom view will begin by providing overrides for some of standard methods that the framework calls on all views. You do not need to override all of these methods. In fact, you can start by just overrideing onDraw(android.graphics.Canvas)

CategoryMethodsDescription
CreationConstructorsThere 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.
Layout`onMeasure(int, int)`Called to determine the size requirements for this view and all of its children.
`onLayout(boolean, int, int, int, int)`Called when this view should assign a size and position to all of its children.
`onSizeChanged(int, int, int, int)`Called when the size of this view has changed.
Drawing`onDraw(android.graphics.Canvas)`Called when the view should render its content.
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.
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.
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.