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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:24:23+00:00 2026-06-13T01:24:23+00:00

I have a problem on dragging multiple Views in android. I have a two

  • 0

I have a problem on dragging multiple Views in android. I have a two circle created using a Canvas. The problem is that I can only drag one Circle and I can’t drag the other Circle. It seems that the first Circle covers the entire screen, and when I try to drag the 2nd Circle still the 1st Circle is moving.

Here is my code.

MainActivity.java

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}}

DragSource.java

public class DragSource extends View {

private Paint viewPaint;
private float startX;
private float startY;
private float touchOffsetX;
private float touchOffsetY;
private float x = 30;
private float y = 30;
private static final float RADIUS = 30;

//needed for finding drop target:

//the constructor:
public DragSource(Context context, AttributeSet attrs) {
    super(context, attrs);

    viewPaint = new Paint();
    viewPaint.setColor(Color.RED);
    viewPaint.setAntiAlias(true);
}

public boolean onTouchEvent(MotionEvent mEvent) {

    int eventAction = mEvent.getAction();
    switch(eventAction) 
    {
    case MotionEvent.ACTION_DOWN:
        startX = x;
        startY = y;
        touchOffsetX = mEvent.getX();
        touchOffsetY = mEvent.getY();
        break;
    case MotionEvent.ACTION_UP:

        break;
    case MotionEvent.ACTION_MOVE:   
    case MotionEvent.ACTION_CANCEL:
        x = startX + mEvent.getX() - touchOffsetX;
        y = startY + mEvent.getY() - touchOffsetY;
        break;
    }
    return true;
}

public void draw(Canvas c) {
    int w = c.getWidth();
    int h = c.getHeight();


    c.drawCircle(x, y, RADIUS, viewPaint);
    this.invalidate();
}}

my activity_main.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" ><com.example.trialsdrag.DragSource
android:layout_width="wrap_content"
android:layout_height="wrap_content"/><com.example.trialsdrag.DragSource
android:layout_width="wrap_content"
android:layout_height="wrap_content"/></RelativeLayout>
  • 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-13T01:24:24+00:00Added an answer on June 13, 2026 at 1:24 am

    What’s happening is your views’ size is the actually the whole screen size, and in onTouchEvent, your simply changing the drawing on them (moving the circle).

    What you need to do is set android:layout_height and android:layout_width for the DragSource in your main_activity.xml, and then change the layout margins dynamically in onTouchEvent using:

    RelativeLayout.LayoutParams params = (LayoutParams) getLayoutParams();
    params.setMargins(x+params.leftMargin, y+params.topMargin, 0, 0);
    setLayoutParams(params);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two Lists that both are drag-drop enabled. I also draw a chart
I have multiple element that are jquery ui draggable, these elements can also be
I have problem with my query on C, I’m using the oci8 driver. This
i have a rather weird problem that should fix a bug in my current
I have a problem that I implemented a functionality in which we draw bitmaps
I have a drag script where i'm dragging div.slider, i'm keeping track of the
I'm developing an iPhone/iPad app that supports dragging items between table views. Since all
I have a problem with dragging object in iPhone sdk , my UIImageView outlet
I have a problem with a dragging and resizable box. It works fine, drags
I have a problem with jQuery Drag&Drop. I have a draggable element and lots

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.