title: Redraw canvas in Android
body: <p>I am making an Android 2.2 application using Eclipse. My application should draw 
  images by clicking on buttons.</p>
  
  <p>I have two buttons at the bottom of the screen, and I need to draw the shape in the same screen that has the buttons. I used <code>FrameLayout</code> in which the shapes are drawn.</p>
  
  <p>My problem is, the two shapes are overwriting, so I need to redraw the canvas. </p>
  
  <p><strong>My Code</strong></p>
  
  <pre><code>package draw.tab;
  
  import android.app.Activity;
  //import android.graphics.Color;
  import android.os.Bundle;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.FrameLayout;
  
  public class DrawActivity extends Activity implements OnClickListener
  {
      DrawView drawview;
      CircleView circleView;
      FrameLayout Frame;
      Button square,circle;
  
      /** Called when the activity is first created. */
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
          Frame=(FrameLayout)findViewById(R.id.MyFrame);      
          drawview=new DrawView(this);
          circleView=new CircleView(this);
  
          square=(Button)findViewById(R.id.buttonTest);
          square.setOnClickListener(this);
          circle=(Button)findViewById(R.id.circleButton);
          circle.setOnClickListener(this);
      }
  
      public void onClick(View v)
      {
          switch (v.getId()) {
              case R.id.circleButton:
                  Frame.addView(circleView);
                  /*circleView.setBackgroundColor(Color.rgb(40,100,20));
                  circleView.findViewById(R.id.CircleViewId);
                  setContentView(circleView);*/
                  break;
              case R.id.buttonTest:
                  Frame.addView(drawview);
                  /*drawview.setBackgroundColor(Color.WHITE);
                  drawview.findViewById(R.id.DrawViewId);
                  drawview.layout(400,0,200,450);
                  setContentView(drawview);*/
                  break;
          }
      }
  }
  </code></pre>
  
  <p><strong>DrawView.java</strong></p>
  
  <pre><code>package draw.tab; 
  
  import android.content.Context;
  import android.graphics.Canvas;
  import android.graphics.Color;
  import android.graphics.Paint;
  import android.graphics.Paint.Style;
  import android.graphics.Path;
  import android.util.AttributeSet;
  import android.view.View;
  
  public class DrawView extends View
  {
      Paint paint=new Paint();
      Path pat=new Path();
  
      public DrawView(Context context) 
      {
          super(context);
          paint.setColor(Color.RED);
          paint.setStyle(Style.STROKE);
          //context.clearRect(70,140,400,450);
          //pat.addRect(50,40,250,400,Path.Direction.CW);
      }
  
      public DrawView(Context con,AttributeSet atts)
      {
          super(con,atts);
      }
  
      @Override
      public void onDraw(Canvas canvas) 
      {       
          //canvas.drawPath(pat,paint);
          canvas.drawLine(20,40,450,40,paint);//horizontal top            
          canvas.drawRect(70,140,400,450,paint);
          canvas.drawLine(20,40,20,600,paint);//vertical left
          canvas.drawLine(20,600,450,600,paint);//horizontal bottom
          canvas.drawLine(450,40,450,600,paint);//vertical right
          //this.invalidate();
      }
  }
  </code></pre>
  
  <p><strong>CircleView.java</strong></p>
  
  <pre><code>package draw.tab;
  
  import android.content.Context;
  import android.graphics.Canvas;
  import android.graphics.Color;
  import android.graphics.Paint;
  import android.graphics.Paint.Style;
  import android.graphics.Path;
  import android.util.AttributeSet;
  import android.view.View;
  
  public class CircleView extends View
  {
      Paint p=new Paint();
      Path path=new Path();
  
      public CircleView(Context context)
      {
          super(context);
          p.setColor(Color.BLUE);
          p.setStyle(Style.STROKE);
          //path.addCircle(250,250,50,Path.Direction.CW);
      }
  
      public CircleView(Context con,AttributeSet atts)
      {
          super(con,atts);
      }
  
      public void onDraw(Canvas c)
      {
          //c.drawPath(path,p);
          //c.save();
          c.drawLine(20,40,450,40, p);//horizontal top
          c.drawCircle(250,350,100,p);
          c.drawLine(20,40,20,600,p);//vertical left
          c.drawLine(20,600,450,600,p);//horizontal bottom
          c.drawLine(450,40,450,600,p);//vertical right
          //c.restore();
          this.invalidate();
      }
  }
  </code></pre>
  
  <p><strong>main.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  
  &lt;FrameLayout 
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:visibility="visible"
      android:id="@+id/MyFrame"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"&gt;
  
      &lt;draw.tab.DrawView
      android:id="@+id/DrawViewId"
      android:layout_width="320dp"
      android:layout_height="600dp"&gt;
      &lt;/draw.tab.DrawView&gt;
  
      &lt;draw.tab.CircleView
      android:id="@+id/CircleViewId"
      android:layout_width="320dp"
      android:layout_height="600dp"&gt;
      &lt;/draw.tab.CircleView&gt;
  
  &lt;LinearLayout 
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="horizontal"
      android:gravity="bottom"&gt;
  
       &lt;Button 
          android:layout_height="wrap_content" 
          android:text="Circle" 
          android:id="@+id/circleButton" 
          android:layout_width="160dp"&gt;
      &lt;/Button&gt;
  
      &lt;Button 
          android:layout_height="wrap_content" 
          android:text="Square" 
          android:id="@+id/buttonTest" 
          android:layout_width="160dp"&gt;
      &lt;/Button&gt;
  &lt;/LinearLayout&gt;
  
  &lt;/FrameLayout&gt;
  </code></pre>
  
  <p>The above is all my code. The shapes are overwriting, so I want to remove and redraw the canvas or redraw without overwriting.</p>