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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:34:12+00:00 2026-05-20T17:34:12+00:00

I currently have something working where I can drag a box horizontally on the

  • 0

I currently have something working where I can drag a box horizontally on the screen (what I want it to do). However, it works when you click ANYWHERE on the screen, and I want it to work only when the box has been clicked. I have tried implementing this in different ways, and I’ve looked all over the place and still remain lost. Can anybody help? I also can’t figure out how a bitmap is placed (I’m using a bitmap right now as I can’t for the life of me figure out how to implement the ImageView inside my SurfaceView). If I say my bitmap is placed at 0,0 will that place the bitmap according to its top left corner at 0,0? I also had an algorithm for stopping the box when it reached an edge, but I’ll just have to rewrite that as I must have deleted it. Please if you can offer your knowledge I would GREATLY appreciate it

public class BoardView extends SurfaceView implements SurfaceHolder.Callback{
    Context mContext;

    private BoardThread thread;
    private float box_x = 140;
    private float box_y = 378;
    ImageView i = (ImageView) findViewById(R.id.box_view);

    Bitmap box = 
        (BitmapFactory.decodeResource
                (getResources(), R.drawable.box));
    private float boxWidth = box.getWidth();
    private float boxHeight = box.getHeight();



    public BoardView(Context context){
        super(context);
        //surfaceHolder provides canvas that we draw on
        getHolder().addCallback(this);
        // controls drawings
        thread = new BoardThread(getHolder(),this);
        //intercepts touch events
        setFocusable(true);

    }


    @Override

    public void onDraw(Canvas canvas){
        canvas.drawColor(Color.WHITE);  


        //draw box and set start location
        canvas.drawBitmap(box, box_x - (boxWidth/2), 
                box_y - (boxHeight/2), null);






    }

    @Override
    public boolean onTouchEvent(MotionEvent event){

        //boolean mode = false;

        if(event.getAction() == MotionEvent.ACTION_DOWN){
            //int x = (int)event.getX();
            //int y = (int)event.getY();

            //if (x > box_x && x < box_x + 29 && y > box_y && y < box_y + 30){
                //mode = true;
                box_x = (int)event.getX();
            //}
        }

        if(event.getAction() == MotionEvent.ACTION_MOVE) {
            //int x = (int)event.getX();
            //int y = (int)event.getY();

            //if (mode == true){
                box_x = (int)event.getX();
            //}
        }

        invalidate();

        return true;
    }


    @Override
    public void surfaceChanged(SurfaceHolder holder, 
            int format, int width, int height ){

    }


    @Override
    public void surfaceCreated(SurfaceHolder holder){
        thread.startRunning(true);
        thread.start();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder){
        thread.startRunning(false);
        thread.stop();
    }



}
  • 1 1 Answer
  • 1 View
  • 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-20T17:34:12+00:00Added an answer on May 20, 2026 at 5:34 pm

    Simply check the touch event x and y coordinate, and check if this x,y-combination is within the box’s bounds. I can see you are on the right path by looking at your commented out code.

    Do something like this:

    RectF rect = new RectF(x,y, x + box.getWidth(), y+box.geHeight());
    
    if(rect.contains(touchX, touchY)) {
        // You hit the box, allow dragging...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently working on a project at work using the HTML5 Drag and Drop
I have something like this currently: <div class = className style = position: absolute;
Currently in my code I have something in a for loop similar to: bstr
I currently have a table structure that looks something like this(some details omitted): ColumnName
I currently have service classes that look something like this public class UserService :
We currently have an ant task that contains something similar to the following: <filelist
I currently have some action associated with when the user types something in a
Currently, I have something like: public partial class Form1 : Form { delegate void
Currently I have a site where it has a header that looks something like
I'm currently learning SML and I have a question about something I have no

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.