I need a dynamic gridlayout
that can be toggled between 3 by 3 and 4 by 4. I can setRowCount
and setColumnCount
from 3 to 4 but not from 4 to 3. It will display following issue:
Caused by: java.lang.IllegalArgumentException: rowCount must be greater than or equal to the maximum of all grid indices (and spans) defined in the LayoutParams of each child.
Is there any work around to achieve this using gridlayout?
GridLayout is also not deprecated.
While you can't make components span multiple rows with GridLayout, you can resize them all uniformly. Put your GridLayout on its own JPanel, and then you can use panel. setSize(x,y) to change the panel size and thus increase or decrease the size of the cells.
Let us not get confused with GridView and GridLayout to be the same. GridView simply gives us a two-dimensional view to display the items on the screen, under ViewGroup. On the other hand, GridLayout is a layout manager that arranges the views in a grid.
getSize(size); int width = size. x; This code creates a GridLayout within the current context and sets its number of rows to four and its number of columns to two.
Based on Teun Kooijman answer you can just change Spec in GridLayout.LayoutParams and keep all Views inside the GridLayout:
private void changeColumnCount(int columnCount) {
if (gridLayout.getColumnCount() != columnCount) {
final int viewsCount = gridLayout.getChildCount();
for (int i = 0; i < viewsCount; i++) {
View view = gridLayout.getChildAt(i);
//new GridLayout.LayoutParams created with Spec.UNSPECIFIED
//which are package visible
view.setLayoutParams(new GridLayout.LayoutParams());
}
gridLayout.setColumnCount(columnCount);
}
}
You can also change Spec in other way by accessing GridLayout.LayoutParams.rowSpec and GridLayout.LayoutParams.columnSpec
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With