5

I have two ImageViews inside an AbsoluteLayout.

<AbsoluteLayout android:id="@+id/AbsoluteLayout01" 
android:layout_width="fill_parent" android:background="@drawable/whitebackground" 
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">

<ImageView android:id="@+id/floorPlanBackgroundImage"
    android:src="@drawable/ic_tab_lights_gray"
    android:scaleType="center" 
    android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>

<ImageView android:id="@+id/fpLight"
    android:src="@drawable/ic_tab_lights_gray"
    android:scaleType="center" android:layout_x="50px" android:layout_y="50px" 
    android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>


</AbsoluteLayout>

the floorPlanBackgroundImage is dynamically set from an image that is 800x600 in size. The can scroll around it.

My second image, fpLight represents a light in a room. It's a small 20x20 image. What I need to do is change the layout_x & layout_y properties in code, but I don't see a way to set that in the ImageView.

I'd expected something like this...

fpLight.setLayoutX("55px");

Is there a way?

bugfixr
  • 7,997
  • 18
  • 91
  • 144

2 Answers2

10
yourImageView.setX(floatXcoord);
yourImageView.setY(floatYcoord);
Andrew
  • 36,676
  • 11
  • 141
  • 113
10

You should use:

AbsoluteLayout.LayoutParams param = new AbsoluteLayout.LayoutParams(int width, int height, int x, int y)

layout Parameter object with the preferred x and y and set it to your fpLight image by

fpLight.setLayoutParams(param);

AbsoluteLayout is Deprecated

You should use RelativeLayout instead

EXAMPLE:

Suppose you want a ImageView of size 50x60 on your screen at postion (70x80)

// RelativeLayout. though you can use xml RelativeLayout here too by `findViewById()`
RelativeLayout relativeLayout = new RelativeLayout(this);
// ImageView
ImageView imageView = new ImageView(this);

// Setting layout params to our RelativeLayout
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(50, 60);

// Setting position of our ImageView
layoutParams.leftMargin = 70;
layoutParams.topMargin = 80;

// Finally Adding the imageView to RelativeLayout and its position
relativeLayout.addView(imageView, layoutParams);
Atiq
  • 14,435
  • 6
  • 54
  • 69
Neeraj Nama
  • 1,562
  • 1
  • 18
  • 24