title: how to clear canvas created using custom view on button click
body: <p>I have created an canvas using custom view and it is working fine. I am able to write draw on it using mouse. But i want to clear the painting drawn on canvas using a button click. I have tried my best but not able to do that. plz help</p>
  
  <p>THIS MY MAIN ACTIVITY</p>
  
  <pre><code>package com.example.paint;
  public class MainActivity extends Activity
  {
  
      LinearLayout canvasAlphabets;
       SingleTouchEventView myView; 
  
      @Override
        public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          Button btnClear = (Button)findViewById(R.id.btnSearchNameWise);
          myView = new SingleTouchEventView(this, null);
         canvasAlphabets = (LinearLayout)findViewById(R.id.First);
          canvasAlphabets.addView(myView);
  
          btnClear.setOnClickListener(new OnClickListener() {
  
              @Override
              public void onClick(View v) 
              {
                  myView.clearCanvas();
                  Toast.makeText(getApplicationContext(), "hello", Toast.LENGTH_LONG).show();
  
              }
          });     
        } 
  
  }
  </code></pre>
  
  <p>THIS IS MY SingleTouchEventView CLASS</p>
  
  <pre><code>public class SingleTouchEventView extends View {
        private Paint paint = new Paint();
        private Path path = new Path();
        public boolean cc = false;
  
  
        public SingleTouchEventView(Context context, AttributeSet attrs) {
          super(context, attrs);
  
          paint.setAntiAlias(true);
          paint.setStrokeWidth(6f);
          paint.setColor(Color.BLACK);
          paint.setStyle(Paint.Style.STROKE);
          paint.setStrokeJoin(Paint.Join.ROUND);
        }
  
        @Override
        protected void onDraw(Canvas canvas) 
        {
            if(cc)
              { 
  
                path = new Path(); 
              Paint clearPaint = new Paint(); 
              clearPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
              canvas.drawRect(0, 0, 0, 0, clearPaint); 
  
              cc = false; 
  
              }
            else
  
          canvas.drawPath(path, paint);
  
        }
  
        public void clearCanvas()
        {
            Toast.makeText(getContext(), "hello in canvas", Toast.LENGTH_LONG).show();
          cc =true;
          invalidate();
        }
  
        @Override
        public boolean onTouchEvent(MotionEvent event) {
          float eventX = event.getX();
          float eventY = event.getY();
  
          switch (event.getAction()) {
          case MotionEvent.ACTION_DOWN:
            path.moveTo(eventX, eventY);
            return true;
          case MotionEvent.ACTION_MOVE:
            path.lineTo(eventX, eventY);
            break;
          case MotionEvent.ACTION_UP:
            // nothing to do
            break;
          default:
            return false;
          }
  
          // Schedules a repaint.
          invalidate();
          return true;
        }
      }
  </code></pre>
  
  <p>AND THIS IS MY LAYOUT FILE</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent" &gt;
  
     &lt;LinearLayout
        android:id="@+id/First"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="5dip"
        android:paddingTop="10dip" 
        android:orientation="vertical"
        android:weightSum="1"
        android:background="#aabbcc"&gt;
  
  
         &lt;com.example.paint.SingleTouchEventView
             android:id="@+id/cusView"
            android:layout_width="match_parent"
          android:layout_height="wrap_content"  
  
         /&gt;
  
  
         &lt;/LinearLayout&gt;
  
      &lt;LinearLayout
        android:id="@+id/Second"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:orientation="vertical"
        android:paddingBottom="50dip"
        android:weightSum="1"
        android:background="#ffaacc" &gt;
  
          &lt;Button
            android:id="@+id/btnSearchNameWise"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="3dip"
            android:paddingRight="3dip"
            android:text="Search Name Wise" /&gt;
  
            &lt;/LinearLayout&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>