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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:20:18+00:00 2026-05-27T13:20:18+00:00

i am making a time table app. i am now testing how does the

  • 0

i am making a time table app.
i am now testing how does the onDraw and onTouchEvent.

I Made a customeView and draw 8×10 lines.
i override onTouchEvent, and if touch is passing by the each rect, the rect is filled with green color.

if i drag from left to right, and from up to bottum, the drag is working right, and each rect is filled with color.
but when i drag back(from right to left, from bottom to up), the first line and last line doesn’t work!
even though I touch the position inside the rect, the rects are not filled with color..

i’m looking into my code and tried to fix really many times but cannot solve out the prob..

please see my code and help me~!

public class TimeTableActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
              OnDrawing drawing = new OnDrawing(this);  //customView
    setContentView(drawing);

}

class OnDrawing extends View{
//x,y = first touch position, endX, endY = last touch position
//startX, startY = calculate where to start for filling color.
// stopX, stopY = calculate where to finish for filling color.
    float x, y, endX, endY, startX, startY, stopX, stopY; 
    public OnDrawing(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //Paint Color 1, 2 ,3
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        Paint paint2 = new Paint();
        paint2.setColor(Color.YELLOW);
        Paint paint3 = new Paint();
        paint3.setColor(Color.GREEN);

        /***row 10lines***/
        for(int i=0; i<11; i++){
            canvas.drawLine(0, getHeight()/10*i, getRight(), getHeight()/10*i, paint);
        }
        /***colum 8lines***/
        for(int i=0; i<9; i++){
            canvas.drawLine(getWidth()/8*i, 0, getWidth()/8*i, getBottom(), paint);
        }
        /***first rows background color***/
        for(int i=0; i<10; i++){
            canvas.drawRect(0, getHeight()/10, getWidth()/8, getBottom(), paint2);
        }
        /***first column background color***/
        for(int i=0; i<8; i++){
            canvas.drawRect(getWidth()/8, 0, getRight(), getHeight()/10, paint2);
        }
        /***first cell color***/
        canvas.drawRect(0, 0, getWidth()/8, getHeight()/10, paint3);

        /***days in the first line***/
        canvas.drawText("Mon", getWidth()/8*1+10, getHeight()/10*1/2, paint);
        canvas.drawText("Tue", getWidth()/8*2+10, getHeight()/10*1/2, paint);
        canvas.drawText("Wed", getWidth()/8*3+10, getHeight()/10*1/2, paint);
        canvas.drawText("Thu", getWidth()/8*4+10, getHeight()/10*1/2, paint);
        canvas.drawText("Fri", getWidth()/8*5+10, getHeight()/10*1/2, paint);
        canvas.drawText("Sat", getWidth()/8*6+10, getHeight()/10*1/2, paint);
        canvas.drawText("Sun", getWidth()/8*7+10, getHeight()/10*1/2, paint);


        /***time in the first line***/
        canvas.drawText("icon", 10, getHeight()/10*1/2, paint);
        canvas.drawText("1", 10, getHeight()/10*1+getHeight()/10*1/2, paint);
        canvas.drawText("2", 10, getHeight()/10*2+getHeight()/10*1/2, paint);
        canvas.drawText("3", 10, getHeight()/10*3+getHeight()/10*1/2, paint);
        canvas.drawText("4", 10, getHeight()/10*4+getHeight()/10*1/2, paint);
        canvas.drawText("5", 10, getHeight()/10*5+getHeight()/10*1/2, paint);
        canvas.drawText("6", 10, getHeight()/10*6+getHeight()/10*1/2, paint);
        canvas.drawText("7", 10, getHeight()/10*7+getHeight()/10*1/2, paint);
        canvas.drawText("8", 10, getHeight()/10*8+getHeight()/10*1/2, paint);
        canvas.drawText("9", 10, getHeight()/10*9+getHeight()/10*1/2, paint);
        canvas.drawText("10", 10, getHeight()/10*10+getHeight()/10*1/2, paint);


        canvas.drawRect(startX, startY, stopX, stopY, paint3);//fill background color, from start position to stop position

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch(event.getAction()){
        case MotionEvent.ACTION_DOWN:
            x = event.getX();  //first touch position
            y = event.getY();

            break;

        case MotionEvent.ACTION_MOVE:
            endX = event.getX();  //last touch point
            endY = event.getY();


            if(endX>x && endY>y){  //If Dragging toward right bottom
            /***First Touch Position***/
            for(int i=0; i<8; i++){  
                if(x>getWidth()/8*i && x<getWidth()/8*(i+1)){
                    for(int j=0; j<10; j++){
                        if(y>getHeight()/10*j && y<getHeight()/10*(j+1)){
                            startX = getWidth()/8*i;  //startX = left side of the cell
                            startY = getHeight()/10*j;  //startY = upside of the cell

                        }
                    }
                }
            }//for

            /***Last Touch position***/

            for(int i=0; i<8; i++){  
                if(endX>getWidth()/8*i ){
                    for(int j=0; j<10; j++){
                        if(endY>getHeight()/10*j && endY<getHeight()/10*(j+1)){
                            stopX = getWidth()/8*(i+1);  //stopX = right side of the cell
                            stopY = getHeight()/10*(j+1);  //stopY = bottom side of the cell
                        }
                    }
                }
            }//for


            if(endX<x && endY<y){  //if dragging toward left top side.(backward) this part is trouble



                /***First Touch position***/
                for(int i=0; i<8; i++){  
                    if(x>getWidth()/8*i && x<getWidth()/8*(i+1)){
                        for(int j=0; j<10; j++){
                            if(y>getHeight()/10*j && y<getHeight()/10*(j+1)){
                                startX = getWidth()/8*(i+1);  //startX = right side of the cell
                                startY = getHeight()/10*(j+1);  //startY = down side of the cell

                            }
                        }
                    }
                }//for

                /***Last Touch position***/

                for(int i=0; i<8; i++){  
                    if(endX>getWidth()/8*i ){
                        for(int j=0; j<10; j++){
                            if(endY>getHeight()/10*j && endY<getHeight()/10*(j+1)){
                                stopX = getWidth()/8*(i);  //stopX = left side of the cell
                                stopY = getHeight()/10*(j);  //stopY = upside of the cell
                            }
                        }
                    }
                }//for


            }


            break;
        }

        invalidate();
        return true;
    }

}

}

