Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Change absolute position of a view programmatically

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);
  }
like image 417
Pascal Klein Avatar asked Aug 09 '10 15:08

Pascal Klein


3 Answers

Use the version of addView that takes LayoutParams:

LayoutParams params = mLayout.generateLayoutParams();
params.x = remoteObject.x;
params.y = remoteObject.y;
mLayout.addView(button, params);
like image 82
Cheryl Simon Avatar answered Nov 15 '22 08:11

Cheryl Simon


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.

like image 24
Amplify91 Avatar answered Nov 15 '22 10:11

Amplify91


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.

like image 5
Lauri Avatar answered Nov 15 '22 09:11

Lauri