I have multiple views being dragged around, each representing an emotion. the idea is to have each view send an entry to a database that varies based on what view is dragged.
the views are created here:
_root = (ViewGroup)findViewById(R.id.root);
_view = new ImageView(this);
_view.setImageResource(R.drawable.smile);
_view2 = new ImageView(this);
_view2.setImageResource(R.drawable.frown);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(150, 50);
layoutParams.leftMargin = 250;
layoutParams.topMargin = 250;
layoutParams.bottomMargin = -250;
layoutParams.rightMargin = -250;
_view.setLayoutParams(layoutParams);
_view.setOnTouchListener(this);
RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(150, 50);
layoutParams2.leftMargin = 250;
layoutParams2.topMargin = 500;
layoutParams2.bottomMargin = -250;
layoutParams2.rightMargin = -250;
_view2.setLayoutParams(layoutParams2);
_view2.setOnTouchListener(this);
_root.addView(_view);
_root.addView(_view2);
}
And the movement and drag is defined here:
public boolean onTouch(View view, MotionEvent event) {
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
_xDelta = X - lParams.leftMargin;
_yDelta = Y - lParams.topMargin;
break;
case MotionEvent.ACTION_UP:
Intent intent = new Intent(this, Exit_Activity.class);
startActivity(intent);
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
case MotionEvent.ACTION_MOVE:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
layoutParams.leftMargin = X - _xDelta;
layoutParams.topMargin = Y - _yDelta;
layoutParams.rightMargin = -250;
layoutParams.bottomMargin = -250;
view.setLayoutParams(layoutParams);
break;
}
_root.invalidate();
return true;
}
}
As you can see, ACTION_UP currently sends the user to the next activity, regardless of which view is dragged. What’s the best way to have the app differentiate between views?
Set ID for the individual view. Retrieve the ID when the view is clicked. ID is unique identity for each view. To set the id:
When clicked, retrieve the id: