Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: Dragging Multiple Views
    primarykey
    data
    text
    <p>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. </p> <p>Here is my code. </p> <p>MainActivity.java</p> <pre><code>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; }} </code></pre> <p>DragSource.java</p> <pre><code>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(); }} </code></pre> <p>my activity_main.xml </p> <pre><code> &lt;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" &gt;&lt;com.example.trialsdrag.DragSource android:layout_width="wrap_content" android:layout_height="wrap_content"/&gt;&lt;com.example.trialsdrag.DragSource android:layout_width="wrap_content" android:layout_height="wrap_content"/&gt;&lt;/RelativeLayout&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload