I put a Log.d() call into the onDraw() of my extended View, so I could see how often and when it's getting called. It gets called upon instantiation of the view, which is not surprising. But then I notice, it gets called on every tap that is handled by onTouchEvent(), even though my code there isn't doing anything remotely related to graphics. However, in the documentation for Views, I can't seem to find anything about when onDraw() is actually called. I'm not really concerned about my particular project here (this doesn't cause a problem for me), I would just like to know if there is a list somewhere or something that shows the order of operations for a View, particularly what causes onDraw() to get called.
6 Answers
AFAIK, a View's onDraw() is called when:
- The view is initially drawn
- Whenever invalidate() is called on the view
Invalidate can be called by you or the system whenever needed. For example, a lot of Views change how they look onTouch, like an EditText getting an outline and cursor, or a button being in the pressed state. Due to this, Views are redrawn on touch.
I agree that it would be nice to have a document that detailed the working of Views, and if one exists and somebody knows where to find it, please let us know.
-
This is not the case for
ViewGroup
s. See my answer stackoverflow.com/a/34367199/1642079 Commented Oct 25, 2016 at 17:52 -
One aspect to pay attention to is that View's
onDraw()
is not guaranteed to be called immediately afterinvalidate()
. Like the doc says, afterinvaldiate()
, if the view is visible,onDraw(android.graphics.Canvas)
will be called at some point in the future.– Sơn PhanCommented Oct 30, 2020 at 3:05 -
@Raghav: I tried your suggested approach (in a View and not in a ViewGroup). The invalidate() call does not trigger the onDraw() method in my example (see stackoverflow.com/questions/70038233/…)– VanessaFCommented Nov 24, 2021 at 17:48
onDraw()
is called when invalidate()
is called.
But you should know for ViewGroups: onDraw()
will not be called like you expect. Rather, onDispatchDraw()
.
However, in a ViewGroup you can call setWillNotDraw(false)
in the constructor to make onDraw()
to be called on invalidate()
.
Take a look at this answer
If you set a background drawable for a View, then the View will draw it for you before calling back to its onDraw() method.
onAttachedToWindow () is called when the view is attached to a window. At this point it has a Surface and will start drawing. Note that this function is guaranteed to be called before onDraw(android.graphics.Canvas), however it may be called any time before the first onDraw -- including before or after onMeasure(int, int).
invalidate() mark the area defined by dirty as needing to be drawn. If the view is visible, onDraw(android.graphics.Canvas) will be called at some point in the future.
-
Is a way we can intercept before calling back onDraw(), would you like to check my question stackoverflow.com/questions/19052952/… Commented Sep 28, 2013 at 6:57
One important thing to keep in mind is that try to minimize calling of invalidate() function with no arguments. Instead try to maximize the invalidate() function with four arguments.As drawing whole view is very expensive.The second variant refreshes only the part of view.
-
1This answer is outdated, the API is updated so that it calculates the "dirty rectangle" by itself, and thus this function is deprecated. Commented Jan 4, 2020 at 16:12
When is onDraw called (check this for more details)
The onDraw method is called whenever android thinks that your view should be redrawn. This can be tha case when your view is animated, in which case onDraw is called for every frame in the animation. It is also called when the layout changes and your view is re-positioned on the screen.
But what if some data inside your view has changed and you want to make sure that the view is redrawn. You can’t call onDraw directly. Instead you should call invalidate to tell the view that it needs to redraw itself.
Additional to the above: The soft keyboard causes a View.invalidate()-->View.onDraw() sequence after resizing the Window to sensibly accommodate the 'keyboard'. A custom View.onDraw() must leave itself in a state that anticipates this possibility.
Such phenomenum explains why the app you developed and tested on a tablet with a bluetooth keyboard went to the dogs once it reached the real world (-:
View
is either constructed during it's inflation (from an xml layout) or directly "from code". None of the measurements are yet known whenonFinishInflate
completes, it is only when theView
is actually "rendered" they calculated (i.e. whenonAttachedToWindow
,onMeasure
,onLayout
, andonDraw
are called).