95

I defined a class like that:

public class TestMyFrameLayout extends FrameLayout{

    Paint mPaint;
    public TestMyFrameLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TestMyFrameLayout(Context context) {
        super(context);
        mPaint = new Paint();
        mPaint.setColor(Color.GREEN);
    }

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(50f, 50f, 30, mPaint);
    }   
}

and called it like:

TestMyFrameLayout myFrameLayout = new TestMyFrameLayout(this);
LayoutParams myFrameLayoutParams = new LayoutParams(300,300);
myFrameLayout.setLayoutParams(myFrameLayoutParams);
setContentView(myFrameLayout);

But In fact TestMyFrameLayout.onDraw(Canvas canvas) function not getting called, why?

MrSnowflake
  • 4,724
  • 3
  • 29
  • 32
old_hou
  • 3,303
  • 2
  • 14
  • 9
  • Could you show the code where you make use of it? Also add `@Override` before onDraw: this will check for typo in eclipse. – Laurent' Oct 16 '11 at 09:14
  • 3
    Thank you Laurent'. My problem is already solved. Add this.setWillNotDraw(false); – old_hou Oct 16 '11 at 11:10

1 Answers1

235

Solved. Add this.setWillNotDraw(false); in constructor

Girish Nair
  • 5,148
  • 5
  • 40
  • 61
old_hou
  • 3,303
  • 2
  • 14
  • 9