enter image description here

  • 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-27T13:20:19+00:00Added an answer on May 27, 2026 at 1:20 pm

    First of all, dont create paint instances in onDraw function. Declare/Create them only once somewhere in constructor, this will improve your drawing performance. Secondly I couldn’t understand your code (i.e. Mathematics) completely but one solution for including drag functionality might be like this:

    Make a point variable, let say:

    Point drag_distance_Point = new Point();
    

    and on Action_Move:

    drag_distance_Point.set(start.x - end.x, start.y-end.y);
    

    now add this point to the coordinates of rectangle or anything you want to drag. Actually this will add/subtract the dragged coordinates from your currently drawn coordinates. By doing this your object will move/drag.

    Cheers

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

Sidebar

Related Questions

This is my first time making a hash table. I'm trying to associate strings
I'm making a sectioned table with fetched results, but am having a hard time
I'm having a hard time making the connection between testing and code. I could
I am making an iphone app, in which I wanna to implement the time
I started making a Rails 3.1 engine, and I'm having a hard time testing
I have tough time making this design decision. I could go with traditional new
Hello I am having a hard time making this UI element look the way
For some time I've been making a 2d tile based sim game, and it's
I am making a 3D cube using kit3D.this cube is generated at run time.I
I'm making Braid in Java. If you rewind the time, the sound plays backward.

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.