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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:41:47+00:00 2026-06-13T06:41:47+00:00

I have this class: My problem: public class Example extends Activity implements OnClickListener{ DrawView

  • 0

I have this class:
My problem:

public class Example extends Activity implements OnClickListener{
    DrawView view1;
    Canvas canvas = new Canvas();

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);   
        view1 = new DrawView(this);
        setContentView(view1);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
    int x = (int) event.getX();
    int y = (int) event.getY();

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            view1.setFirstCoord(x, y);
            break;
        case MotionEvent.ACTION_MOVE:
            break;
        case MotionEvent.ACTION_UP:
            view1.setSecondCoord(x, y);
            view1.dispatchDraw(canvas);
            break;
        }
    return true;
    }

    public class DrawView extends LinearLayout {
        private Paint paint = new Paint();
        public int x1, x2;
        public int y1, y2;

        public DrawView(Context c){
            super(c);           
            LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            addView(inflater.inflate(R.layout.union, null));

            x1 = 0;
            x2 = 0;
            y1 = 0;
            y2 = 0;

            paint.setColor(Color.BLACK);
            paint.setAntiAlias(true);
            paint.setDither(true);
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeJoin(Paint.Join.ROUND);
            paint.setStrokeWidth(10);        
        }

        @Override
        public void dispatchDraw(Canvas canvas) {
            super.dispatchDraw(canvas);
            canvas.drawLine(0, 50, 900,2000 , paint);//WORKS
            canvas.drawLine((int) x1, (int) y1,(int) x2,(int) y2, paint);//NO WORKS
        }

        public void setFirstCoord(int e, int f){
            x1 = e;
            y1 = f;
        }

        public void setSecondCoord(int e, int f){
            x2 = e;
            y2 = f;
        }
    }
}

When the next line is executed I see the “example” line on the screem:

canvas.drawLine(0, 50, 900,2000 , paint);

But when the following line “is executed” is not painted any straight line. Why???

canvas.drawLine((int) x1, (int) y1,(int) x2,(int) y2, paint);

I also tried with:

canvas.drawLine(x1, y1, x2, y2, paint);

But, obviously, the results are the same.

I also tried with executed a onDraw() method but any line is painted because onDraw paint under de “UI” (under the content of the layout .xml file)

I hope found somebody who can help me. I think the solution can very easy but I’m going crazy trying things without finding the solution.

Thanks!!!

The solution:

public class Example extends Activity implements OnClickListener{
    DrawView view1;
    Canvas canvas = new Canvas();

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);   
        view1 = new DrawView(this);
        setContentView(view1);
        view1.setOnClickListener(this);
    }

    public class DrawView extends LinearLayout {
        private Paint paint = new Paint();
        public int x1, x2;
        public int y1, y2;

        public DrawView(Context c){
            super(c);           
            LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            addView(inflater.inflate(R.layout.union, null));

            x1 = 0;
            x2 = 0;
            y1 = 0;
            y2 = 0;

            paint.setColor(Color.BLACK);
            paint.setAntiAlias(true);
            paint.setDither(true);
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeJoin(Paint.Join.ROUND);
            paint.setStrokeWidth(10);        
        }

        public boolean onTouchEvent(MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            view1.setFirstCoord(x, y);
            break;
        case MotionEvent.ACTION_MOVE:
            break;
        case MotionEvent.ACTION_UP:
            view1.setSecondCoord(x, y);
            view1.onDraw(canvas);
            view1.postInvalidate();
            break;
        }
        return true;
        }

        public void setFirstCoord(int e, int f){
            x1 = e;
            y1 = f;
        }

        public void setSecondCoord(int e, int f){
            x2 = e;
            y2 = f;
        }

        public void onDraw(Canvas canvas){
        canvas.drawLine((int) x1, (int) y1,(int) x2,(int) y2, paint);
    }
    }
}

THANKS AGAIN for your help and your attention. It’s a pleasure!!!

  • 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-06-13T06:41:48+00:00Added an answer on June 13, 2026 at 6:41 am

    Instead of putting your drawing code in dispatch draw, try putting it in onDraw.

    Then modify your onTouch method to look like this:

    @Override
    public boolean onTouchEvent(MotionEvent event) {
      int x = (int) event.getX();
      int y = (int) event.getY();
      switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
        view1.setFirstCoord(x, y);
        break;
        case MotionEvent.ACTION_MOVE:
        break;
        case MotionEvent.ACTION_UP:
        view1.setSecondCoord(x, y);
        view1.postInvalidate();
        break;
      }
       return true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i've a problem with my tabularinline field. I have model like this class Product(models.Model):
I have a problem, I need to change body of method when this class
This is my problem: I have a player class and SwipeDetector Class in c#,
Ok guys, I have a serious problem with this. I have a static class
I have this class which i would like to map: public class Contract {
i have this class public class Image { public string url { get; set;
I had this problem Launching a new activity - android and don't understand why
I have two classes, this one is XMLParser.java extends the activity and execute the
Can't start new activity. Got problem in onClickListener. I post a code an errors.
I have a problem with following simple code. I create GraphicView class which extends

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.