title: When dragging an Imageview goes outside the screen
body: <p>I want to implement a movable imageView when it is drag. The dragging is working perfectly alright.  But when i drag to the end , the imageView goes out of the screen. <strong>How can i make the movable of the imageview only inside the screen ?</strong></p>
  
  <p>This is my OnTouch listerner :</p>
  
  <pre><code>public boolean onTouch(View view, MotionEvent event) {
      switch (event.getActionMasked()) {
        case MotionEvent.ACTION_DOWN:
          dX = view.getX() - event.getRawX();
          dY = view.getY() - event.getRawY();
          lastAction = MotionEvent.ACTION_DOWN;
          break;
  
        case MotionEvent.ACTION_MOVE:
          view.setY(event.getRawY() + dY);
          view.setX(event.getRawX() + dX);
          lastAction = MotionEvent.ACTION_MOVE;
          break;
  
        case MotionEvent.ACTION_UP:
          if (lastAction == MotionEvent.ACTION_DOWN)
            Toast.makeText(DraggableView.this, "Clicked!", Toast.LENGTH_SHORT).show();
          break;
  
        default:
          return false;
      }
      return true;
    }
  </code></pre>
  
  <p>and this is my layout. the layout contain a scrollview and their child, and the imageview which i want to drag.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent" &gt;
  
      &lt;ScrollView
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:clickable="true" &gt;
  
          &lt;LinearLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical" &gt;
  
              &lt;TextView
                  android:layout_width="match_parent"
                  android:layout_height="400dp"
                  android:background="#ff0000"
                  android:gravity="center"
                  android:text="View 1"
                  android:textColor="#000"
                  android:textSize="100dp" /&gt;
  
              &lt;TextView
                  android:layout_width="match_parent"
                  android:layout_height="400dp"
                  android:background="#00ff00"
                  android:text="View 2"
                  android:textColor="#000"
                  android:textSize="100dp" /&gt;  
          &lt;/LinearLayout&gt;
      &lt;/ScrollView&gt;
  
      &lt;ImageView
          android:id="@+id/draggable_view"
          android:layout_width="100dp"
          android:layout_height="100dp"
          android:layout_gravity="bottom|right"
          android:layout_marginBottom="20dp"
          android:layout_marginEnd="20dp"
          android:background="@drawable/icon" /&gt;
  
  &lt;/FrameLayout&gt;
  </code></pre>