Kamran224
Kamran224

Reputation: 1614

android sdk set imageview x y coordinates

I'm using an absolutelayout (I know its deprecated). I am adding imageview dynamically but I can't change its position from the top left corner of the screen.

ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.orb1);
this.addContentView(iv, new AbsoluteLayout.LayoutParams(80, 80, 200, 200));

However, this creates the image with top left at 0, 0 instead of anywhere else. If I change the last two values of Layout Params it still remains in that position.

Can someone assist? Thanks.

Edit: Using relative layout still doesn't work

ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.orb1);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(80, 80);
params.topMargin=50;
params.leftMargin=50;
this.addContentView(iv, params);

Upvotes: 0

Views: 11390

Answers (2)

jeet
jeet

Reputation: 29199

Add view to a layout using addView method, because layout params can be applied to a layout not window, so do the following:

RelativeLayout rlMain = (RelativeLayout) findViewById(R.id.mainLayout);

ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.orb1);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(80, 80);
params.topMargin=50;
params.leftMargin=50;
rlMain.addView(iv, params);

replace mainLayout to the id which you are providing to relativelayout, imageVIew being supposed to add.

Upvotes: 4

Huang
Huang

Reputation: 4842

I think you can't archieve this by using a AbsoluteLayout. You should use a RelativeLayout or FrameLayout to control the position of your views precisely.

I once answered a similar question. Please see here: How to move a Textview dynamically on the screen? (framelayout)

Upvotes: 0

Related Questions