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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:29:43+00:00 2026-05-28T01:29:43+00:00

My onTouchevent recognizes correct which drawable shape i have clicked. And i can set

  • 0

My onTouchevent recognizes correct which drawable shape i have clicked.
And i can set the x and y parameters for it too.
But at the end of the onTouchEvent i call invalidate() but the onDraw method is never called.

Why is this!?

public class DrawView extends RelativeLayout {
    private ArrayList<Rectangle> rectangles = new ArrayList<Rectangle>();
    private int rectId = 0;
    private int width;
    private int height;

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

        this.setLayoutParams(new RelativeLayout.LayoutParams(width, height));       

        Rectangle rect1 = new Rectangle(this.getContext());
        rect1.setId(1);
        rectangles.add(rect1);
        this.addView(rect1);

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 10, 0, 0);
        Rectangle rect2 = new Rectangle(this.getContext());
        rect2.setId(2);
        params.addRule(RelativeLayout.BELOW, rect1.getId());    
        rect2.setLayoutParams(params);  
        rectangles.add(rect2);
        this.addView(rect2);


    }

    @Override
    protected void onDraw(Canvas canvas) {
        Log.i("ondraw","yes"+rectangles.size());

        for(Rectangle v: rectangles){
            Paint paint = new Paint();
            paint.setColor(Color.GREEN);
            Log.i("draw", ""+v.getTop());
            Log.i("draw", ""+v.getLeft());
            canvas.drawRect((float)v.getX(),(float) v.getY(),(float) v.getX()+150, (float)v.getY()+50, paint);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        int x = (int) event.getX();
        int y = (int) event.getY(); 

        switch(event.getAction()){
            case MotionEvent.ACTION_DOWN:

                rectId = 0;

                for(int i=0; i < rectangles.size(); i++){
                    View child = rectangles.get(i);
                    if(x > child.getLeft() && x < child.getLeft()+150 && y > child.getTop() && y < child.getTop()+50){
                        rectId = child.getId();
                        rectangles.get(rectId-1).setTop(child.getTop());
                        rectangles.get(rectId-1).setLeft(child.getLeft());
                        break;
                    }               
                }
                break;
            case MotionEvent.ACTION_MOVE:

                if(rectId > 0){
                    rectangles.get(rectId-1).setBackgroundRessource(true);
                    rectangles.get(rectId-1).setTop(y-25);
                    rectangles.get(rectId-1).setLeft(x-25);
                    Log.i("MOVE X", rectId+" "+rectangles.get(rectId-1).getX());
                    Log.i("MOVE Y", rectId+" "+rectangles.get(rectId-1).getY());
                }

                break;
            case MotionEvent.ACTION_UP:
                Log.i("rectid Cancel", ""+rectId);
                if(rectId > 0){
                    rectangles.get(rectId-1).setBackgroundRessource(true);
                }

                break;
        }

        invalidate();
        return true;

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        width = View.MeasureSpec.getSize(widthMeasureSpec);
        height = View.MeasureSpec.getSize(heightMeasureSpec);

        setMeasuredDimension(width, height);
    }

}
  • 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-28T01:29:44+00:00Added an answer on May 28, 2026 at 1:29 am

    I thik you need to add: this.setWillNotDraw(false); in DrawView constructor

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

Sidebar

Related Questions

I can override onTouchEvent, but there is no x and y coordinates to set
I have a View in Android that has an onTouchEvent which stores the x
I extend my class from View. and I override onTouchEvent function. But, I can
I have a custom Android view which overrides onTouchEvent(MotionEvent) to handle horizontal scrolling of
So, I am trying to check multiple screen touches with an onTouchEvent, but it
I have a Canvas on which I am drawing lines bewteen points in a
I'm using: @Override public boolean onTouchEvent(MotionEvent event) { I'm using surfaceview. But when I
I have a: @Override public boolean onTouchEvent(MotionEvent event) { synchronized (event) { try {
When you have a surfaceView class, used for animation/game/... you have the onTouchEvent() method
Why OnTouchEvent in Mono for Android doesn't have the View argument OnTouch has in

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.