23

If you use an AbsoluteLayout (I know that it is deprecated, but it was the only way to solve my problem) you can give the childViews the tag android:layout_x and android:layout_y to set their absolute position within the AbsoluteLayout.

However I don't want to set these information in the xml, because I only know them at runtime. So how can I set these parameters at runtime programmatically? I don't see any method on the View like view.setLayoutX(int x) or something.

Here is my XML, which works fine, when I set the layout_x and layout_y values:

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/myLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
     android:src="@drawable/myImageView"
     android:layout_width="1298px"
     android:layout_height="945px"
     android:layout_x="0px"
  android:layout_y="0px" />
 <Button  
  android:id="@+id/myButton1"
  android:text="23"
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content"
  android:layout_x="50px"
  android:layout_y="300px"
  android:tag="23"/>


 <Button  
  android:id="@+id/myButton2"
  android:text="48"
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content"
  android:layout_x="50px"
  android:layout_y="300px"
  android:tag="48"/>

</AbsoluteLayout>

In fact, I don't want to set any button within the xml anymore, but rather retrieve some information via remote and add buttons depending on that information.

Here is the part the code I'm using so in my onCreateMethod to add these buttons:

  for (MyRemoteObject remoteObject: list)  {
   Button button = new Button(this);
   button.setOnClickListener (listener);
   button.setTag(remoteObject.id);
   button.setText(remoteObject.id);
   // button.setLayoutX(remoteObject.x) ????
   // button.setLayoutY(remoteObject.y) ????
   myLayout.addView(button);
  }
Community
  • 1
  • 1
Pascal Klein
  • 23,665
  • 24
  • 82
  • 119

5 Answers5

16

Use the version of addView that takes LayoutParams:

LayoutParams params = mLayout.generateLayoutParams();
params.x = remoteObject.x;
params.y = remoteObject.y;
mLayout.addView(button, params);
Cheryl Simon
  • 46,552
  • 15
  • 93
  • 82
  • 1
    Thank you, but how how can i create LayoutParams given that mLayout is of Type AbsoluteLayout? AbsoluteLayout only has a method absolutLayout.generatelayoutParams(AttributeSet). I dont know how to create an attributeSet in the first place so that I can pass that set to the generateLayoutParams()-Method – Pascal Klein Aug 09 '10 at 17:35
  • I have the same problem with a RelativeLayout instead of Absolute – Amplify91 Mar 25 '11 at 20:04
  • `params.x` and `params.y` seems like they are deprecated for `RelativeLayout` – ThunderWiring Mar 31 '18 at 16:51
10

In response to your comment on Mayra's answer, I had a similar issue with RelativeLayout instead of AbsoluteLayout. The solution was to use a similar method and cast it as your layout type. Something like this:

LayoutParams params = (android.widget.RelativeLayout.LayoutParams) this.generateDefaultLayoutParams();
params.height = 360;
params.width = 360;
params.addRule(CENTER_IN_PARENT);
this.addView(board, params);

It took me forever to figure this out so I wanted to post it here for someone else if they needed it.

Amplify91
  • 2,690
  • 5
  • 24
  • 32
5

I had just the same problem but found a somewhat different solution.

int width = 100, height = 50, x = 10, y = 20;
Button button = new Button(this);
AbsoluteLayout myLayout = (AbsoluteLayout)findViewById(R.id.myLayout);
myLayout.add(button, new AbsoluteLayout.LayoutParams(width, height, x, y));

And if you find out what to use for "fill_parent" (hint try -1) then you may use those constants for width and height.

Shaheen Ghiassy
  • 7,397
  • 3
  • 40
  • 40
Lauri
  • 51
  • 1
  • 1
0

I don't know why, but when moving top and left edges the Android keeps the right and bottom edges in same place. As I could not change the width and height properly (the image was disappearing), I've used this:

LayoutParams layoutParams=new LayoutParams(width, height);
layoutParams.leftMargin = newLeft;
layoutParams.topMargin = newTop;
imageView.setLayoutParams(layoutParams);

I don't know if it's the best way, but worked for me. I've tested it in 2.2+ and works fine!

You must import android.widget.LinearLayout.LayoutParams; because the default is ViewGroup's LayoutParams (by Davit T)

Community
  • 1
  • 1
Rafael Brasil
  • 232
  • 1
  • 6
0

the above answers are right.but this one would be more perfect

AbsoluteLayout.LayoutParams layoutParams = new AbsoluteLayout.LayoutParams(AbsoluteLayout.LayoutParams.WRAP_CONTENT,AbsoluteLayout.LayoutParams.WRAP_CONTENT,DragLayer.int left_location,int top_location);
 layout.addView(view,layoutParams)
Asthme
  • 5,163
  • 6
  • 47
  • 65