title: how to drag a button from center of a cardview layout to its left, right, top and bottom in android
body: <p>I want to <strong>drag</strong> a button/view from center of a <strong>card view</strong> layout to its adjacent sides.and the view should not go out of the card view.</p>
  
  <p>This is what i did so far</p>
  
  <pre><code> &lt;android.support.v7.widget.CardView
      android:layout_width="300dp"
      android:layout_height="300dp"
      app:cardCornerRadius="150dp"
      app:cardElevation="0dp"
      android:gravity="center"
      app:cardBackgroundColor="#50001848"
      android:layout_centerInParent="true"&gt;
      &lt;RelativeLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_gravity="center"
          android:gravity="center"
          android:id="@+id/root"&gt;
          &lt;ImageView
              android:id="@+id/image"
              android:layout_width="100dp"
              android:layout_height="100dp"
              android:scaleType="centerCrop"
              android:src="@mipmap/ic_launcher_round"/&gt;
      &lt;/RelativeLayout&gt;
  &lt;/android.support.v7.widget.CardView&gt;
  </code></pre>
  
  <p>MainActivity.java</p>
  
  <pre><code>    public class MainActivity extends AppCompatActivity implements View.OnTouchListener {
      ImageView _view;
      ViewGroup _root;
      private int _xDelta;
      private int _yDelta;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          _root = findViewById(R.id.root);
  
  
          _view=findViewById(R.id.image);
  
          RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(300, 300);
          layoutParams.leftMargin = 50;
          layoutParams.topMargin = 50;
          layoutParams.bottomMargin = 50;
          layoutParams.rightMargin = 50;
          _view.setLayoutParams(layoutParams);
  
          _view.setOnTouchListener(this);
  
      }
  
      @Override
      public boolean onTouch(View view, MotionEvent event) {
          final int X = (int) event.getRawX();
          final int Y = (int) event.getRawY();
          switch (event.getAction() &amp; MotionEvent.ACTION_MASK) {
              case MotionEvent.ACTION_DOWN:
                  RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
                  _xDelta = X - lParams.leftMargin;
                  _yDelta = Y - lParams.topMargin;
                  break;
              case MotionEvent.ACTION_UP:
                  break;
              case MotionEvent.ACTION_POINTER_DOWN:
                  break;
              case MotionEvent.ACTION_POINTER_UP:
                  break;
              case MotionEvent.ACTION_MOVE:
                  RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
                  layoutParams.leftMargin = X - _xDelta;
                  layoutParams.topMargin = Y - _yDelta;
                  layoutParams.rightMargin = -50;
                  layoutParams.bottomMargin = -50;
                  view.setLayoutParams(layoutParams);
                  break;
          }
          _root.invalidate();
          return true;
      }
  }
  </code></pre>
  
  <p>When i drag the image goes out of the circle.i want the image stay inside the circle and get the boundary of the circle.Please help to achieve this.</p>