Moving widgets in runtime on Android

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();
            }
        });
    }
}


Asked by: Maddie699 | Posted: 24-01-2022






Answer 1

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

Answered by: Catherine853 | Posted: 25-02-2022



Answer 2

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

Answered by: Lenny459 | Posted: 25-02-2022



Similar questions

java - Android widgets

How to add imges from SD card in to image widget which is displaying some images in android?


layout - Ugly Widgets - Android

I'm new to Android programming, and i'm building a small test program. My problem is that in the graphical layout of the .xml files the widgets(Spinners,Buttons...) looks nice and round, but when I put my program into my phone(Nexus S, if that matters) or in the emolator, they come out with rounded edges,making the whole program look really ugly. Is theres something i'm doing wrong? Thanks!


layout - Create custom widgets for android

I'm trying to create a rotating imageView. Looking at this (http://stackoverflow.com/questions/1930963/rotating-a-view-in-android) I know I'll have to override onDraw. But when I create my own derrived TextView class, how do I use it in my xml layouts?


android - Source code of widgets

How do you view the source code of widgets? I'm trying to make a custom spinner by extending Spinner and am curious to see the original code. In Eclipse, I've looked in Android 2.2 > android.jar > android.widget > Spinner.class but it tells me source not found.


android - Get widgets from other layouts

How do I access widgets in the java-code from a layout of another Activity? I have a TabActivity which uses two tabs each with it's own Activity class calling setContentView on it's own layout. The TabActivity do this also. I want to be able to use findViewById(R.id.id_from_view_inside_tab_activity) in the TabActivity class. For example. I have the TabActivity Foo and another Activity called Bar. Foo have ...


android - Set the XML file for Layout design according IPhone screen widgets

I want this type of layout how i can do this all the titles should be clickable please help me I used Button and TextView but it seems very dull


Using Custom Widgets / Classes in XML Android

I found a custom TextView Class on here by Chase but for the life of me I cannot get it to work. I have searched high and dry and I am obviously doing something wrong. I would think I would basically be able to call it with &lt;com.packagewhatever.AutoResizeTextView /&gt; with id parameters and such. but... It's not working. I have posted his code below. Please, any help would be ...


Android unit of widgets for different size screens

I am developing an android application for tablet pc's. But i am however confuse with the size of the screen. I created emulator with the size of 850 * 500. And developed some xmls according to this screen, its looking abs fine with this resolution, but when i tried to display same page in smaller screen say 500 * 350, its hiding the down parts of the screen. Lower widgets got invisible. Currently i am using dp


android - how to make two different widgets in one screen?

I'm makeing a project that can show defferent watches to users.The watch has different clock dial.I want to make two different widgets(the watch)in one screen and the watch has different time and clock dial.How can I do it?


android - How to handle widgets buttons

I'm starting with widgets and got a very nice tutorial on the internet, got the example run perfectly, but when i tried to change somethings I got stuck. The thing is: I just want to change the image from my imageButton when i press it, I've tried somethings but nothing seems to work. I didn't get how the RemoteView and Intent works exactly. So if someone can explain it shortly I would appreciate it =) Here...






Still can't find your answer? Check out these communities...



Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android



top