I am working on a photo editor application, in which i am trying to drag and drop an edittext, in which user can enter any string.I have applied touch listener to drag the view, it runs perfectly for the first time but when i drag and drop the edittext second time it gets invisible when the user drops the view.
Here is my code snippet :
This is my editText
<RelativeLayout
android:layout_below="@+id/top"
android:id="@+id/middle1"
android:layout_width="wrap_content"
android:layout_height="420dp"
>
<EditText
android:id="@+id/screen"
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:background="@drawable/screen" />
</RelativeLayout>
And this is the java code
private final class MyTouchListener implements OnTouchListener {
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
ClipData data = ClipData.newPlainText("", "");
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(data, shadowBuilder, view, 0);
view.setVisibility(View.INVISIBLE);
return true;
} else {
return false;
}
}
}
class MyDragListener implements OnDragListener {
@Override
public boolean onDrag(View v, DragEvent event) {
int action = event.getAction();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
// do nothing
break;
case DragEvent.ACTION_DRAG_ENTERED:
break;
case DragEvent.ACTION_DRAG_EXITED:
break;
case DragEvent.ACTION_DROP:
// Dropped, reassign View to ViewGroup
if( v instanceof ViewGroup && ((ViewGroup)v).getChildCount()!=0 ){
View view = (View) event.getLocalState();
ViewGroup owner = (ViewGroup) view.getParent();
owner.removeView(view);
RelativeLayout container = (RelativeLayout) v;
container.addView(view);
view.setVisibility(View.VISIBLE);}
break;
case DragEvent.ACTION_DRAG_ENDED:
default:
break;
}
return true;
}
}
And this is how i have applied touch and drag listener:
findViewById(R.id.screen).setOnTouchListener(new MyTouchListener());
findViewById(R.id.middle1).setOnDragListener(new MyDragListener());
Please have a look and suggest me how to solve it. Thanks in advance.