Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5938441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:41:10+00:00 2026-05-22T15:41:10+00:00

how to clear the surfaceView in android? I have a class like this –

  • 0

how to clear the surfaceView in android?

I have a class like this –

class DrawingPanel extends SurfaceView implements SurfaceHolder.Callback {

can any one tell as to which method shud i call to clear my custom drawingPanel

This is my class –

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<Path> _graphics = new ArrayList<Path>();
    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;
    }

}

and i want to clear the screen onClickListner. Any body any idea pls…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T15:41:10+00:00Added an answer on May 22, 2026 at 3:41 pm

    You don’t clear it, you just avoid drawing anything to the canvas… just use an empty canvas (perhaps drawing a black rectangle or whatever you want as background)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any clear documentation on the binary formats used to serialize the various
Just to make this clear - what is the difference between: String(value) and value
Can anyone provide a clear explanation / example of what these functions do, and
Please clear my doubt about this, In SQL Server (2000 and above) is primary
Please clear this scenario. I created a viewController named RootViewController,so obiviously we'll get RootViewController.h
to clear: both my content i use this: CSS: .clr { clear: both; height:
I want to clear the selection of checkboxes if they click cancel. How can
I can clear all the layers using mapControl.ClearLayers(), but I only want to clear
Hoping to get some clear advice on this one. I want to push updates
I have a clear history button which clears the data in plist. Now, loading

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.