views:

25

answers:

1

Suppose I have the following layout

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/TableLayout">
    <TableRow android:id="@+id/TableRow01" >
        <Button android:text="1" android:id="@+id/button01"/>
        <Button android:text="2" android:id="@+id/button02"/>
        <Button android:text="3" android:id="@+id/button03"/>
        <Button android:text="4" android:id="@+id/button04"/>
    </TableRow>
    <TableRow android:id="@+id/TableRow01" >
        <Button android:text="5" android:id="@+id/button05"/>
        <Button android:text="6" android:id="@+id/button06"/>
        <Button android:text="7" android:id="@+id/button07"/>
        <Button android:text="8" android:id="@+id/button08"/>
    </TableRow>
</TableLayout>

How could I swap position of button01 with button02? And would it work also for swapping button01 with button05?

Before asking what I have tried so far, I don't even know where to start. I googled but the only answer I could find was about AbsoluteLayout, which is not what I need.

+1  A: 

When you say "swap", what do you mean? I suppose you're talking about doing it dynamically, or else you'd just do it in the layout.

What exactly is the purpose of the swapping? Just replacing the text? If that's the case, you could simply call setText() on whichever button you want to change.

EDIT (after clarifying the question via comments): If you want to move them and animate that, then AbsoluteLayout WOULD work, except that it's deprecated. There are other layouts, like using a RelativeLayout and specifying absolute offsets from the top left, or even a GridView. You could then use a TranslateAnimation to move the buttons.

EboMike
@EboMike I think klez means to literally swap the locations of the two buttons. But you are right, "swapping" can be emulated by switching the text (and other appearances) and the callback handlers.
Andy Zhang
It's a fifteen puzzle, so I'm hoping to also animate the swap.
klez
Ah, so you don't want to just swap them, you want to physically move them? Well, AbsoluteLayout WOULD work, except that it's deprecated. There are other layouts, like using a RelativeLayout and specifying absolute offsets from the top left, or even a GridView. You could then use a TranslateAnimation to move the buttons.
EboMike
I'll try, thanks.
klez
@EboMike: could you please edit your answer, or post another answer, about the RelativeLayout so I can accept it?
klez
You ask and shall receive :)
EboMike