I understand that this question is quite old, but for people who still encounter this exception today, I will provide an explanation that may shed light on how GridLayout is getting shorter and why I think it / throws an exception for OP .
In short:
GridLayout views can, after reduction, occupy cells that are not part of the GridLayout grid, which raises the IllegalArgumentException mentioned by OP. To avoid this, remove the child views that will occupy cells outside the GridLayout grid until the actual call to setRowCount() or setColumnCount() . This can be done using GridLayout.removeView(aboutToBeIllegalChild); or by clearing the entire layout using GridLayout.removeAllViews(); .
In the long:
Everything that calls the GridLayout.setRowCount() call sets the new number of rows that the layout should contain. However, it does not interfere with the child views that GridLayout currently contains, and does not specify Spec (which columns and rows the child view occupies).
Which exception basically tells us and the documents confirm, is that GridLayout does not allow any of its child views to occupy cells that are outside the GridLayout grid. As an example, the layout will not allow the child view to occupy the cell (5, 1) when the grid is only 4 x 1 .
This leads us to why the original poster was successful at dynamically increasing the size of the GridLayout , but unsuccessfully at decreasing it. If you increase the size, any child views that were already bound to the GridLayout with the specified cells will still be placed in the left cells if the grid receives additional rows or columns dynamically. When mesh sizes are reduced, children's views that were placed in cells that would disappear as a result of deleting rows or columns will now be considered illegal.
To get around this, you must either first remove those (child) child views from your parent GridLayout by calling GridLayout.removeView(aboutToBeIllegalChild); , or simply erase all GridLayout by calling GridLayout.removeAllViews(); .
Hope this helps!
source share