android - Why onDraw is not called after invalidate()? -


i have found many posts on stackoverflow still cannot solve problem. here code piece:

public class myview extends relativelayout {  button b1; button b2; context scontext; public static int = 0; private int w = 400; private int h = 400; private int w2 = 100; private int h2 = 100;  public myview(context context) {     super(context);     scontext = context;     init(); }  public myview(context context, attributeset attrs) {     super(context, attrs);     scontext = context;     init(); }  private void init() {     b1 = new button(scontext);     addview(b1);     b1.setbackgroundcolor(color.yellow);      b1.setonclicklistener(new onclicklistener() {          @override         public void onclick(view v) {              if (w >= 600) {                 myview.this.setbackgroundcolor(color.green);                 //b1.setbackgroundcolor(color.red);             } else {                 myview.this.setx(100);             }              myview.this.invalidate();             w += 100;             w2 += 20;         }      }); }  @override protected void ondraw(canvas canvas) {     super.ondraw(canvas);     //b1.setbackgroundcolor(color.red);     toast.maketext(scontext, ""+i, toast.length_short).show();     ++i; } } 

would please explain why ondraw not called first 3 times press b1? because called invalidate everytime press b1. thank much!

by default viewgroup sub-classes not call ondraw method, should enable calling setwillnotdraw(false) link


Comments