FishFeetSlapper
FishFeetSlapper

Reputation: 1

Adding new TextViews to ConstraintLayout doesn't work onCreate but works afterwards

I'm making a dynamic grid of colored TextViews as to make tiles, since it's dynamic, so I want the gridSize to be able to be changed while it's running, I need to do it programmatically.

The problem is that when I generate the grid's cells in the onCreate method it does nothing [at least this is what I'm guessing since I don't see anything], but when I do it from the onClick attribute of an element in the activity it does it's job [I see the grid perfectly]

This is the onCreate method, the function to create the grid is generateGrid() [I've put the parameters to View v so that I can test wether it doesn't work or it only doesn't work on onCreate]:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(Utils.currentTheme);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.duek48);

        generateGrid(new View(this)); //this doesn't work for some reason
    }

This is the method I've created to generate the grid:

    public void generateGrid(View v) {
        TextView previousCell = null; //to attach the current cell to the previous one
        for (int i = 0; i < gridSize * gridSize; i++) {
            ConstraintLayout grid = findViewById(R.id.grid);
            int gapSize = 16; //this needs improvement: it has to be automatic, proportionally inverse to the grid size
            int cellSize = (grid.getHeight() - (gapSize * (gridSize  + 1))) / gridSize;
            TextView gridBackgroundCell = new TextView(this);
            gridBackgroundCell.setId(View.generateViewId());
            gridBackgroundCell.setText("");
            gridBackgroundCell.setBackgroundTintList(ColorStateList.valueOf(Color.rgb(170, 170, 170)));
            gridBackgroundCell.setBackground(getResources().getDrawable(R.drawable.rounded_view));
            grid.addView(gridBackgroundCell);
            //add constraints
            ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(cellSize, cellSize);
            if (previousCell == null) {
                layoutParams.topMargin = gapSize;
                layoutParams.topToTop = R.id.grid;
                layoutParams.startToStart = R.id.grid;
            } else {
                if (i % gridSize == 0) {
                    layoutParams.topMargin = gapSize;
                    layoutParams.topToBottom = previousCell.getId();
                    layoutParams.startToStart = R.id.grid;
                } else {
                    layoutParams.topMargin = 0;
                    layoutParams.topToTop = previousCell.getId();
                    layoutParams.startToEnd = previousCell.getId();
                }
            }
            layoutParams.leftMargin = gapSize;
            gridBackgroundCell.setLayoutParams(layoutParams);

            grid.updateViewLayout(gridBackgroundCell, layoutParams);

            previousCell = gridBackgroundCell;
        }
    }

I've realized that when I remove ALL the LayoutParams part [I'll quote it just to make it clear] the text is shown so it works perfectly, even from the onCreate method, but when I put it back it doesn't:

    ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(cellSize, cellSize);
    if (previousCell == null) {
        layoutParams.topMargin = gapSize;
        layoutParams.topToTop = R.id.grid;
        layoutParams.startToStart = R.id.grid;
    } else {
        if (i % gridSize == 0) {
            layoutParams.topMargin = gapSize;
            layoutParams.topToBottom = previousCell.getId();
            layoutParams.startToStart = R.id.grid;
        } else {
            layoutParams.topMargin = 0;
            layoutParams.topToTop = previousCell.getId();
            layoutParams.startToEnd = previousCell.getId();
        }
    }
    layoutParams.leftMargin = gapSize;
    gridBackgroundCell.setLayoutParams(layoutParams);
    grid.updateViewLayout(gridBackgroundCell, layoutParams);

It's as if the LayoutParams are having trouble to be set from the onCreate function. If anyone got any suggestions please help :)

Upvotes: 0

Views: 42

Answers (1)

FishFeetSlapper
FishFeetSlapper

Reputation: 1

The problem was that the int cellSize was negative since the method getHeight(), of a view that has a LAYOUT height and not a real height, appears to return a negative value; as a result, I was setting the layout height of each view to a negative number.

Upvotes: 0

Related Questions