How to change two place buttons?

2

I'm developing an Android application that's basically a game. In one of the Activities there are a few buttons, and I wanted that when one event occurred (it could even be the trigger of another button) two buttons changed position in the layout. I searched the Transition class documentation to see if it was possible to use an animation to make this switch, 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>
    
asked by anonymous 01.04.2018 / 18:56

1 answer

2

Use a GridLayout to arrange 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 you have indicated the row and column that occupy the grid in the parameters of each button, just change those 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 Btn0 button, when clicked, changes the btn2 and btn5 :

public void onClickBtn0(View view) {
    swapButtons(R.id.btn2, R.id.btn5);
}
    
05.04.2018 / 15:31