Graphics Should Persist When Other Button On The Activity Is Pressed
<p>In my example when I press a button, a shape is displayed but when I press another button the previous shape gets erased.I want the shape to persist when another button is pressed. I am using the method <code>invalidate()</code> after every shape is displayed. Can you please give me a solution?Below is the Activity i have used.</p>

<pre><code>public class StartMyDrawView extends Activity{
    MyDrawView mydrawview;
    public static int action=0;
    Intent netIntent;
    LinearLayout draw ;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("StartMyDrawView", "OnCreate()");
        setContentView(R.layout.designgraphic);
        draw = (LinearLayout) findViewById(R.id.linearLayout1);
        Button btnLine=(Button) findViewById(R.id.button1);
        Button btnCircle=(Button) findViewById(R.id.button2);
        Button btnRectangle=(Button) findViewById(R.id.button4);
        Button btnText=(Button) findViewById(R.id.button5);

        btnLine.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                action=1;
                draw.invalidate();
            }
        });

        btnCircle.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                action=2;
                draw.invalidate();
            }
        });

       btnRectangle.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                action=3;
                draw.invalidate();
            }
        });

        btnText.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                action=4;
                draw.invalidate();
            }
        });

        mydrawview =new MyDrawView(this);
        draw.addView(mydrawview);
    }
</code></pre>

<p>}</p>

<p>and My class that extends View is as follows</p>

<p>public class MyDrawView extends View {</p>

<pre><code>Paint paint = new Paint();
int actionVal=0;
public MyDrawView(Context context) {
    super(context);
}

public void onDraw(Canvas canvas)
{
    super.onDraw(canvas);
     paint.setColor(Color.RED);
     paint.setStyle(Paint.Style.STROKE);
     if(StartMyDrawView.action==1)
         canvas.drawLine(0, 0, 200, 200, paint);
     if(StartMyDrawView.action==2)
         canvas.drawCircle(150, 150, 50, paint);
     if(StartMyDrawView.action==3)
         canvas.drawRect(20, 20, 150, 150, paint);
     if(StartMyDrawView.action==4)
         canvas.drawText("JUST DEMO", 150, 150, paint);
}
</code></pre>

<p>}</p>
