38

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.

1
  • "It gets called upon instantiation of the view" - A View is either constructed during it's inflation (from an xml layout) or directly "from code". None of the measurements are yet known when onFinishInflate completes, it is only when the View is actually "rendered" they calculated (i.e. when onAttachedToWindow, onMeasure, onLayout, and onDraw are called).
    – samus
    Commented Nov 21, 2018 at 17:25

6 Answers 6

66

AFAIK, a View's onDraw() is called when:

  1. The view is initially drawn
  2. 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.

3
  • This is not the case for ViewGroups. 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 after invalidate(). Like the doc says, after invaldiate(), if the view is visible, onDraw(android.graphics.Canvas) will be called at some point in the future.
    – Sơn Phan
    Commented 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/…)
    – VanessaF
    Commented Nov 24, 2021 at 17:48
23

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

9
  • 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.

1
1

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.

1
  • 1
    This 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
1

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.

0

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 (-:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.