title: Button Movement according to user finger in screen android
body: <p>I have a relativeLayout which contains a button.I need to make the button that moves according to the movement of the user interaction only in horizontal way and only in left side from the starting point(Like whatsapp screen audio button)</p>
  
  <pre><code> mButton = (ImageButton) findViewById(R.id.button);
  </code></pre>
  
  <p>mPrevX,mPrevY => starting coordinates</p>
  
  <pre><code>mPrevX = mButton.getX();
  mPrevY = mButton.getY();
  </code></pre>
  
  <p>currX,currY => current coordinates</p>
  
  <pre><code>currX = event.getRawX();
  currY = event.getRawY();
  </code></pre>
  
  <p>Code to move button</p>
  
  <pre><code>mButton = (ImageButton) findViewById(R.id.button);
  ViewGroup.MarginLayoutParams marginParams =
             new ViewGroup.MarginLayoutParams(v.getLayoutParams());
  marginParams.setMargins((int)(currX-200), (int)(mlayout.getWidth()-mPrevY )
            ,(int)(mlayout.getWidth()-currX),(int)(mlayout.getHeight()));
  RelativeLayout.LayoutParams layoutParams =
            new RelativeLayout.LayoutParams(marginParams);
  mButton.setLayoutParams(layoutParams);
  </code></pre>
  
  <p>Problem is it is moving but at directions apart from the user interaction on screen help me to do this.</p>