In a nutshell, I want to give users the ability to drag and drop EditText
- however, when the EditText
is released instead of dropping it disappears for some reason and I get the message:
I/ViewRootImpl﹕ Reporting drop result: false
I have an Activity with 2 FrameLayout's, camera_preview and bottom_toolbar like so:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/camera_preview"
android:layout_above="@+id/bottom_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<FrameLayout
android:id="@+id/bottom_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentBottom="true">
</FrameLayout>
</RelativeLayout>
When bottom_toolbar is tapped, it is replaced by a Fragment who's XML contains a LinearLayout holding 2 EditTexts. I have set touch and drag listeners for these EditText's in onCreate() and added these classes to the said Fragment:
private final class MyTouchListener implements View.OnTouchListener {
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.setVisibility(View.INVISIBLE);
view.startDrag(data, shadowBuilder, view, 0);
return true;
}
else
{
return false;
}
}
}
class MyDragListener implements View.OnDragListener {
private android.widget.LinearLayout.LayoutParams layoutParams;
@Override
public boolean onDrag(View v, DragEvent event) {
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
layoutParams = (LinearLayout.LayoutParams) v.getLayoutParams();
Log.d("TAG", "Started");
break;
case DragEvent.ACTION_DRAG_ENTERED:
int x_cord = (int) event.getX();
int y_cord = (int) event.getY();
Log.d("TAG", "Entered");
break;
case DragEvent.ACTION_DRAG_EXITED:
x_cord = (int) event.getX();
y_cord = (int) event.getY();
layoutParams.leftMargin = x_cord;
layoutParams.topMargin = y_cord;
v.setLayoutParams(layoutParams);
Log.d("TAG", "Exited");
break;
case DragEvent.ACTION_DRAG_LOCATION:
x_cord = (int) event.getX();
y_cord = (int) event.getY();
Log.d("TAG", "Loc");
break;
case DragEvent.ACTION_DRAG_ENDED:
//((View)event.getLocalState()).setVisibility(View.VISIBLE);
v.setVisibility(View.VISIBLE);
Log.d("TAG", "Ended");
break;
case DragEvent.ACTION_DROP:
v.setVisibility(View.VISIBLE);
Log.d("TAG", "Dropped");
break;
default:
break;
}
return true;
}
}
Does anybody know why my EditText
s disappear when I drop them? I have spent countless hours online looking for a solution, but none of them seem to work.
Appreciate any advice, thanks.
EDIT:
Changed my drag listener to this:
class MyDragListener implements View.OnDragListener {
@Override
public boolean onDrag(View v, DragEvent event) {
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
break;
case DragEvent.ACTION_DRAG_ENTERED:
break;
case DragEvent.ACTION_DRAG_EXITED:
break;
case DragEvent.ACTION_DROP:
View view = (View) event.getLocalState();
ViewGroup owner = (ViewGroup) view.getParent();
owner.removeView(view);
FrameLayout container = (FrameLayout) v;
container.addView(view);
view.setVisibility(View.VISIBLE);
break;
case DragEvent.ACTION_DRAG_ENDED:
default:
break;
}
return true;
}
}
I removed the drag listener from my EditText
(kept the touch listener) and added it to the view I want to drop them in instead. I no longer get the false drop message and I'm certain it's dropping the EditText
's because the fragment gets smaller when they are dragged out of it. I still can't see them though. Could it be that they are being dropped BEHIND the view instead of in front of it? If so, how can I avoid that? Thanks again.
Edit: bump