Stack Exchange
Android Canvas not drawing anything if not using OnDraw
class SquareImageView extends View {



    public SquareImageView(Context context) {
        super(context);

        init();
    }

    private void init() {

        bitmap = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888);
       =mCanvas = new Canvas(bitmap);
        mpaint = new Paint();
        mpaint.setAntiAlias(true);
        mpaint.setStyle(Paint.Style.STROKE);
        mpaint.setColor(Color.RED);
        mCanvas.drawColor(Color.BLUE, PorterDuff.Mode.DARKEN);
        mCanvas.drawRect(100, 100, 200, 200, mpaint);


    }





    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

    }``

}



    if i use mCanas.drawRect() inside init method then it's not drawing anything but if i use inBuilt canvas object inside OnDraw(Canvas canvas) method like this

  @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
           canvas.drawRect(100, 100, 200, 200, mpaint);

        }

then rectangle is drawing  and  if i used mCanvas inside onDraw()  then again it's not showing anything can anybody explain to me why this happening i m really get frustrated any help would be appreciated

You should put all your drawing methods within onDraw. This is required by Android. Basically, onDraw methods puts all your drawing commands in queue, and the system executes all those drawing during a special vsync cycle, which happens 60 times in a second.

so it means without using onDraw i Can' t draw anything really?

because its not drawing anything inside onDraw as well as inside init() ,but if i used canvas inside onDraw(Canvas canvas) then rectangle is showing

it shows empty bitmap

It means that onDraw is the only drawing gate for you. https://developer.android.com/training/custom-views/custom-drawing#java

but if i used mCanvas inside onDraw() then again it's not showing anything

@YogeshSingh mCanvas is not the View's Canvas. It's one that you've created separately. The Canvas passed into onDraw() is the one you need to draw to in order to be able to see it on the View.

Thanks for everyOne
