title: Clearing SurfaceView in android
body: <p>how to clear the surfaceView in android?</p>
  
  <p>I have a class like this - </p>
  
  <pre><code>class DrawingPanel extends SurfaceView implements SurfaceHolder.Callback {
  </code></pre>
  
  <p>can any one tell as to which method shud i call to clear my custom drawingPanel</p>
  
  <p>This is my class - </p>
  
  <pre><code>package com.applenty.LearnToCount;
  
  import java.util.ArrayList;
  
  import android.app.Activity;
  import android.content.Context;
  import android.content.Intent;
  import android.graphics.Canvas;
  import android.graphics.Color;
  import android.graphics.ColorFilter;
  import android.graphics.Paint;
  import android.graphics.Path;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.Gravity;
  import android.view.KeyEvent;
  import android.view.MotionEvent;
  import android.view.SurfaceHolder;
  import android.view.SurfaceView;
  import android.view.View;
  import android.view.ViewGroup;
  import android.view.Window;
  import android.view.WindowManager;
  import android.view.ViewGroup.LayoutParams;
  import android.widget.Button;
  import android.widget.FrameLayout;
  import android.widget.LinearLayout;
  import android.widget.Toast;
  
  public class WhiteBoard extends Activity {
      private ArrayList&lt;Path&gt; _graphics = new ArrayList&lt;Path&gt;();
      private Paint mPaint;
      Button button_clear;
      FrameLayout layout;
  
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          requestWindowFeature(Window.FEATURE_NO_TITLE);
          getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                  WindowManager.LayoutParams.FLAG_FULLSCREEN);
          setContentView(R.layout.shake);
  
          layout = new FrameLayout(this);
          final LinearLayout l1 = new LinearLayout(this);
          final LinearLayout l2 = new LinearLayout(this);
  
          final DrawingPanel drawingPanel = new DrawingPanel(this);
          // drawingPanel.setBackgroundColor(Color.WHITE);
  
          LayoutParams params = new LayoutParams(
                  ViewGroup.LayoutParams.WRAP_CONTENT,
                  ViewGroup.LayoutParams.WRAP_CONTENT);
  
          button_clear = new Button(this);
          button_clear.setText("Clear Screen");
          button_clear.setGravity(Gravity.TOP | Gravity.LEFT);
          button_clear.setVisibility(View.VISIBLE);
          button_clear.setId(1);
          button_clear.setLayoutParams(params);
          //layout.addView(button_clear);
  
          drawingPanel.setLayoutParams(params);
          drawingPanel.setId(2);
  
  
          l1.addView(button_clear);
          l2.addView(drawingPanel);
  
          layout.addView(l2);
          layout.addView(l1);
          setContentView(layout);
          mPaint = new Paint();
          mPaint.setDither(true);
          mPaint.setColor(Color.RED);
          mPaint.setColorFilter(new ColorFilter());
          mPaint.setStyle(Paint.Style.STROKE);
          mPaint.setStrokeJoin(Paint.Join.ROUND);
          mPaint.setStrokeCap(Paint.Cap.ROUND);
          mPaint.setStrokeWidth(5);
  
          button_clear = (Button)findViewById(1);
          button_clear.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {
                  Toast t = Toast.makeText(WhiteBoard.this, "ISNIDE", 3);
                  t.show();
                  Log.d("DEBUG","CLEARING***************************");
                  //Canvas canvas = new Canvas();
                  //canvas.drawColor(0);
                  //Canvas canvas = new Canvas();
                  //canvas.drawColor(Color.WHITE);
                  drawingPanel.setBackgroundColor(Color.WHITE);
                  Canvas c = new Canvas();
                  drawingPanel._thread.getSurfaceHolder().unlockCanvasAndPost(c);
  
  
                  //drawingPanel.getHolder().unlockCanvasAndPost(canvas);
                  //canvas = drawingPanel.getHolder().lockCanvas(null);
              }
          });
  
      }
  
      class DrawingPanel extends SurfaceView implements SurfaceHolder.Callback {
          private DrawingThread _thread;
          private Path path;
  
          public DrawingPanel(Context context) {
              super(context);
              getHolder().addCallback(this);
              _thread = new DrawingThread(getHolder(), this);
          }
  
          public boolean onTouchEvent(MotionEvent event) {
              synchronized (_thread.getSurfaceHolder()) {
                  if (event.getAction() == MotionEvent.ACTION_DOWN) {
                      path = new Path();
                      path.moveTo(event.getX(), event.getY());
                      path.lineTo(event.getX(), event.getY());
                  } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
                      path.lineTo(event.getX(), event.getY());
                  } else if (event.getAction() == MotionEvent.ACTION_UP) {
                      path.lineTo(event.getX(), event.getY());
                      _graphics.add(path);
                  }
  
                  return true;
              }
          }
  
          @Override
          public void onDraw(Canvas canvas) {
              for (Path path : _graphics) {
                  // canvas.drawPoint(graphic.x, graphic.y, mPaint);
                  canvas.drawPath(path, mPaint);
              }
          }
  
          public void surfaceChanged(SurfaceHolder holder, int format, int width,
                  int height) {
              // TODO Auto-generated method stub
  
          }
  
          public void surfaceCreated(SurfaceHolder holder) {
              // TODO Auto-generated method stub
              _thread.setRunning(true);
              _thread.start();
          }
  
          public void surfaceDestroyed(SurfaceHolder holder) {
              // TODO Auto-generated method stub
              boolean retry = true;
              _thread.setRunning(false);
              while (retry) {
                  try {
                      _thread.join();
                      retry = false;
                  } catch (InterruptedException e) {
                      // we will try it again and again...
                  }
              }
          }
      }
  
      class DrawingThread extends Thread {
          private SurfaceHolder _surfaceHolder;
          private DrawingPanel _panel;
          private boolean _run = false;
  
          public DrawingThread(SurfaceHolder surfaceHolder, DrawingPanel panel) {
              _surfaceHolder = surfaceHolder;
              _panel = panel;
          }
  
          public void setRunning(boolean run) {
              _run = run;
          }
  
          public SurfaceHolder getSurfaceHolder() {
              return _surfaceHolder;
          }
  
          @Override
          public void run() {
              Canvas c;
              while (_run) {
                  c = null;
                  try {
                      c = _surfaceHolder.lockCanvas(null);
                      synchronized (_surfaceHolder) {
                          _panel.onDraw(c);
                      }
                  } finally {
                      // do this in a finally so that if an exception is thrown
                      // during the above, we don't leave the Surface in an
                      // inconsistent state
                      if (c != null) {
                          _surfaceHolder.unlockCanvasAndPost(c);
                      }
                  }
              }
          }
      }
  
      @Override
      public boolean onKeyDown(int keyCode, KeyEvent event) {
          if (keyCode == KeyEvent.KEYCODE_BACK) {
              startActivity(new Intent(WhiteBoard.this, HomeActivity.class));
              finish();
              return true;
          }
          return true;
      }
  
  }
  </code></pre>
  
  <p>and i want to clear the screen onClickListner. Any body any idea pls...</p>