tags:

views:

296

answers:

2

Hi!
I have a simple demo with two buttons. They are laid out with a RelativeLayout at the top and bottom of the screen.
When I click one of them, I want them to switch places. What is the best way to do that?

This is my res/layout/main.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ImageButton
        android:id="@+id/android_button_1"
        android:layout_width="200dip"
        android:layout_height="wrap_content"
        android:src="@drawable/icon"
        android:layout_alignParentTop="true" />

    <ImageButton
        android:id="@+id/android_button_2"
        android:layout_width="200dip"
        android:layout_height="wrap_content"
        android:src="@drawable/icon2"
        android:layout_alignParentBottom="true" />

</RelativeLayout>

And this is my Activity:

public class HelloButtons extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        final ImageButton button1 = (ImageButton) findViewById(R.id.android_button_1);
        final ImageButton button2 = (ImageButton) findViewById(R.id.android_button_2);
        button1.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // Perform action on clicks
                Toast.makeText(HelloButtons.this, "Beep Bop", Toast.LENGTH_SHORT).show();
            }
        });
        button2.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // Perform action on clicks
                Toast.makeText(HelloButtons.this, "Beep Bop", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
+1  A: 

I would say the best way to do that is not to switch the actually ImageButton locations, but to switch the ImageButton images, and keep track of the state inside your application to react to onClicks correctly.

mbaird
That depends. I am looking into something quite similar where I want my element to be long clicked to pick it up, then dragged to a new location on screen---when it is dragged into the area one of the others is already in, it would "displace" the existing one to fill the space it was in before being picked up.
That wasn't as clear as I meant- While the user is still holding onto the item they've picked up, they audition it in different position, and doing so causes displacement of the views like a preview of what it would look like if they dropped it there
+1  A: 

Use something along these lines

   RelativeLayout.LayoutParams lp1 = (LayoutParams) b1.getLayoutParams();
   RelativeLayout.LayoutParams lp2 = (LayoutParams) b2.getLayoutParams();

   lp2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
   lp2.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
   lp1.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
   lp1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 0);

   b1.setLayoutParams(lp1);
   b2.setLayoutParams(lp2);

(and the opposite to revert them again) in you OnClickListeners

dtmilano