Home > Enterprise >  How to swap elements in a Grid Layout Android
How to swap elements in a Grid Layout Android

Time:03-11

<GridLayout
            android:id="@ id/tools_grid_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:columnCount="4"
            android:rowCount="3"
            android:alignmentMode="alignBounds"
            android:background="#AAAAAA">

            <ImageButton
                android:id="@ id/button_undo"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_columnWeight="1"
                android:src="@drawable/undo_icon"
                android:text="Undo" />
......
</GridLayout>

Basically, my goal is to be able to swap button positions inside a grid layout while maintaining the size of other buttons.

I tried to swap elements of GridLayout using getX() and getY() on each element. But then I realized that this would affect the size of other buttons I have in the grid layout because the size of each button is dependent on its weight. Is there any other way I can directly swap elements inside a gridLayout?

Any help would be very helpful. Thank you

CodePudding user response:

You can try this.

private void swap(ViewGroup parent, int resourceId1, int resourceId2)
{
    final int children = parent.getChildCount();
    if(children == 0) return;
        
    View view1=null, view2=null;
    int id, count=0, index1=0, index2=0;
    for(int i=0; i<children;   i) {
        final View view = parent.getChildAt(i);
        id = view.getId();
        if(id == resourceId1) {
            view1 = view;
            index1 = i;
              count;
        } else if(id == resourceId2) {
            view2 = view;
            index2 = i;
              count;
        }
        if(count == 2) break;
    }
    if(count == 2) {
        if(index1 < index2) {
            parent.removeViewAt(index2);
            parent.removeViewAt(index1);
            parent.addView(view2, index1);
            parent.addView(view1, index2);
        } else {
            parent.removeViewAt(index1);
            parent.removeViewAt(index2);
            parent.addView(view1, index2);
            parent.addView(view2, index1);
        }
    }
}

How to use it:

GridLayout gl = findViewById(R.id.tools_grid_layout);
swap(gl, R.id.btn_undo, R.id.btn_redo);
// Or this (doesn't matter which one first).
// swap(gl, R.id.btn_redo, R.id.btn_undo);

CodePudding user response:

I have found a workaround for this but if you have a better solution please let me know. I saved references to all the buttons in an ArrayList Then deleted all the elements inside GridLayout with

toolGridLayout.removeAllViews();

Then I swapped buttons using my ArrayList however I need to. Finally I used toolGridLayout.addView(element) function to add the buttons inside my arraylist.

  • Related