Reputation: 554
I want to add drag and drop functionality to a textview in my testing app The main layout contains a relativelayout(RL1) , which inturn contains another relativelayout(RL2) now the RL2 contains a textview which can able to be dragged and dropped anywhere
But when i perform drag on the textview its getting dragged but went i drop it it returns back to its originsl position please help me
my mainactivity
public class MainActivity extends Activity
{
TextView tv;
LayoutParams lParams;
RelativeLayout layout;
private static final String Tv_TAG =" TV_DRAG";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.mainTextView1);
tv.setTag(Tv_TAG);
tv.setOnTouchListener(new MyTouchListener());
layout = (RelativeLayout) findViewById(R.id. draggableRelativeLayout );
layout.setOnDragListener(new MyDragListener());
}
private class MyTouchListener implements OnTouchListener
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
ClipData.Item item = new ClipData.Item((CharSequence) v.getTag());
String[] mimeTypes ={ClipDescription.MIMETYPE_TEXT_PLAIN};
ClipData dragData = new ClipData(v.getTag().toString(), mimeTypes, item);
View.DragShadowBuilder shdwbldr = new View.DragShadowBuilder(v);
v.startDrag(dragData, shdwbldr, v, 0);
v.setVisibility(View.INVISIBLE);
return true;
}
}
private class MyDragListener implements OnDragListener
{
@Override
public boolean onDrag(View v, DragEvent event)
{
View view = (View) event.getLocalState();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
lParams =(RelativeLayout.LayoutParams)view.getLayoutParams();
break;
case DragEvent.ACTION_DRAG_ENTERED:
int x_cord = (int) event.getX();
int y_cord = (int) event.getY();
break;
case DragEvent.ACTION_DRAG_EXITED:
x_cord = (int) event.getX();
y_cord = (int) event.getY();
lParams.leftMargin = x_cord;
lParams.topMargin = y_cord;
view.setLayoutParams(lParams);
break;
case DragEvent.ACTION_DROP:
// Dropped, reassign View to ViewGroup
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:
break;
default: break;
}
return true;
}
}
}
**my layout **
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="#AE9C9B"
android:id="@+id/ draggableRelativeLayout ">
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="@null"
android:id="@+id/mainRelativeLayout1">
<TextView
android:layout_height="wrap_content"
android:text="DRAG ME"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"
android:layout_margin="10dp"
android:id="@+id/mainTextView1"/>
</RelativeLayout>
Upvotes: 0
Views: 2827
Reputation: 5517
You'll need to set the new coordinates to your view:
case DragEvent.ACTION_DROP:
v.setX(event.getX());
v.setY(event.getY());
break;
Upvotes: 2