0

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);
Kamran224
  • 1,584
  • 7
  • 20
  • 33

2 Answers2

4

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.

Joseph Woodward
  • 9,191
  • 5
  • 44
  • 63
jeet
  • 29,001
  • 6
  • 52
  • 53
0

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)

Community
  • 1
  • 1
Huang
  • 4,812
  • 3
  • 21
  • 20