Q:

Touch draw android paint color

I am using this piece of code for user to draw o line with a finger:
public class DrawingView extends View {

private Paint paint;
private Path path;

public DrawingView(Context context , AttributeSet attrs) {
    super(context, attrs);

    this.paint = new Paint();
    this.paint.setAntiAlias(true);
    this.paint.setColor(Color.BLACK);
    this.paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeJoin(Paint.Join.ROUND);
    paint.setStrokeWidth(5f);

    this.path = new Path();
}

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawPath(path, paint);
}

@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:
        case MotionEvent.ACTION_UP:
            path.lineTo(eventX, eventY);
            break;
        default:
            return false;
    }

    // Schedules a repaint.
    invalidate();
    return true;
}

public void clear() {
    path.reset();
    invalidate();
}

public void setPaintColor(int color) {
    paint.setColor(color);
}

public int getCurrentPaintColor() {
    return paint.getColor();
}
}

With method setPaintColor() I am changing the color of the paint. But when I change the color, the whole drawing gets changed (even the lines that I drew before). How to change the color of the paint and leave previous drawings untached? I tried to create new Path, but then the previous drawing disappears.

A:

You need to make a small datastructure for this, which will store both color and path of the drawing.. here is an example: 
class PaintClass
{
    Path path;
    int Color;
    public int getColor() { return color; }
    public void setColor(int color){this.Color = color;}
    public int getPath() { return path; }
    public void setPath(Path path){this.path = path;}

    ...
    ...
    ...   
}

Now maintain an arraylist having objects of PaintClass. 
Implement it like this in onDraw method
{    
    for(PaintClass item : yourArrayListOfPaintClassObjects)
    {
        //set Paint color like this
        setPaintColor(item.getPaintColor());
        canvas.drawPath(.....,paint);
    }
}

Note: On each new drawing add newely made PaintClass object in arraylist...

