I just followed this tutorial to create a custom View as a GridLayout element.
What's my CustomView
public class RowView extends View{ boolean touchOn; boolean mDownTouch = false; private OnToggledListener toggledListener; int _IdRow = 0; int _IdColumn = 0; public RowView(Context context, int Rows, int Columns) { super(context); this._IdRow = Rows; this._IdColumn = Columns; init(); } public RowView(Context context) { super(context); init(); } public RowView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public RowView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { touchOn = false; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec)); } @Override protected void onDraw(Canvas canvas) { if (touchOn) { canvas.drawColor(Color.RED); } else { canvas.drawColor(Color.GRAY); } }
In this class, I can detect when the user clicks on the GridLayout element and changes it to a different color, which is good. But the problem arises at the time to create this:
This is my MainActivity , where I show the GridLayout :
int numOfCol = mGridLayout.getColumnCount(); int numOfRow = mGridLayout.getRowCount(); mRowViews = new RowView[numOfCol*numOfRow]; for(int yPos=0; yPos<numOfRow; yPos++){ for(int xPos=0; xPos<numOfCol; xPos++){ RowView tView = new RowView(this, xPos, yPos); tView.setOnToggledListener(this); mRowViews[yPos*numOfCol + xPos] = tView; mGridLayout.addView(tView); } } mGridLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){ @Override public void onGlobalLayout() { final int MARGIN = 5; int pWidth = mGridLayout.getWidth(); int pHeight = mGridLayout.getHeight(); int numOfCol = mGridLayout.getColumnCount(); int numOfRow = mGridLayout.getRowCount(); int w = pWidth/numOfCol; int h = pHeight/numOfRow; for(int yPos=0; yPos<numOfRow; yPos++){ for(int xPos=0; xPos<numOfCol; xPos++){ GridLayout.LayoutParams params = (GridLayout.LayoutParams)mRowViews[yPos*numOfCol + xPos].getLayoutParams(); params.width = w - 2*MARGIN; params.height = h - 2*MARGIN; params.setMargins(MARGIN, MARGIN, MARGIN, MARGIN); mRowViews[yPos*numOfCol + xPos].setLayoutParams(params); } } }});
There is also an Interface OnToggledListener method that gives me the row and column of my GridLayout when an element is clicked on it:
@Override public void OnToggled(MyView v, boolean touchOn) {
I would like to avoid creating this mGridLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() because it fills things on the screen that I don't need ... I tried putting GridLayout 6x6 with android:layout_height="400dp" and it shows only 3x3 and this is this LogCat message
D / android.widget.GridLayout: vertical limits: y6-y0> = 1749, y6-y5 <= 291, y5-y4 <= 291, y4-y3 <= 291, y3-y2 <= 291, y2 -y1 < = 291, y1-y0 <= 291 are inconsistent; permanent delete: y6-y5 <= 291.
I would like to do something like GridLayout[row][colum] to get the background color and then do something, but I cannot find this solution.