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 7093521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:26:59+00:00 2026-05-28T08:26:59+00:00

I have a custom view called DrawView created in the main activity. I have

  • 0

I have a custom view called DrawView created in the main activity. I have implemented the onDraw() method in the DrawView class and it initially draws a circle. I have then added a touch listener, so that when a user clicks, it then draws a square. I am up to the part where the user clicks and a square is drawn. I’m not to sure how to go about this.

public class TestActivity extends Activity {
    DrawView drawing;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ViewGroup myLayout = (ViewGroup) findViewById(R.id.mainLayout);

        drawing = new DrawView(this);
        myLayout.addView(drawing);  

        drawing.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    // draw a square
                }
                return true;
            }
        });
    }

    private class DrawView extends View {

        public DrawView(Context context) {
            super(context);
        }

        protected void onDraw(Canvas canvas) {
            Paint myPaint = new Paint();
            myPaint.setColor(Color.BLACK);
            // draw a circle
        }
    }
}

Help would be much appreciated.

  • 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-28T08:27:00+00:00Added an answer on May 28, 2026 at 8:27 am

    here is the simple snippet for drawing the rectangle when user down,move and up of touch listener fire, just override in DrawView class not by setOnTouchListener()

    define the Rect r = new Rect() in DrawView class then after implement this code in DrawView class

    public boolean onTouch(View v, MotionEvent event) {
       if (event.getAction() == MotionEvent.ACTION_DOWN) {
          sx = event.getX();
          sy = event.getY();
          r.set(sx,sy,sx,sy);
       }else if(event.getAction==MotionEvent.ACTION_UP){
          r.set(sx,sy,event.getX(),event.getY());
       }else if(event.getAction==MotionEvent.ACTION_MOVE){
          r.set(sx,sy,event.getX(),event.getY());
       }
       invalidate();
       return true;
    

    }

    here is the onDraw()

    public void onDraw(Canvas canvas){
        super.onDraw(canvas);
        canvas.drawRect(r, new Paint());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a custom view by extending AutoCompleteTextView (with a specialized function called
I have a class that subclasses UIView called DrawView. This class contains custom drawing
I have a custom view which I override the onDraw method to draw bitmaps
I have a custom-made view that extends the View class. I would like 2
I have created a custom tree view control in .NET to display directories. What
Setup: I have a custom class I've created that handles data retrieved from a
I have a custom view being dynamically inserted and the onDraw is not being
I have a custom view in my app that I draw using the onDraw()
In my program I have a custom view object. Inside the view class is
I have created a Custom View component and my layout file consists of a

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.