How to clear a Canvas in Android

In this android programming source code example, we are going to clear a Canvas in Android.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to clear a Canvas in Android.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/canvas"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/clear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:textColor="#ffffff"
        android:layout_alignParentBottom="true"
        android:text="Clear Canvas"/>

</RelativeLayout>

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;


public class CanvasActivity1 extends AppCompatActivity {
    private RelativeLayout view;
    private Button clearbtn;
    private canvas1 customcanvas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_canvas1);
        view = (RelativeLayout) findViewById(R.id.canvas);
        clearbtn = (Button) findViewById(R.id.clear);
        customcanvas = new canvas1(CanvasActivity1.this, null);
        view.addView(customcanvas);
        clearbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                customcanvas.clearCanvas();
            }
        });
    }

    public static class canvas1 extends View {
        private Paint paint;
        private Canvas mcanvas;
        private boolean cc = false;

        public canvas1(Context context, AttributeSet attributeSet){
            super(context, attributeSet);
            paint = new Paint();
            paint.setColor(getResources().getColor(R.color.colorAccent));
            paint.setStrokeWidth(10);
            paint.setStyle(Paint.Style.STROKE);
        }

        @Override
        protected void onDraw(Canvas canvas){
            super.onDraw(canvas);
            mcanvas = canvas;
            if(cc){
                mcanvas.drawColor(Color.WHITE);
            }else{
                mcanvas.drawRect(50,50,400,250, paint);
                cc = false;
            }
        }

        public void clearCanvas(){
            cc = true;
            invalidate();
        }
    }
}

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

Add a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.