title: Drawing multiple object in view using onDraw android
body: <p>I'm not working on any project, just simply want to learn how to create simple animation using view onDraw method. So i started with drawing multiple objects on custom view, but my program draws only two of defined object with wrong colors.</p>
  
  <p>This is my simple object class, named Square which extends abstract class AbstractObject which defines properties and methods for square object. </p>
  
  <pre><code>
  
      package com.kgkg.animationlearn.myDrawings;
      import android.graphics.Canvas;
      import android.graphics.Paint;
      import android.util.Log;
      import com.kgkg.animationlearn.MainActivity;
      /**
       * Created by Krzysiek on 2016-03-11.
       */
      public class Square extends AbstractShape {
          //****************************************************FIELDS*******************
          private int side;
  
          //**************************************************CONSTRUCTORS*******************************
          private Square() {}
  
          public Square(int side) {
              super();
              this.side = side;
          }
  
          public Square(Paint fillColor, int side) {
              super(fillColor);
              this.side = side;
          }
  
          public Square(Paint fillColor, int x, int y, int side) {
              super(fillColor, x, y);
              this.side = side;
          }
  
          public Square(int x, int y, int side) {
              super(x, y);
              this.side = side;
          }
  
          //************************************************GETTERS AND SETTERS**************************
          public int getSide() {
              return side;
          }
  
          public void setSide(int side) {
              this.side = side;
          }
  
          //****************************************************METHODS**********************************
  
          /**
           * side, canvas and paint fields cannot be null otherwise they will use defaults
           * @param canvas
           */
          @Override
          public void drawOnCanvas(Canvas canvas) {
              if (side > 0 && canvas != null){
                  canvas.drawRect(getX(), getY(), side, side, getFillColor());
              } else {
                  Log.i(MainActivity.TAG, "Somthing is wrong with your object");
              }
          }
  
          @Override
          public String toString() {
              return "Square object \n Properties: \n side: " + side + "\n fillColor: " + getFillColor() +
                      "\nPosition ( " + getX() + ", " + getY() + ")";
          }
      }
  
  </code></pre>
  
  <p>AbstractObject class</p>
  
  <pre><code>
  
      public abstract class AbstractShape implements IDrawOnCanvas{
  
          //****************************************************FIELDS***********************************
          private Paint fillColor = new Paint(Color.DKGRAY);
          private int x, y;
  
          //**************************************************CONSTRUCTORS*******************************
          public AbstractShape() {}
  
          public AbstractShape(Paint fillColor) {
              this.fillColor = fillColor;
          }
  
          public AbstractShape(Paint fillColor, int x, int y) {
              this.fillColor = fillColor;
              this.x = x;
              this.y = y;
          }
          public AbstractShape(int x, int y) {
              this.x = x;
              this.y = y;
          }
  
          //************************************************GETTERS AND SETTERS**************************
          public Paint getFillColor() {
              return fillColor;
          }
  
          public void setFillColor(Paint fillColor) {
              this.fillColor = fillColor;
          }
  
          public int getX() {
              return x;
          }
  
          public void setX(int x) {
              this.x = x;
          }
  
          public int getY() {
              return y;
          }
  
          public void setY(int y) {
              this.y = y;
          }
  
          public void setPosition(int x, int y){
              this.x = x;
              this.y = y;
          }
  
          //****************************************************METHODS**********************************
  
      }
  
  </code></pre>
  
  <pre><code>Interface IDrawOnCanvas
  &lt;pre&gt;&lt;code&gt;
  package com.kgkg.animationlearn.myDrawings;
  import android.graphics.Canvas;
  public interface IDrawOnCanvas {
      void drawOnCanvas(Canvas canvas);
  }
  </code></pre>
  
  <p></p>
  
  <pre><code>View class
  &lt;pre&gt;&lt;code&gt;
  public class myView extends View {
  
      private boolean toDraw;
      private Square[] objects;
  
      public myView(Context context) {
          super(context);
          init();
      }
  
      public myView(Context context, AttributeSet attrs) {
          super(context, attrs);
          init();
      }
  
      private void init(){
          toDraw = false;
      }
  
      public boolean isToDraw() {
          return toDraw;
      }
  
      public void setToDraw(boolean toDraw) {
          this.toDraw = toDraw;
      }
  
      public Square[] getObjects() {
          return objects;
      }
  
      public void setObjects(Square[] objects) {
          this.objects = objects;
      }
  
      @Override
      protected void onDraw(Canvas canvas) {
          if (toDraw){
              for (int i = 0; i &lt; objects.length; i++){
                  canvas.save();
                  Log.i(MainActivity.TAG, "rysownaie + " + objects[i].toString());
                  objects[i].drawOnCanvas(canvas);
                  canvas.restore();
                  invalidate();
              }
          }
          super.onDraw(canvas);
      }
  
  
  }
  </code></pre>
  
  <p></p>
  
  <pre><code>MainActivity
  &lt;pre&gt;&lt;code&gt;
  public class MainActivity extends AppCompatActivity {
  
      public final static String TAG = "kgkg";
  
      RelativeLayout relLayout;
      Button btnTest;
      View myV;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          relLayout = (RelativeLayout) findViewById(R.id.relLayout);
          btnTest = (Button) findViewById(R.id.button);
          myV = findViewById(R.id.view);
          btnTest.setOnClickListener(c);
      }
  
      private View.OnClickListener c = new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              Square[] squares = {new Square(new Paint(Color.RED), 0, 0, 50), new Square(50, 50, 100), new Square(50, 0, 50), new Square(new Paint(Color.CYAN), 150, 150, 130)};
              ((myView)myV).setToDraw(true);
              ((myView) myV).setObjects(squares);
              myV.invalidate();
          }
      };
  }
  </code></pre>
  
  <p>
  It should create four squares on myView with red, cyan and black colors after button's onclick method, but instead it creates only two squares with black color, my question is: what is the problem ? why only two objects are drawn and why they don't have proper colors ?</p>
  
  <p>Thank you in advance</p>