title: How do I programmatically position an ImageView in AndroidStudio
body: <p>I want to position my ImageView in Android Studio so it can be in the same place (ratio wise) throughout the many screen sizes of Android phones.</p>
  
  <p>this is my code in <code>activity_main.xml</code>:</p>
  
  <pre><code>&lt;RelativeLayout 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"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context="com.slippysam.sooper_fly.slippysam.MainActivity"
  android:background="@drawable/bgday"&gt;
  
  &lt;ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:maxHeight="40dp"
      android:maxWidth="40dp"
      android:adjustViewBounds="true"
  
      android:src="@drawable/sam"
      android:id="@+id/samm"
      /&gt;
  &lt;/RelativeLayout/&gt;
  </code></pre>
  
  <p>and here is a function I tried using in my <code>Activity.java</code></p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
     positionSam();
  
  }
  public void positionSam(){
      final ImageView sam = (ImageView)findViewById(R.id.samm);
  
      DisplayMetrics dm = getApplicationContext().getResources().getDisplayMetrics();
      float dpHeight = dm.heightPixels / dm.density;
      float dpWidth = dm.widthPixels / dm.density;
  
      RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) sam.getLayoutParams();
      lp.leftMargin = (int) ((dpWidth * 0.5));
      lp.topMargin = (int) ((dpHeight * 1.0555));
      sam.setLayoutParams(lp);
  }
  }
  </code></pre>
  
  <p>this function does indeed change the position of my ImageView however it's position still isn't concrete when I switch between products. </p>