Note that there are some explanatory texts on larger screens.

plurals
  1. POdragging a image in boundary
    primarykey
    data
    text
    <p><img src="https://i.stack.imgur.com/Gm8J6.png" alt="enter image description here"></p> <p>I want that image only be dragged in its parent view. when user tries to drag it outside the view it will return its original position. basically i'm implementing android unlock type feature. please help me out. Its my main class.</p> <pre><code>public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.myimage1).setOnTouchListener(new MyTouchListener()); findViewById(R.id.topleft).setOnDragListener(new MyDragListener()); } 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 { Drawable enterShape = getResources().getDrawable(R.drawable.shape_droptarget); Drawable normalShape = getResources().getDrawable(R.drawable.shape); @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: v.setBackgroundResource(R.drawable.shape_droptarget); break; case DragEvent.ACTION_DRAG_EXITED: v.setBackgroundResource(R.drawable.shape); setFinishOnTouchOutside(true); break; case DragEvent.ACTION_DROP: // Dropped, reassign View to ViewGroup View view = (View) event.getLocalState(); ViewGroup owner = (ViewGroup) view.getParent(); owner.removeView(view); LinearLayout container = (LinearLayout) v; container.addView(view); view.setVisibility(View.VISIBLE); break; case DragEvent.ACTION_DRAG_ENDED: v.setBackgroundResource(R.drawable.shape); default: break; } return true; } } } </code></pre> <p>main xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"&gt; &lt;RelativeLayout android:id="@+id/topleft" android:layout_width="match_parent" android:layout_height="100dp" android:layout_centerInParent="true" android:background="@drawable/shape" &gt; &lt;ImageView android:id="@+id/myimage1" android:layout_width="wrap_content" android:layout_height="80dp" android:layout_centerVertical="true" android:src="@drawable/ic_launcher" /&gt; &lt;/RelativeLayout&gt; &lt;/RelativeLayout&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload