title: change color of line in surfaceview and retain the color of the previous line
body: <p>I followed the tutorial from here <a href="https://github.com/vivekrk/SurfaceView-Sample" rel="nofollow">https://github.com/vivekrk/SurfaceView-Sample</a> and modified it to the code below.     </p>
  
  <p>Every time I press on the button to change the color The entire color including the the line which was already present on the view changes.I want only the new line to change its color .<br>
  How do I do that?</p>
  
  <p>I am a noob in Android and any help would be greatly appreciated.
  Thanks in Advance.</p>
  
  <p>CanvasView.java</p>
  
  <pre><code>import java.util.ArrayList;
  
  import android.content.Context;
  import android.graphics.Canvas;
  import android.graphics.Color;
  import android.graphics.Paint;
  import android.graphics.Path;
  import android.graphics.RectF;
  import android.view.MotionEvent;
  import android.view.SurfaceHolder;
  import android.view.SurfaceView;
  
  public class CanvasView extends SurfaceView implements SurfaceHolder.Callback {
  
      private static final float STROKE_WIDTH = 8;
  
      private UIThread uiThread;
  
      private Path mPath;
  
      public static Paint mPaint;
  
      private Path oldPath;
  
      private DrawableObject drawableObject = null;
  
      private float mX;
      private float mY;
      private float TOUCH_TOLERANCE = 8;
  
      private RectF dirtyRect;
  
      private int mMode = Constants.FREE_DRAWING;
  
      private ArrayList&lt;DrawableObject&gt; objectsToDraw;
  
  
      public static int colorofpaint;
  
  
  
      public CanvasView(Context context) {
          super(context);
          getHolder().addCallback(this);
  
          objectsToDraw = new ArrayList&lt;DrawableObject&gt;();
  
          mPath = new Path();
      }
  
      public void clearCanvas() {
          mPath.reset();
      }
  
      public void setDrawingMode(int drawingMode) {
          mMode = drawingMode;
      }
  
      @Override
      public void onDraw(Canvas canvas) {
          setPaintProperties();
  
          if (canvas != null) {
              canvas.drawColor(Color.WHITE);
              synchronized (objectsToDraw) {
                  for (DrawableObject drawableObject : objectsToDraw) {
                      drawableObject.draw(canvas);
                  }
              }
          }
      }
  
      public void stopUIThread() {
          uiThread.setRunning(false);
      }
  
      private void setPaintProperties() {
          mPaint = new Paint();
          mPaint.setAntiAlias(true);
          mPaint.setAntiAlias(true);
          mPaint.setDither(true);
          //mPaint.setColor(Color.RED);
          mPaint.setStyle(Paint.Style.STROKE);
          mPaint.setStrokeJoin(Paint.Join.ROUND);
          mPaint.setStrokeCap(Paint.Cap.ROUND);
          mPaint.setStrokeWidth(STROKE_WIDTH);
      }
  
      @Override
      public void surfaceChanged(SurfaceHolder holder, int format, int width,
              int height) {
          oldPath = mPath;
      }
  
      @Override
      public boolean onTouchEvent(MotionEvent event) {
          float x = event.getX();
          float y = event.getY();
  
          switch (mMode) {
          case Constants.FREE_DRAWING:
              drawableObject = new FreeDrawing(mPath, mPaint);
  
              if (colorofpaint==1) {
                  mPaint.setColor(Color.GREEN);
              }
  
              if (colorofpaint==2) {
                  mPaint.setColor(Color.RED);
              }
  
              if (colorofpaint==3) {
                  mPaint.setColor(Color.BLUE);
              }
  
              if (colorofpaint==4) {
                  mPaint.setColor(Color.YELLOW);
              }
  
              if (colorofpaint==5) {
                  mPaint.setColor(Color.CYAN);
              }
  
              if (colorofpaint==6) {
                  mPaint.setColor(Color.BLACK);
              }
  
              if (colorofpaint==7) {
                  mPaint.setColor(Color.MAGENTA);
              }
  
  
              break;
  
  
          default:
              break;
          }
  
          switch (event.getAction()) {
          case MotionEvent.ACTION_DOWN:
              synchronized (objectsToDraw) {
                  objectsToDraw.add(drawableObject);
              }
              mPath.moveTo(x, y);
              mX = x;
              mY = y;
              break;
  
          case MotionEvent.ACTION_MOVE:
              switch (mMode) {
              case Constants.FREE_DRAWING:
                  float dx = Math.abs(x - mX);
                  float dy = Math.abs(y - mY);
  
                  if (dx &gt;= TOUCH_TOLERANCE  || dy &gt;= TOUCH_TOLERANCE) {
                      mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
  
                      mX = x;
                      mY = y;
                  }
                  break;
              }
              break;
  
          case MotionEvent.ACTION_UP:
              switch (mMode) {
              case Constants.FREE_DRAWING:
                  mPath.moveTo(x, y);
                  break;
              }
              break;
          }
          return true;
      }
  
      @Override
      public void surfaceCreated(SurfaceHolder holder) {
          uiThread = new UIThread(this);
          uiThread.setRunning(true);
          uiThread.start();
      }
  
      public void restoreOldPath() {
          mPath = oldPath;
      }
  
      @Override
      public void surfaceDestroyed(SurfaceHolder holder) {
          uiThread.setRunning(false);
      }
  
      public RectF getDirtyRect() {
          if(dirtyRect == null) {
              dirtyRect = new RectF();
          }
          return dirtyRect ;
      }
  
  
  
  }
  </code></pre>
  
  <p>UIThread .java</p>
  
  <pre><code>import android.graphics.Canvas;
  import android.view.SurfaceHolder;
  
  public class UIThread extends Thread {
  
      private static boolean toRun = false;
      private CanvasView canvasView;
      private SurfaceHolder surfaceHolder;
  
      public UIThread(CanvasView canvasView) {
          this.canvasView = canvasView;
          surfaceHolder = canvasView.getHolder();
      }
  
      public boolean isThreadRunning() {
          return toRun;
      }
  
      public void setRunning(boolean run) {
          toRun = run;
      }
  
      @Override
      public void run() {
          Canvas c;
          while (toRun) {
                  c = null;
                  try {
                      c = surfaceHolder.lockCanvas(null);
                      canvasView.onDraw(c);
                  } finally {
                      if (c != null) {
                          surfaceHolder.unlockCanvasAndPost(c);
                      }
                  }
          }
      }
  
  }
  </code></pre>
  
  <p>FreeDrawing .java</p>
  
  <pre><code>import android.graphics.Canvas;
  import android.graphics.Paint;
  import android.graphics.Path;
  
  public class FreeDrawing extends DrawableObject {
  
      private Path mPath;
      private Paint mPaint;
  
      public FreeDrawing(Path path, Paint paint) {
          mPath = path;
          mPaint = paint;
      }
  
      @Override
      public void draw(Canvas canvas) {
          canvas.drawPath(mPath, mPaint);
      }
  }
  </code></pre>
  
  <p>DrawableObject .java</p>
  
  <pre><code>import android.graphics.Canvas;
  
  public abstract class DrawableObject {
  
      abstract public void draw(Canvas canvas);
  
  }
  </code></pre>
  
  <p>Constants .java</p>
  
  <pre><code>public class Constants {
  //  XXX:Drawing modes
      public static final int FREE_DRAWING = 1000;
      public static final int LINE_DRAWING = 1001;
      public static final int RECT_DRAWING = 1002;
      public static final int SELECT = 1003;
  }
  </code></pre>
  
  <p>SurfaceViewSampleActivity .java</p>
  
  <pre><code>import android.app.Activity;
  import android.content.res.Configuration;
  import android.graphics.Color;
  import android.graphics.LightingColorFilter;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.FrameLayout;
  import android.widget.RadioGroup;
  import android.widget.RadioGroup.OnCheckedChangeListener;
  import android.widget.Toast;
  
  public class SurfaceViewSampleActivity extends Activity  {
      /** Called when the activity is first created. */
      private CanvasView view;
      private static final int CLEAR = 100;
  
      private FrameLayout surfaceViewFrame;
  
      private RadioGroup mode;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
  
  
          surfaceViewFrame = (FrameLayout) findViewById(R.id.surfaceviewFrame);
          view = new CanvasView(this);
          surfaceViewFrame.addView(view, 0);
  
          CanvasView.colorofpaint=6;
  
  
          view.setDrawingMode(Constants.FREE_DRAWING);
  
  
          Button b1=(Button)findViewById(R.id.button1);
          b1.setOnClickListener(new OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  // TODO Auto-generated method stub
                  CanvasView.colorofpaint=2;
          //      view.setDrawingMode(Constants.FREE_DRAWING);
              }
          });
  
  
  
          Button b2=(Button)findViewById(R.id.button2);
          b2.setOnClickListener(new OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  // TODO Auto-generated method stub
                  CanvasView.colorofpaint=3;
  
                  view.setDrawingMode(Constants.FREE_DRAWING);
              }
          });
  
  
  
  
  
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          menu.add(0, CLEAR, 0, "Clear");
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          switch (item.getItemId()) {
          case CLEAR:
              view.clearCanvas();
              break;
          }
          return true;
      }
  
      @Override
      public void onConfigurationChanged(Configuration newConfig) {
          super.onConfigurationChanged(newConfig);
          Toast.makeText(getApplicationContext(), "onConfigurationChanged", Toast.LENGTH_SHORT).show();
      }
  
  
  
  }
  </code></pre>