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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:40:33+00:00 2026-05-23T13:40:33+00:00

I have written a code to move a button as it is dragged. I

  • 0

I have written a code to move a button as it is dragged. I am updating the buttons X and Y coordinates based on the current mouse coordinates, but when I am dragging the button the mouse coordinate values are toggling between low and high values, even when I am dragging very slowly.

When I am logging the coordinates the values show as :

(70, 24) - (15, 36) - (86, 51) - (20, 48) - (90, 54) - (32, 60) - (102, 66) ...

As you can see, they are toggling between high and low values, even when I am dragging very slowly in one direction. Can anyone tell me why?

Here is my code :

public class MyFrames extends Activity implements View.OnTouchListener {
public Button moveable;
// public TextView log;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //changer = (Button) findViewById(R.id.btn_changer);
    moveable = (Button) findViewById(R.id.btn_moveable);

    moveable.setOnTouchListener(this);
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
    switch(event.getAction() & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_MOVE:
        int x = (int)event.getX();
        int y = (int)event.getY();
        int w = moveable.getMeasuredWidth();
        int h = moveable.getMeasuredHeight();
        moveable.layout( x, y, x + w, y + h);
        Log.v("Moveable", "X = " + x + " Y = " + y + "\n");
        return true;
    }
    return false;
}

}
  • 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-23T13:40:34+00:00Added an answer on May 23, 2026 at 1:40 pm

    I haven’t tested it with your code, but I have had something similar happen to me in the past.

    The getX() and getY() functions return values that are relative to your Button. Since you are changing the button’s position in the onTouchListener, you also change the coordinate system that getX() and getY() are based on.

    Try using getRawX() and getRawY() instead. Those functions are independent of the Button’s position, and shouldn’t give you alternating values.

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

Sidebar

Related Questions

I have written code that automatically creates CSS sprites based on the IMG tags
I have written a piece of code to simulate mouse click which is working
I have written a code to move a file as follows private void Move_Click(object
Hi all i have written a code to move a file from the original
I have written code that opens 16 figures at once. Currently, they all open
I have written code in Java to access web cam,and to save image... I
I have written some code in my VB.NET application to send an HTML e-mail
I have written jQuery code, in files Main.html and ajax.php . The ajax.php file
I have written some code for playing a .wav through my application. Now I
I have written this code to join ArrayList elements: Can it be optimized more?

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.