views:

256

answers:

1

Hello,

for an application I need to place some objects at the exact position that I want to have them and therefore I need to use AbsoluteLayout this time.

I want to add buttons dynamically exactly like in the following XML - but during runtime.

<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button 
     android:id="@+id/backbutton"
     android:text="TEST123"
     android:layout_x="120px"
     android:layout_y="120px"
     android:layout_width="100px"
     android:layout_height="100px" />
</AbsoluteLayout>

How can I archive this? I tried it with the following code to add a button, but I haven't found a function so far to set layout_x and layout_y. How can I do this? Thanks.

AbsoluteLayout al = new AbsoluteLayout(this);
Button t = new Button(this);
t.setHeight(300);
t.setWidth(300);
t.setText("TEST123");
// x y ???
setContentView(al);
A: 

When you add the view to the layout, you can provide LayoutParams, which is where you specify layout information for the view.

However, AbsoluteLayouts are deprecated, with good reason. How are you going to handle different screen sizes and resolutions? You should be able to create the layout you are looking for with a combination of the non-deprecated layout objects. See developer docs as a starting point.

If you describe your goals in more detail, someone should be able to help you select the proper layout.

Mayra
Well, imagine some kind of a room map, where you place devices to click on them to trigger an action. I thought about using buttons with background images for that and a background image of the room. In my opinion there is no other way than using AbsoluteLayout for that, as I have to deal with x/y coordinates the whole time (right position in the room). What would you recommend me instead ? The problem with different screensize I just ignored as it's a program for the N1 and landscape as layout. I deactived the landscape/portrait-switching as it caused problems in the program.
Nils
Are you saying you have a background image, and you need the buttons to line up with things in the background? In that case it might be hard to not lay things out somewhat absolutely, I think the FrameLayout is the preferred method for that though. It allows you to overlap views. Also, not sure why you are only targeting the N1, just keep in mind that is only a small percentage of Android devices out there! http://developer.android.com/resources/dashboard/screens.html
Mayra
Well, actually I want to draw this backgroud image first (imagine a 2D map of a room, which I want to draw with lines) and then I want to place some items in it, which are clickable images (buttons?). Currently I'm taking a look at GridView, but I'm still very unsure, which might be the correct one for this.
Nils