Understanding The Lifecycle of Custom View In Android

Posted By : Bipin Tiwari | 31-Aug-2020

In Android all the view widgets are based on View. Let's delve into understanding the lifecycle of custom view in Android.


To start implementing our own custom view, we will need to create a subclass of View and override some lifecycle callbacks methods of view. So first you need to understand the view lifecycle first. The lifecycle are as follows : 

1. Constructor : When creating custom view you have to first override the constructor which initializes the view. There are 4 constructor available :

  1. Create new view with code View(Context context)

  2. Using the XML layout View(Context context, AttributeSet attrs)

  3. Using XML *with a style from theme attribute View(Context context, AttributeSet attrs, int defStyleAttr)

  4. Using XML *with a style from theme attribute or style resource View(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)


2. OnAttachedToWindow() : The method is called when the view is attached to the window. It let us know that the view can be active and now has surface on which it can draw and now we can also allocate any resource or create listeners.


3. onMeasure() : The method is called to determine the dimensions of view that should be drawn. But in case its a viewgroup, it will call measure on each of its child views and then result can help determine its own size.

onMeasure(int widthMeasureSpec, int heightMeasureSpec)
@param widthMeasureSpec Horizontal space requirements as imposed by the parent
@param heightMeasureSpec Vertical space requirements as imposed by the parent


4. onLayout() : This is called after the measuring is done and now to position views on the screen. In case of viewgroup its called on each of its children.


5. onDraw() : So after the sizes and positions are calucated in previous methods, now the view can be drawn based on them. It has Canvas object generated, on which the background will be drawn.


Note : Try not to create objects in this method as it gets called frequently.


6. invalidate() : The call to this method invalidate the whole view. If the view is visible the onDraw(Canvas) will be called at some point in future. This method should only be called from a UI thread. If calling from a non-UI thread the call postInvalidate().


Note :  We don't call onDraw() directly, instead we call invalidate(). To better optimization try to call this method as less frequently as possible.


7. requestLayout() : Call to this method happens when something has changed and it has invalidated the layout of this view. The call to this method will schedule a layout phase (measure -> layout -> draw) of the view tree. In the simple terms call requestLayout() when there is some change in layout view bound.


Note : You should always call this method from UI thread. If calling from non-UI thread use Handler.


This all the methods require to draw a custom view. Hope you have a better idea now on custom views lifecycle. Next we will learn to create our own custom view.


We, at Oodles Technologies, provide end-to-end mobile app development services to build scalable and user-friendly mobile apps to address varied business requirements. Our development team is skilled at using the latest mobile app development tools, frameworks, and SDKs to build native, hybrid, and cross-platform mobile apps with custom features. Our 360-degree mobile app development services enable organizations to meaningfully engage their target audience and gain their trust and loyalty. For project-related queries, contact us at [email protected].

About Author

Author Image
Bipin Tiwari

Bipin is a highly skilled Android developer with extensive professional experience in creating innovative and efficient mobile applications. He possesses proficiency in programming languages such as Java and Kotlin, along with a deep understanding of Android SDK, design patterns, and best coding practices. Bipin is committed to delivering high-quality code and ensuring exceptional user experiences. With excellent problem-solving skills and the ability to work collaboratively in a team environment, he has made valuable contributions to both client and internal projects, including the development of the Blackbook Travels App and Corniz App.

Request for Proposal

Name is required

Comment is required

Sending message..