In my application,I want to drag the image only in its parent layout,but it is dragging in whole device screen. I tried my best to achieve this,but did not find any solution.
I have added my xml and code here. I searched for this but could not able to find the solution.can anyone help me to solve this issue?
I mentioned in the picture clearly about what I want to achieve.I want to restrict the image dragging in those y1 and y2 values.
my xml:
RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mainlayout"
tools:context=".MainActivity" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:id="@+id/images">
<!-- cartoon image -->
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/img_1"
android:layout_centerInParent="true"
android:id="@+id/cartoon_image">
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@drawable/play_btn"
android:orientation="horizontal"
android:layout_centerInParent="true"
android:id="@+id/play_btn"
>
</RelativeLayout>
</RelativeLayout>
<!-- end of cartoon image -->
</RelativeLayout>
</RelativeLayout>
My Activity:
public class MainActivity extends Activity {
RelativeLayout rlImages,Cartoon_image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Cartoon_image=(RelativeLayout)findViewById(R.id.cartoon_image);
rlImages=(RelativeLayout)findViewById(R.id.images);
rlImages.setOnDragListener(new MyDragListener());
Cartoon_image.setOnTouchListener(new MyTouchListener());
}
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 dragAction = event.getAction();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
break;
case DragEvent.ACTION_DRAG_ENTERED:
Log.e("in entered","in enter");
break;
case DragEvent.ACTION_DRAG_EXITED:
Log.e("in exited","in exit");
break;
case DragEvent.ACTION_DROP:
Float s1=event.getX();
Float s2=event.getY();
int x=Integer.valueOf(s1.intValue());
int y=Integer.valueOf(s2.intValue());
View view = (View) event.getLocalState();
ViewGroup owner = (ViewGroup) view.getParent();
owner.removeView(view);
RelativeLayout container = (RelativeLayout) v;
container.addView(view);
break;
case DragEvent.ACTION_DRAG_ENDED:
Log.e("xs"+(int)event.getX(),"xs"+(int)event.getX());
v.setVisibility(View.VISIBLE);
if (dropEventNotHandled(event)) {
v.setVisibility(View.VISIBLE);
}
view.setVisibility(View.VISIBLE);
default:
break;
}
return true;
}
private boolean dropEventNotHandled(DragEvent dragEvent) {
return !dragEvent.getResult();
}
}
}![I have added the image for better understanding,Please find it][1]
1: