Magic Marbles Magic Marbles - 4 months ago 5x
Android Question

Canvas won't draw

I don't get it, I have other custom views in which I can draw to canvas perfectly fine. But now the new views I'm creating don't draw to canvas at all! I can't find any discernible difference between what I was doing then and now, it's still the same "canvas.drawLine()" calls afterall.

Here's an example view that will not draw regardless of what I try to do with it:

public class TestView extends View {

public TestView(Context context)

public TestView (Context context, AttributeSet attrs)
super(context, attrs);

public void onDraw(Canvas canvas){
Paint paint = new Paint();

canvas.drawLine(0,0, 500, 500, paint);
canvas.drawCircle(0,0, 40, paint);
Logy.Print("Drew top:" + getTop() + " left:" + getLeft() + " right:" + getRight() + " bottom:" + getBottom());


I've looked at other similar questions, and this issue I'm having is definitely not like the others. I didn't mispell onDraw, onDraw IS being called (I can tell via Logy, which is just a simpler facade to Debug.log()). Please consider this before marking this as duplicate of something else.

The dimensions of the view when onDraw is called is 0,0,800,800 (left, top, right, bottom). onDraw is called at least once when the activity starts.
canvas.drawRGB() will flood the view with color.

This view is the only view in the activity, and when I put another "working" custom view in it's place that draws just fine. But now any new custom views I try to get drawing DON'T DRAW.


The colour you are drawing with has an alpha value of 0 (aka transparent), and colour of white. Try drawing with use paint.setColor(0xFFFF3432); or something similarly likely to contrast with your background -- see if you see a red line, you know it is working.

Then choose the colour you wish. If it is white you are going for make sure to use 0xFFFFFFFF not 0xFFFFFF

Colour Format:


where A = Alpha/Transparency, R = Red, G = Green, B = Blue.