title: Moving properly an imageview inside a view
body: <p>I am struggling a bit to move an image view properly. My layout has a linearLayout on the top of the screen, right below an edit text and then a view with my image view. When I touch the image, this goes down the screen and since that point I can control it, moving my finger where the ball was at the beginning, not where it actually appears on the screen. I am using getRawX and getRawY (these methods gives the absolute coordinates relative to the screen) and I think the problem is that the image appears on those values but relative to its view, not to the screen. I thought if I would sum up the height of the views above the view where the image view is located and substract it to the getRawX value it would get fixed, but I saw the the method height of LayoutParams just would give me a constant representing fill parent, match parent and wrap content. Anyway, it has to be way easier than doing that.</p>
  
  <p>This is how my layout looks:</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
   &gt;
  
  &lt;LinearLayout
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical" &gt;
  
      &lt;LinearLayout
          android:id="@+id/buttonsLayout"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content" &gt;
  
          &lt;Button
              android:id="@+id/leftButton"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_weight="0.25"
              android:text="LEFT" /&gt;
  
          &lt;Button
              android:id="@+id/upButton"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_weight="0.25"
              android:text="UP" /&gt;
  
          &lt;Button
              android:id="@+id/downButton"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_weight="0.25"
              android:text="DOWN" /&gt;
  
          &lt;Button
              android:id="@+id/rightButton"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_weight="0.25"
              android:text="RIGHT" /&gt;
      &lt;/LinearLayout&gt;
  
      &lt;EditText
          android:id="@+id/coordinatesTextView"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:ems="10" &gt;
  
          &lt;requestFocus /&gt;
      &lt;/EditText&gt;
  
      &lt;ImageView
          android:id="@+id/ballImageView"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:src="@drawable/ball" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p></p>
  
  <pre><code>        ball.setOnTouchListener(new OnTouchListener() {
  
          @Override
          public boolean onTouch(View v, MotionEvent event) {
  
              LayoutParams layoutParams = (LayoutParams) ball.getLayoutParams();
              switch (event.getAction()){
  
                  case MotionEvent.ACTION_DOWN:
                      break;                         
                  case MotionEvent.ACTION_UP:
                      break;                  
                  case MotionEvent.ACTION_MOVE:
  
                      int x_cord = (int) event.getRawX();
                      int y_cord = (int) event.getRawY();
                      layoutParams.leftMargin = x_cord;
                      layoutParams.topMargin = y_cord;
                      ball.setLayoutParams(layoutParams);
                      break;
  
                  default:
                      break;
              }
  
              return true;
          }
      });
  </code></pre>