Set the absolute position of a view Set the absolute position of a view android android

Set the absolute position of a view


You can use RelativeLayout. Let's say you wanted a 30x40 ImageView at position (50,60) inside your layout. Somewhere in your activity:

// Some existing RelativeLayout from your layout xmlRelativeLayout rl = (RelativeLayout) findViewById(R.id.my_relative_layout);ImageView iv = new ImageView(this);RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(30, 40);params.leftMargin = 50;params.topMargin = 60;rl.addView(iv, params);

More examples:

Places two 30x40 ImageViews (one yellow, one red) at (50,60) and (80,90), respectively:

RelativeLayout rl = (RelativeLayout) findViewById(R.id.my_relative_layout);ImageView iv;RelativeLayout.LayoutParams params;iv = new ImageView(this);iv.setBackgroundColor(Color.YELLOW);params = new RelativeLayout.LayoutParams(30, 40);params.leftMargin = 50;params.topMargin = 60;rl.addView(iv, params);iv = new ImageView(this);iv.setBackgroundColor(Color.RED);params = new RelativeLayout.LayoutParams(30, 40);params.leftMargin = 80;params.topMargin = 90;rl.addView(iv, params);

Places one 30x40 yellow ImageView at (50,60) and another 30x40 red ImageView <80,90> relative to the yellow ImageView:

RelativeLayout rl = (RelativeLayout) findViewById(R.id.my_relative_layout);ImageView iv;RelativeLayout.LayoutParams params;int yellow_iv_id = 123; // Some arbitrary ID value.iv = new ImageView(this);iv.setId(yellow_iv_id);iv.setBackgroundColor(Color.YELLOW);params = new RelativeLayout.LayoutParams(30, 40);params.leftMargin = 50;params.topMargin = 60;rl.addView(iv, params);iv = new ImageView(this);iv.setBackgroundColor(Color.RED);params = new RelativeLayout.LayoutParams(30, 40);params.leftMargin = 80;params.topMargin = 90;// This line defines how params.leftMargin and params.topMargin are interpreted.// In this case, "<80,90>" means <80,90> to the right of the yellow ImageView.params.addRule(RelativeLayout.RIGHT_OF, yellow_iv_id);rl.addView(iv, params);


In general, you can add a View in a specific position using a FrameLayout as container by specifying the leftMargin and topMargin attributes.

The following example will place a 20x20px ImageView at position (100,200) using a FrameLayout as fullscreen container:

XML

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/root"    android:background="#33AAFF"    android:layout_width="match_parent"    android:layout_height="match_parent" ></FrameLayout>

Activity / Fragment / Custom view

//...FrameLayout root = (FrameLayout)findViewById(R.id.root);ImageView img = new ImageView(this);img.setBackgroundColor(Color.RED);//..load something inside the ImageView, we just set the background colorFrameLayout.LayoutParams params = new FrameLayout.LayoutParams(20, 20);params.leftMargin = 100;params.topMargin  = 200;root.addView(img, params);//...

This will do the trick because margins can be used as absolute (X,Y) coordinates without a RelativeLayout:

enter image description here


Just to add to Andy Zhang's answer above, if you want to, you can give param to rl.addView, then make changes to it later, so:

params = new RelativeLayout.LayoutParams(30, 40);params.leftMargin = 50;params.topMargin = 60;rl.addView(iv, params);

Could equally well be written as:

params = new RelativeLayout.LayoutParams(30, 40);rl.addView(iv, params);params.leftMargin = 50;params.topMargin = 60;

So if you retain the params variable, you can change the layout of iv at any time after adding it to rl.