I want to do a simple drag and drop of an ImageView inside another ImageView or layout. Here is a picture of what I want to do

The only way I know this is to have 2 linear layouts, one βOTβ and βTOβ, and assign a background image of a gray letter to the second layout.
Here is my code
public class MainActivity extends Activity implements OnTouchListener,
OnDragListener {
private static final String LOGCAT = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.letter_a).setOnTouchListener(this);
findViewById(R.id.top_container).setOnDragListener(this);
findViewById(R.id.bottom_container).setOnDragListener(this);
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(null, shadowBuilder, view, 0);
view.setVisibility(View.INVISIBLE);
return true;
} else {
return false;
}
}
@Override
public boolean onDrag(View layoutview, DragEvent dragevent) {
int action = dragevent.getAction();
View view = (View) dragevent.getLocalState();
switch (action) {
case DragEvent.ACTION_DRAG_STARTED:
break;
case DragEvent.ACTION_DRAG_ENTERED:
break;
case DragEvent.ACTION_DRAG_EXITED:
break;
case DragEvent.ACTION_DROP:
ViewGroup owner = (ViewGroup) view.getParent();
owner.removeView(view);
LinearLayout container = (LinearLayout) layoutview;
container.addView(view);
view.setVisibility(View.VISIBLE);
break;
case DragEvent.ACTION_DRAG_ENDED:
Log.d(LOGCAT, "Drag ended");
if (dropEventNotHandled(dragevent)){
view.setVisibility(View.VISIBLE);
}
break;
default:
break;
}
return true;
}
private boolean dropEventNotHandled(DragEvent dragEvent) {
return !dragEvent.getResult();
}
}
I managed to get onTouch and onDrag to work and detect when you move, but I want to WRITE it in place so that it cannot be dragged back to its original place.
Many thanks!
source
share