What is the use of dispatchDraw(Canvas canvas) method in ViewGroup class??
2 Answers
From the Android documentation - protected void dispatchDraw (Canvas canvas)
:
Called by draw to draw the child views. This may be overridden by derived classes to gain control just before its children are drawn (but after its own view has been drawn).

- 66,919
- 24
- 133
- 141
-
5That is where you should do your drawing, yes. `draw(Canvas canvas)` initiates drawing; `onDraw(Canvas canvas)` is where drawing takes place; `dispatchDraw(Canvas canvas)` is what tells the children of the `ViewGroup` to be drawn. – Cat Aug 13 '12 at 05:33
For example, ViewGroup
is the Parent of Linear-, Relative-, FrameLayout-, AbsoluteLayout. 1
Because the ViewGroup Class is abstract, directly creating an Instance is not possible.
You can use 1 from this, with the function:
dispatchDraw(Canvas canvas)
For unknown reasons, classes 1 wouldn't draw with
onDraw(Canvas canvas)
Drawing with
dispatchDraw(Canvas canvas)
is in this Classes 1, possible and it is fun to create UI's.
It would look like this:
/////
public class MainActi extends MyLinearLayout.
public MainActi(Context con...
super(cont...)
MyLinearLayout extends LinearLayout
@Override protected void dispatchDraw(Canvas canvas) {
canvas.drawColor(Color.YELLOW);
... ///
Or you skip-back to draw with
onDraw(Canvas canvas)
extending in the MainActivity
"View" or refer it in a extern class like this:
MyMelIsDrawing extends View
onDraw(Canvas canvas)
//now draw...

- 11,410
- 7
- 34
- 49

- 117
- 1
- 7