title: How to Bring layout back to its original position in android after drag
body: <p>I have working on a project. where I need to drag the layout,  I manage to move (drag) the layout but when I release the touch it is not coming at its original position.</p>
  
  <p>Please anyone tell me what should I do in MotionEvent.ACTION_UP so that I can get layout back to its original position.</p>
  
  <p>Following is my code.</p>
  
  <pre><code>import android.app.Activity;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.MotionEvent;
  import android.view.View;
  import android.view.View.OnTouchListener;
  import android.view.ViewGroup;
  import android.widget.RelativeLayout;
  
  public class HomeActivity extends Activity implements OnTouchListener {
  
      RelativeLayout _view;
      RelativeLayout _view1;
      // TextView _view;
      ViewGroup _root;
      private int _xDelta;
      RelativeLayout.LayoutParams lParams;
      RelativeLayout.LayoutParams mainlParams;
      private int _yDelta;
  
      // private int X1, Y1, width;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
          _root = (ViewGroup) findViewById(R.id.root);
  
          _view = (RelativeLayout) findViewById(R.id.relativeLayout);
  
  
  
          _view.setOnTouchListener(this);
          lParams = (RelativeLayout.LayoutParams) _view.getLayoutParams();
          mainlParams = (RelativeLayout.LayoutParams) _view.getLayoutParams();
          // _root.addView(_view1);
  
      }
  
      public boolean onTouch(View view, MotionEvent event) {
          int X = (int) event.getRawX();
          int Y = (int) event.getRawY();
  
          RelativeLayout.LayoutParams layoutParams;
          lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
  
          switch (event.getAction() &amp; MotionEvent.ACTION_MASK) {
          case MotionEvent.ACTION_DOWN:
              Log.e("", "inside ACTION_DOWN");
              lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
              _xDelta = X - lParams.leftMargin;
              _yDelta = Y - lParams.topMargin;
  
              break;
          case MotionEvent.ACTION_UP:
              Log.e("", "inside ACTION_UP");
              if ((X - _xDelta) &lt; -150) {
                  _root.removeView(_view);
              } else {
                  view.setLayoutParams(mainlParams);
              }
  
              break;
          case MotionEvent.ACTION_MOVE:
              Log.e("", "inside ACTION_MOVE");
  
              layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
              layoutParams.leftMargin = X - _xDelta;
              layoutParams.topMargin = Y - _yDelta;
              layoutParams.rightMargin = -250;
              layoutParams.bottomMargin = -250;
  
              view.setLayoutParams(layoutParams);
              break;
          }
          _root.invalidate();
          return true;
      }
  
  }
  </code></pre>