I’m extending LinearLayout:
public class MyLinearLayout extends LinearLayout {
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(0xFFFF0000);
canvas.drawLine(0, 0, getWidth(), getHeight(), paint);
}
}
If the layout has child elements, shouldn’t the red line above be drawn on top of them? Considering a layout like:
<MyLinearLayout>
<ImageView />
</MyLinearLayout>
I’d expect to get the red line drawn above the child ImageView. But it gets drawn below it. I was assuming all child drawing would have been completed after the super.onDraw() line is finished.
Is there a way to get to the canvas and draw something on it after all child drawing has been completed?
Thanks
Layouts don’t draw unless you call
setWillNotDraw(false);. They do that for efficiency reasons.EDIT:
onDraw()really is meant to just allow you to modify theCanvasbefore the drawing operation happens. It doesn’t actually draw. What you want to do is overridedraw()like so:Calling the super will draw all the children in the view. Then it will draw all the lines. I do still believe you need
setWillNotDraw(false)to be called though.