Android CustomView (extends from ImageView) fill with White

This Android App features a Signature Pad.

This is accomplished by a Custom View which extends from ImageView, and overriding theonTouchEvent() and onDraw() methods.

Now, if the user makes a mistake, we want to allow wiping the signature pad clean, and let the user start over. We are trying to accomplish this with an fillWhite() method.

How can we fill the ImageView with white?

public class SignatureView extends ImageView {
// setup initial color
private final int paintColor = Color.BLACK;
// defines paint and canvas
private Paint drawPaint = null;
// stores next circle
private Path path = new Path();

// set up default measurements of the signature pad
private final int PX_SIGNATURE_PAD_WIDTH = 521;
private final int PX_SIGNATURE_PAD_HEIGHT = 134;

private Canvas mCanvas = null;
public static Context stContext;
public static  AttributeSet stAttrs;

public int iScale = 0; // the scale factor from signature pad to Signature View

public SignatureView(Context context, AttributeSet attrs) {
    super(context, attrs);
    stContext = context;
    stAttrs = attrs;

protected void onDraw(Canvas canvas) {

    if (null == drawPaint)
        // Setup paint with color and stroke styles
        // Do this only the first time, when null == drawPaint
        drawPaint = new Paint();
        Log.w("SigView","Set strokewidth to: "+3*iScale+" pixels");
        drawPaint.setStrokeWidth(3 * iScale);

    canvas.drawPath(path, drawPaint);
    if (null == mCanvas) {
        mCanvas = canvas;

public boolean onTouchEvent(MotionEvent event) {
    float pointX = event.getX();
    float pointY = event.getY();
    // Checks for the event that occurs
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            path.moveTo(pointX, pointY);
            return true;
        case MotionEvent.ACTION_MOVE:
            path.lineTo(pointX, pointY);
            return false;
    // Force a view to draw again
    return true;
Note that the below implementation does not work

public void fillWhite() {
    setImageBitmap(Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888));
    // Force a view to draw again

protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {

    // test if we are allowed to change the size of the SignatureView:
    final int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
    final int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
    boolean bResizeWidth = widthSpecMode != MeasureSpec.EXACTLY;
    boolean bResizeHeight = heightSpecMode != MeasureSpec.EXACTLY;
    if (bResizeWidth && bResizeHeight)
        // Yes, we ARE allowed to change the size of the SignatureView:
        Dimensions d = fitSignaturePadIntoView(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension( d.width, d.height );
        // We are NOT allowed to change the size of the SignatureView:
        setMeasuredDimension( widthMeasureSpec, heightMeasureSpec );




Since you’ve set the View‘s background color to white, you just need to empty the Path and cause a redraw.


I would also suggest moving the Paint object initialization to the constructor. Even though the if will skip it after the first draw, you’re redrawing on every touch event, so it’s best to keep the onDraw() method as tight as possible.