Like moving two buttons around?

Asked

Viewed 87 times

2

I’m developing an Android app that’s basically a game. In one of the Activities there are some buttons, and I wanted that when an event occurred (including the trigger of another button) two buttons change position in the layout. I researched the documentation of the Transition class to see if it was possible to use an animation to make this change, but I really wanted something with a non-existent delay.

NOTE: Just changing the appearance of the buttons are not options for me, since each of these buttons have different functions within the application and I need to keep them.

<LinearLayout android:id="@+id/lytKeyboard"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginStart="15dp"
        android:layout_marginEnd="15dp"
        android:orientation="vertical">

        <LinearLayout android:id="@+id/lytKeyboardLine1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button android:id="@+id/btn1"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:text="1"
                android:textSize="50sp"/>

            <Button android:id="@+id/btn2"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:text="2"
                android:textSize="50sp"/>

            <Button android:id="@+id/btn3"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:text="3"
                android:textSize="50sp"/>
        </LinearLayout>

        <LinearLayout android:id="@+id/lytKeyboardLine2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button android:id="@+id/btn4"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:text="4"
                android:textSize="50sp"/>

            <Button android:id="@+id/btn5"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:text="5"
                android:textSize="50sp"/>

            <Button android:id="@+id/btn6"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:text="6"
                android:textSize="50sp"/>
        </LinearLayout>

        <LinearLayout android:id="@+id/lytKeyboardLine3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button android:id="@+id/btn7"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:text="7"
                android:textSize="50sp"/>

            <Button android:id="@+id/btn8"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:text="8"
                android:textSize="50sp"/>

            <Button android:id="@+id/btn9"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:text="9"
                android:textSize="50sp"/>
        </LinearLayout>

        <Button android:id="@+id/btn0"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:text="0"
            android:textSize="50sp"/>
    </LinearLayout>
  • Ask the layout xml question. When it says swap refers to the button 1 to the place of button 2 and button 2 to the place of button 1?

  • Yes, exactly

1 answer

2


Use a Gridlayout to deploy the buttons.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/lytKeyboard"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginStart="15dp"
    android:layout_marginEnd="15dp"
    android:orientation="vertical">

    <GridLayout android:id="@+id/lytKeyboardGrid"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:columnCount="3"
        android:rowCount="3"
        android:orientation="horizontal">

        <!--linha 1-->
        <Button android:id="@+id/btn1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="1"
            android:textSize="50sp"
            android:layout_row="0"
            android:layout_column="0"/>

        <Button android:id="@+id/btn2"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="2"
            android:textSize="50sp"
            android:layout_row="0"
            android:layout_column="1"/>

        <Button android:id="@+id/btn3"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="3"
            android:textSize="50sp"
            android:layout_row="0"
            android:layout_column="2"/>

        <!--linha 2-->
        <Button android:id="@+id/btn4"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="4"
            android:textSize="50sp"
            android:layout_row="1"
            android:layout_column="0"/>

        <Button android:id="@+id/btn5"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="5"
            android:textSize="50sp"
            android:layout_row="1"
            android:layout_column="1"/>

        <Button android:id="@+id/btn6"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="6"
            android:textSize="50sp"
            android:layout_row="1"
            android:layout_column="2"/>

        <!--linha 3-->
        <Button android:id="@+id/btn7"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="7"
            android:textSize="50sp"
            android:layout_row="2"
            android:layout_column="0"/>

        <Button android:id="@+id/btn8"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="8"
            android:textSize="50sp"
            android:layout_row="2"
            android:layout_column="1"/>

        <Button android:id="@+id/btn9"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="9"
            android:textSize="50sp"
            android:layout_row="2"
            android:layout_column="2"/>
    </GridLayout>

    <Button android:id="@+id/btn0"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:text="0"
        android:textSize="50sp"
        android:onClick="onClickBtn0"/>
</LinearLayout>

If it has been specified, in the parameters of each button, the row and column they occupy in the grid, just change these parameters.

private void swapButtons(@IdRes int firstButtonId, @IdRes int secondButtonId) {

    Button firstButton;
    Button secondButton;
    try {
        firstButton = findViewById(firstButtonId);
        secondButton = findViewById(secondButtonId);

    }catch(ClassCastException e){
        throw new IllegalArgumentException(
                "Um ou os dois argumentos não são ID's do tipo Button", e);
    }

    GridLayout firstButtonParent;
    GridLayout secondButtonParent;
    try {
        firstButtonParent = (GridLayout) firstButton.getParent();
        secondButtonParent = (GridLayout) secondButton.getParent();
    }catch (ClassCastException e){
        throw new UnsupportedOperationException(
                "Um ou os dois botões não estão num GridLayout", e);
    }

    if(firstButtonParent != secondButtonParent){
        throw new UnsupportedOperationException(
            "Os dois botões não estão no mesmo GridLayout");
    }

    GridLayout.LayoutParams firstButtonParameters =
            (GridLayout.LayoutParams) firstButton.getLayoutParams();
    GridLayout.LayoutParams secondButtonParameters =
            (GridLayout.LayoutParams) secondButton.getLayoutParams();

    firstButton.setLayoutParams(secondButtonParameters);
    secondButton.setLayoutParams(firstButtonParameters);
}

Example of use where the button Btn0, when clicked, change places btn2and btn5:

public void onClickBtn0(View view) {
    swapButtons(R.id.btn2, R.id.btn5);
}
  • That’s what I wanted, thank you. In fact, the way I planned the 0 button could also be replaced with another button, but just put it inside the Gridlayout and I believe it will work.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.