Dilip Kumar Chaudhary
Dilip Kumar Chaudhary

Reputation: 87

How to swap two button position in android

I want to swap two button positions when we click on toggle button. I have tried a lot but haven't got a success. So if anyone knows how to do this please suggest me. Thanks all.

Upvotes: 3

Views: 5015

Answers (5)

Zar E Ahmer
Zar E Ahmer

Reputation: 34390

Example for exchanging edittext

button2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            EditText button3;
            EditText button4;
            button3 = (EditText) findViewById(R.id.button3);
            button4 = (EditText) findViewById(R.id.button4);
            int top3 = button3.getTop();
            int bot3 = button3.getBottom();
            int left3 = button3.getLeft();
            int right3 = button3.getRight();

            int top4 = button4.getTop();
            int bot4 = button4.getBottom();
            int left4 = button4.getLeft();
            int right4 = button4.getRight();

            button4.setTop(top3);
            button4.setBottom(bot3);
            button4.setLeft(left3);
            button4.setRight(right3);

            button3.setTop(top4);
            button3.setBottom(bot4);
            button3.setLeft(left4);
            button3.setRight(right4);

        }
    });

Upvotes: 0

Raju
Raju

Reputation: 21

//This is the Code what you need for swapping buttons.

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Foo extends Activity {

    Button switcher;
    Button A1;
    Button A2;
    Button B1;
    Button B2;
    boolean buttons1AreVisible = true;

    protected void onCreate(Bundle allthethings) {
        super.onCreate(allthethings);
        setContentView(R.layout.activity_main);

        switcher = (Button) findViewById(R.id.switcher);
        A1 = (Button) findViewById(R.id.buttonA1);
        A2 = (Button) findViewById(R.id.buttonA2);
        B1 = (Button) findViewById(R.id.buttonB1);
        B2 = (Button) findViewById(R.id.buttonB2);


        switcher.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (buttons1AreVisible) {
                    A1.setVisibility(View.GONE);
                    B1.setVisibility(View.GONE);
                    B2.setVisibility(View.VISIBLE);
                    A2.setVisibility(View.VISIBLE);
                } else {
                    A1.setVisibility(View.VISIBLE);
                    B1.setVisibility(View.VISIBLE);
                    B2.setVisibility(View.GONE);
                    A2.setVisibility(View.GONE);
                }

                buttons1AreVisible = !buttons1AreVisible;

            }
        });
    }


}

Upvotes: 2

gobernador
gobernador

Reputation: 5739

You could try a sneaky workaround using View.setVisibility(). Start with res/layout/main.xml:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:orientation="vertical" >

        <Button
            android:id="@+id/switcher"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Switch"
            android:onClick="switchButtons" />
        <Button
            android:id="@+id/buttonA1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button A" />
        <Button
            android:id="@+id/buttonB1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button B" />
        <Button
            android:id="@+id/buttonB2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button B"
            android:visibility="gone" />
        <Button
            android:id="@+id/buttonA2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button A"
            android:visibility="gone" />

</LinearLayout>

And now we have an Activity to put this layout in:

public class Foo extends Activity {

    Button switcher;
    Button A1;
    Button A2;
    Button B1;
    Button B2;
    boolean buttons1AreVisible = true;

    protected void onCreate(Bundle allthethings) {
        super.onCreate(allthethings);
        setContentView(R.layout.main);

        switcher = findViewById(R.id.switcher);
        A1 = findViewById(R.id.buttonA1);
        A2 = findViewById(R.id.buttonA2);
        B1 = findViewById(R.id.buttonB1);
        B2 = findViewById(R.id.buttonB2);
    }

    private void switchButtons() {
        if (buttons1AreVisible) {
            A1.setVisibility(View.GONE);
            B1.setVisibility(View.GONE);
            B2.setVisibility(View.VISIBLE);
            A2.setVisibility(View.VISIBLE);
        } else {
            A1.setVisibility(View.VISIBLE);
            B1.setVisibility(View.VISIBLE);
            B2.setVisibility(View.GONE);
            A2.setVisibility(View.GONE);
        }

        buttons1AreVisible = !buttons1AreVisible;
    }
}

All that remains is for you to assign onClick() methods to the four buttons.

Upvotes: 0

Lalit Kumar Sahoo
Lalit Kumar Sahoo

Reputation: 375

You can do the following:

On clicking
1. Change the label of the Button dynamically.
2. Track the button click event by button label.

For Example :
1. You have two buttons labeled as "Button 1" & "Button 2".
2. On Click of "Button 1" -> Change the button label as "Button 1" to "Button 2" And "Button 2" to "Button 1".
3. Similar in case of on clicking "Button 2".
4. In OnClick Method track the button click event through label ("Button 1" or "Button 2")

Upvotes: 0

Lalit Kumar Sahoo
Lalit Kumar Sahoo

Reputation: 375

ToggleButton available in Android since API Level 1.

Below link provides the information about the use of various button in Android

http://android-pro.blogspot.in/2010/03/android-button-controls.html

I think the above link is helpful for you.

If not please provide snaps in order to get more clarity on the question.

Upvotes: 0

Related Questions