title: Using RelativeLayout dynamically and setting margins in px, dp, inch, mm
body: <p>My application is just a modified ImageViewer with the options of zooming and dragging.
  Containing that modified Imageviewer there is a RelativeLayout (that I want to use as an AbsoluteLayout).</p>
  
  <p>Then, new elements can be added to the layout to situate them in certain points of the image. The position (x, y) in pixels of the elements is in a database so I guess there is no other way that adding them programmatically, and not with XML.</p>
  
  <p>Then the position of those elements is updated every time that a dragging or zooming action is performed.</p>
  
  <p>The problem is that somehow the pixels of the image are not matching with the pixels in the RelativeLayout! So the items are "more or less" situated but not in the proper place (the further from (0,0) the bigger is the error).</p>
  
  <p>Things I've tried already:</p>
  
  <ul>
  <li><p>Try different conversions because maybe the problem is related to the difference of densities between the Bitmap and the Layout:</p>
  
  <pre><code>Resources r = getResources();
  float x = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, r.getDisplayMetrics());
  </code></pre></li>
  <li><p>Try to use different methods in the layout to set the margins:</p>
  
  <pre><code>RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
  params.leftMargin = (int) newPosition.x;
  params.topMargin = (int) newPosition.y;
  element.setLayoutParams(params);
  </code></pre></li>
  <li><p>I also wanted to use the options available for the RelativeLayout in XML but I really cant find it programmatically but I can find the way to specify the dimension, I mean:</p>
  
  <pre><code>android:layout_marginLeft ="50px" // or "50mm", or dp, or inches...
  </code></pre></li>
  </ul>