Android: After Drop, Edittext That Was Being Dragged Disapears
Solution 1:
I get this error message in logcat: Reporting drop result: false
It's not error message. It's mean that the Action drop not returned true
as result by the View
DragListener
.
Your EditText
disappear because you set it invisiblity to View.INVISIBLE
inside your onTouchListener
and never changed it back.
try this: First choose if you want to Drag by Touch or by Long Click. You don't need both. Let's say you decided to do it by touch:
editText.setOnTouchListener(newView.OnTouchListener() {
@OverridepublicbooleanonTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
ClipDatadata= ClipData.newPlainText("", "");
View.DragShadowBuildershadowBuilder=newView.DragShadowBuilder(editText);
editText.startDrag(data, shadowBuilder, editText, 0);
editText.setVisibility(View.INVISIBLE);
returntrue;
}
else
{
returnfalse;
}
}
});
Remove all the reduntant code from the code Listener, no need it to the Drag as the system handle the drag for you. If you want the coordinations of the Drop to be the new location of the EditText
than you need to assign DragListener
to the EditText
container RelativeLayout
and retrieve them in `ACTION_DROP. If not:
editText.setOnDragListener(new View.OnDragListener() {
@Override
public boolean onDrag(View v, DragEvent event) {
if (event.getAction == DragEvent.ACTION_DRAG_STARTED){
layoutParams = (RelativeLayout.LayoutParams)v.getLayoutParams();
} elseif (event.getAction() == DragEvent.ACTION_DRAG_ENDED)
v.setVisibility(View.VISIBLE)
returntrue;
}
});
For getting the drop location you need something like that:
findViewById(R.id.relativeLayout).setOnDragListener(new View.OnDragListener() {
@Override
public boolean onDrag(View v, DragEvent event) {
if (event.getAction() == DragEvent.ACTION_DRAG_DROP){
int x_cord = (int) event.getX();
int y_cord = (int) event.getY();
layoutParams.leftMargin = x_cord;
layoutParams.topMargin = y_cord;
editText.setLayoutParams(layoutParams);
}
returntrue;
}
});
Post a Comment for "Android: After Drop, Edittext That Was Being Dragged Disapears"