Note that there are some explanatory texts on larger screens.

plurals
  1. POMoving widgets in runtime on Android
    text
    copied!<p>I have a simple demo with two buttons. They are laid out with a RelativeLayout at the top and bottom of the screen.<br> When I click one of them, I want them to switch places. What is the best way to do that?</p> <p>This is my res/layout/main.xml :</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;ImageButton android:id="@+id/android_button_1" android:layout_width="200dip" android:layout_height="wrap_content" android:src="@drawable/icon" android:layout_alignParentTop="true" /&gt; &lt;ImageButton android:id="@+id/android_button_2" android:layout_width="200dip" android:layout_height="wrap_content" android:src="@drawable/icon2" android:layout_alignParentBottom="true" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>And this is my Activity:</p> <pre><code>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(); } }); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload