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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:05:55+00:00 2026-05-31T07:05:55+00:00

I am working in android, I am using a button. Now I want to

  • 0

I am working in android, I am using a button. Now I want to perform drag and drop of this button.

This is my main.xml

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/ll_first"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
>

 <Button  
   android:id="@+id/btn"
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content" 
    android:text="drag me"
  />
 </LinearLayout>

And this is my code to move this button on mouse drag:-

public class DragdropActivity extends Activity implements OnTouchListener {

private final static int START_DRAGGING = 0;
private final static int STOP_DRAGGING = 1;

private Button btn;

private int status;

private ImageView image;

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

    btn = (Button) findViewById(R.id.btn);
    btn.setDrawingCacheEnabled(true);
    btn.setOnTouchListener(this);

}

@Override
public boolean onTouch(View view, MotionEvent me) {
    if (me.getAction() == MotionEvent.ACTION_DOWN) {
        status = START_DRAGGING;


    }
    if (me.getAction() == MotionEvent.ACTION_UP) {
        status = STOP_DRAGGING;
        Log.i("Drag", "Stopped Dragging");
    } else if (me.getAction() == MotionEvent.ACTION_MOVE) {
        if (status == START_DRAGGING) {
            System.out.println("Dragging");
            Log.v("***Drag and drop **",
                    "me.getRawX and Y = " + me.getRawX() + " "
                            + me.getRawY());
            Log.v("***Drag and drop **",
                    "image position = " + image.getLeft() + " "
                            + image.getRight());
     btn.setPadding((int) me.getRawX(), (int) me.getRawY(), 0,0); //this is not working fine. 


            btn.invalidate();
        }
    }
    return false;
}

}

I think btn.setPadding() is not working correctly, please suggest me what should I do so that the button can easily move to the position of mouse or gesture touch ?

  • 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-31T07:05:56+00:00Added an answer on May 31, 2026 at 7:05 am

    I resolved my problem like this.

        @Override
         public boolean onTouch(View view, MotionEvent me) {
        if (me.getAction() == MotionEvent.ACTION_DOWN) {
            status = START_DRAGGING;
    
    
        }
        if (me.getAction() == MotionEvent.ACTION_UP) {
            status = STOP_DRAGGING;
            Log.i("Drag", "Stopped Dragging");
        } else if (me.getAction() == MotionEvent.ACTION_MOVE) {
            if (status == START_DRAGGING) {
                System.out.println("Dragging");
    
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                        50, 50);
                layoutParams.setMargins((int) me.getRawX() - 25,
                        (int) me.getRawY() - 50, 0, 0);
                layout.removeView(btn);
                layout.addView(btn, layoutParams);
    
                btn.invalidate();
            }
        }
        return false;
    }
    

    Now its working fine.

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

Sidebar

Related Questions

I am now working on changing android theme style at runtime, when the user
I am now working on a program for Android which is something related to
I'm currently working on an application in Android. As a start, I want to
I want to integrate facebook(like),twitter,google plus(bubble button) into my android app, which is developed
I am working on an Android code that on click of a button inserts
Here i make 5 Tab buttons that are working proper but now i want
I'm working on an Android widget which essentially places a button on the homescreen.
I made the android Bluetooth-Chat tutorial. Instead of using the OnClickListeners in the main
I'm new to android developing but right now I'm working on an application that
Now I am working on a Android application. I created a custom keyboard with

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.