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

  • Home
  • SEARCH
  • 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 7989901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:52:11+00:00 2026-06-04T12:52:11+00:00

I am trying to draw a line using on touch method. I want that

  • 0

I am trying to draw a line using on touch method.

I want that when user touches a particular point and then drags the finger, the line should be drawn.

using canvas.drawLine(_,_,_,_,_ ); is not drawing lines. as onDraw works only once.

my main question is “how to draw a line during runtime without using onDraw()? “

edit :

public class DrawView  extends View {
    int radius=30;
    int initialX =0;
    int initialY=0;
    int finalX=0;
    int finalY=0;
    private Point currentPoint;
    private int index;
    static ArrayList<Point> pointListDrawView = new ArrayList<Point>();

    public DrawView(Context context, ArrayList<Point> pointList) {
        super(context);
        // TODO Auto-generated constructor stub
        this.setBackgroundColor(Color.WHITE);
        //pointListDrawView = pointList;


        pointListDrawView.add(pt(600,200));

        pointListDrawView.add(pt(500,200));
        pointListDrawView.add(pt(400,200));
        pointListDrawView.add(pt(400,300));

        pointListDrawView.add(pt(400,400));
        pointListDrawView.add(pt(500,400));
        pointListDrawView.add(pt(400,400));

        pointListDrawView.add(pt(400,500));
        pointListDrawView.add(pt(400,600));
        pointListDrawView.add(pt(500,600));
        pointListDrawView.add(pt(600,600));

    }
    private Point pt(int i, int j) {
        // TODO Auto-generated method stub
        Point P = new Point(i, j);
        return P ;
    }
    Paint paint= new Paint();
    Canvas universalCanvas;
    @Override 
    protected void onDraw(final Canvas canvas) {

            paint.setColor(Color.GREEN);
            paint.setStrokeWidth(10);
            for(int i=0; i<pointListDrawView.size();i++){
            canvas.drawCircle(pointListDrawView.get(i).x, pointListDrawView.get(i).y, 15, paint);
            }
            universalCanvas = canvas;
    //      drawLines(canvas);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub

        int i =0;

        switch (event.getAction()){

        case MotionEvent.ACTION_DOWN:
                if(((event.getX()>pointListDrawView.get(i).x-radius) && (event.getX()<pointListDrawView.get(i).x+radius) && (event.getY()>pointListDrawView.get(i).y-radius) && (event.getY()<pointListDrawView.get(i).y+radius)))

                {
                    Log.d("touch", "touched correct point");
                    initialX=pointListDrawView.get(i).x;
                    initialY=pointListDrawView.get(i).y;

                } 

                else{
                    Log.d("touch", "touch discarded");
                return false;
                }
        break;

        case MotionEvent.ACTION_MOVE:
                universalCanvas.drawLine(initialX, initialY, event.getX(), event.getY(), paint);

                break;

        case MotionEvent.ACTION_UP:
                if(((event.getX()>pointListDrawView.get(i+1).x-radius) && (event.getX()<pointListDrawView.get(i+1).x+radius) && (event.getY()>pointListDrawView.get(i+1).y-radius) && (event.getY()<pointListDrawView.get(i+1).y+radius)))
                {
                    finalX=pointListDrawView.get(i+1).x;
                    finalY=pointListDrawView.get(i+1).y;
                    universalCanvas.drawLine(initialX,initialY, finalX,finalY, paint);
                    Log.d("touch", "line drawn");
                    return true;
                }
                else
                {
                    Log.d("touch", "correct point touched and discarded");
                    return false;
                }


        default:
            break;

        }
        invalidate();
        return true;

    }

}
  • 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-04T12:52:12+00:00Added an answer on June 4, 2026 at 12:52 pm

    I just write this code for you. Check if it helps.

    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.Paint.Style;
    import android.graphics.PointF;
    import android.view.MotionEvent;
    import android.view.View;
    
    public class TestView extends View
    {
    
        private Paint paint;
        private PointF startPoint, endPoint;
        private boolean isDrawing;
    
        public TestView(Context context)
        {
            super(context);
            init();
        }
    
        private void init()
        {
            paint = new Paint();
            paint.setColor(Color.RED);
            paint.setStyle(Style.STROKE);
            paint.setStrokeWidth(2);
            paint.setAntiAlias(true);
        }
    
        @Override
        protected void onDraw(Canvas canvas)
        {
            if(isDrawing)
            {
                canvas.drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y, paint);
            }
        }
    
    
        @Override
        public boolean onTouchEvent(MotionEvent event)
        {
            switch (event.getAction())
            {
                case MotionEvent.ACTION_DOWN:
                    startPoint = new PointF(event.getX(), event.getY());
                    endPoint = new PointF();
                    isDrawing = true;
                    break;
                case MotionEvent.ACTION_MOVE:
                    if(isDrawing)
                    {
                        endPoint.x = event.getX();
                        endPoint.y = event.getY();
                        invalidate();
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    if(isDrawing)
                    {
                        endPoint.x = event.getX();
                        endPoint.y = event.getY();
                        isDrawing = false;
                        invalidate();
                    }
                    break;
                default:
                    break;
            }
            return true;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to draw text using Core Text functions, with a line spacing that's
I'm trying to draw a single line using OnMouseMove() event. My Problem is that
I am trying to draw some basic graphics primitives(line, rectangle etc.) using GDI+ apis
I am trying to write some code that that will draw the line which
Hi I am trying to draw one line graph and for that I am
i'm trying to draw a line from the originate point of an image view
I'm trying to make a program that will draw lines over a picturebox using
I'm trying to draw a line using ctx.lineTo in loop. I have small function
I'm trying to manually draw a line in WPF by overriding the OnRender method
I'm trying to draw a line between two cities, on a world map, using

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.