Why onDraw is not called after invalidate()?

Android

Android Problem Overview


I have found many posts on stackoverflow but I still cannot solve my problem. Here is my code piece:

public class MyView extends RelativeLayout {

Button b1;
Button b2;
Context sContext;
public static int i = 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 you please explain why onDraw is not called the first three times I press b1? Because I called invalidate everytime I press b1. Thank you very much!

Android Solutions


Solution 1 - Android

By default all ViewGroup sub-classes do not call their onDraw method, you should enable it by calling setWillNotDraw(false) link

Solution 2 - Android

Borrowing from the link @

https://stackoverflow.com/questions/13056331/android-custom-layout-ondraw-never-gets-called

Try the below it works

1.If you're extending a ViewGroup (in your case a RelativeLayout) you should override dispatchDraw() instead of onDraw().

Discussion on the topic @

https://groups.google.com/forum/?fromgroups=#!topic/android-developers/oLccWfszuUo

protected void dispatchDraw (Canvas canvas)

Called by draw to draw the child views. This may be overridden by derived classes to gain control just before its children are drawn (but after its own view has been drawn).

Parameters

canvas the canvas on which to draw the view

Example

public class Hello  extends Activity {
	
	private MyView myView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);

	    	    super.onCreate(savedInstanceState);
	    	    Log.e("hello", "hello");
	    	    this.myView = new MyView(this);
	    	    setContentView(this.myView);

	}
	 public class MyView extends RelativeLayout
	   {

	  private Paint myPaint = new Paint();
	  private int[] numbers;
	  public MyView(Context paramContext)
	  {
	    super(paramContext);
	    Log.e("MyView", "MyView");
	    setFocusable(true);
	    setBackgroundResource(R.drawable.ic_launcher);
	  }
	  @Override
	  protected void dispatchDraw(Canvas canvas){         
		  
		    super.dispatchDraw(canvas);     
		    Log.i("...............","drawing");   
		}

  }
}

2.If you Override onDraw in the constructor call setWillNotDraw(false) then it should work.

http://developer.android.com/reference/android/view/View.html#setWillNotDraw(boolean)

public MyView(Context context) {
super(context);
sContext = context;
init();
setWillNotDraw(false); 
}

                 

Solution 3 - Android

You need to call setWillNotDraw(false); in order for your onDraw method to be called:

private void init() {
  setWillNotDraw(false);
  ...
}

Solution 4 - Android

I was having OnDraw not called in C# using 4.1 Jelly Bean API 16. I switched from compiling with API 16 SDK to compiling with 7.1 Nougat SDK. It solved the problem completely. I still have in manifest min sdk of API 16 and it runs fine in my Jelly Beam avd.

Solution 5 - Android

In my case onDraw was not called because I returned 0 as width or height of view.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondarklordView Question on Stackoverflow
Solution 1 - AndroidTomas ŽemaitisView Answer on Stackoverflow
Solution 2 - AndroidRaghunandanView Answer on Stackoverflow
Solution 3 - AndroidCodingIntrigueView Answer on Stackoverflow
Solution 4 - AndroidD WoodhouseView Answer on Stackoverflow
Solution 5 - AndroidFartabView Answer on Stackoverflow