I would like to use GridLayout (not GridView ) as a board for a game such as chess or checkers. Since I am a little reluctant to use the xml file with 64 child View s, I tried to add them programmatically.
To keep things simple, I started by using TextView as a child View for GridLayout .
My problem is that the View not distributed evenly and that I do not know how to get even distribution in my java code. There is no "setWeight ()" method to set layout_columnWeight and layout_rowWeight .
This is currently my activity_dynamic_grid_layout.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="match_parent" android:layout_height="80dp" android:id="@+id/ivLogo" android:background="#ff0000" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true"/> <android.support.v7.widget.GridLayout android:id="@+id/grid_layout" android:background="#004080" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/ivLogo" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginTop="36dp"> </android.support.v7.widget.GridLayout>
I set the width and height of the GridLayout to match_parent here, but I change them at runtime using ViewTreeObserver.OnPreDrawListener to get a square board. It works, a colored background shows a square space as intended.
My onCreate ()
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_dynamic_grid_layout); GridLayout gl = (GridLayout) findViewById(R.id.grid_layout); gl.setColumnCount(8); gl.setRowCount(8); for(int i=0; i<gl.getRowCount(); i++) { GridLayout.Spec rowSpec = GridLayout.spec(i, 1, GridLayout.FILL); for(int j=0;j<gl.getColumnCount();j++) { GridLayout.Spec colSpec = GridLayout.spec(j,1, GridLayout.FILL); TextView tvChild = new TextView(this); tvChild.setText("[ " + i + " | " + j + " ]"); tvChild.setTextSize(18f); tvChild.setTextColor(Color.WHITE); tvChild.setGravity(Gravity.CENTER); GridLayout.LayoutParams myGLP = new GridLayout.LayoutParams(); myGLP.rowSpec = rowSpec; myGLP.columnSpec = colSpec; gl.addView(tvChild, myGLP ); } } final View rootView = findViewById(R.id.dynamic_root); rootView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { @Override public boolean onPreDraw() { int w = rootView.getMeasuredWidth(); int h = rootView.getMeasuredHeight(); int min = Math.min(w, h); ViewGroup.LayoutParams lp = gl.getLayoutParams(); lp.width = min - min % 9; lp.height = lp.width; gl.setLayoutParams(lp); rootView.getViewTreeObserver().removeOnPreDrawListener(this); return true; } }); }
What I already tried:
I put one TextView file in a layout file and tried to copy layout_columnWeight and layout_rowWeight from its GridLayout.LayoutParams :
<android.support.v7.widget.GridLayout ...> <TextView android:id="@+id/clone_my_params" android:text="[ 0 | 0 ]" android:textColor="#ffffff" android:textSize="18sp" app:layout_column="0" app:layout_row="0" app:layout_columnWeight="1" app:layout_rowWeight="1" /> </android.support.v7.widget.GridLayout>
Extra code in onCreate() , before the double loop:
TextView v = (TextView)gl.findViewById(R.id.clone_my_params); v.setGravity(Gravity.CENTER); GridLayout.LayoutParams gridLayoutParamsToCopy = new GridLayout.LayoutParams(v.getLayoutParams());
Inside the loop, I skipped (i, j) = (0,0) and changed
GridLayout.LayoutParams myGLP = new GridLayout.LayoutParams();
to
GridLayout.LayoutParams myGLP = new GridLayout.LayoutParams(gridLayoutParamsToCopy);
Before the change, all the elements were in the upper left corner, excess space was assigned to the last row / column. After the change, the first row / column had excess space, no change for other elements.
Call gl.invalidate() and / or gl.requestLayout() after the double loop has not been affected.
It seems to me that I was not able to set the desired weight using the copy constructor.