Programmatically adding TextView to Grid Layout alignment not proper

29,606

Solution 1

For this you have to dynamically set the column width for the views. This will finally align each view properly with equal amount of space.

GridLayout gridLayout = new GridLayout(getContext());
            gridLayout.setAlignmentMode(GridLayout.ALIGN_BOUNDS);
            gridLayout.setColumnCount(2);
            gridLayout.setRowCount(3);
            TextView titleText;
            for (int i = 0; facilities != null && i < facilities.size(); i++) {
                titleText = new TextView(getContext());
                titleText.setText(facilities.get(i));
                gridLayout.addView(titleText, i);
                titleText.setCompoundDrawablesWithIntrinsicBounds(rightIc, 0, 0, 0);
                GridLayout.LayoutParams param =new GridLayout.LayoutParams();
                param.height = LayoutParams.WRAP_CONTENT;
                param.width = LayoutParams.WRAP_CONTENT;
                param.rightMargin = 5;
                param.topMargin = 5;
                param.setGravity(Gravity.CENTER);
                param.columnSpec = GridLayout.spec(c);
                param.rowSpec = GridLayout.spec(r);
                titleText.setLayoutParams (param);

} 

Solution 2

The following code sample should give each text view equal height and width, and order the TextViews left-to-right and then top-to-bottom.

The critical part is explicitly providing the GridLayout.LayoutParams, setting height/width to 0 and defining the row/column specs with weights set to 1 so that the height and width will be automatically calculated based on the weights.

Also notice I set the number of rows as a function of the number of facilities, so that if your list grows you'll have more rows.

if (facilities == null) {
    // In this case there is nothing to display. You can adjust this part to your needs.
    return;
}

GridLayout gridLayout = new GridLayout(getContext());
gridLayout.setAlignmentMode(GridLayout.ALIGN_BOUNDS);
gridLayout.setColumnCount(2);
gridLayout.setRowCount(facilities.size() / 2);
for (int i = 0; i < facilities.size(); i++) {
    TextView titleText = new TextView(getContext());
    titleText.setText(facilities.get(i));
    titleText.setCompoundDrawablesWithIntrinsicBounds(rightIc, 0, 0, 0);

    GridLayout.LayoutParams layoutParams = new GridLayout.LayoutParams();
    layoutParams.height = 0;
    layoutParams.width = 0;
    int currentCol = i % 2;
    int currentRow = i / 2;
    // The last parameter in the specs is the weight, which gives equal size to the cells
    layoutParams.columnSpec = GridLayout.spec(currentCol, 1, 1);
    layoutParams.rowSpec = GridLayout.spec(currentRow, 1, 1);

    // Optional, if you want the text to be centered within the cell
    layoutParams.setGravity(Gravity.CENTER);

    gridLayout.addView(titleText, layoutParams);
} 

Solution 3

Basically that is the column and row count. I have re wrotten the complete logic

GridLayout gridLayout = new GridLayout(getContext());
    int total = facilities.size();
    int column =  2;
    int row = total / column;
    gridLayout.setAlignmentMode(GridLayout.ALIGN_BOUNDS);
    gridLayout.setColumnCount(column);
    gridLayout.setRowCount(row + 1);
    TextView titleText;
    for(int i =0, c = 0, r = 0; i < total; i++, c++)
    { 
        if(c == column)
        { 
            c = 0;
            r++;
        } 
         titleText = new TextView(getContext());
         titleText.setText(facilities.get(i));
         gridLayout.addView(titleText, i);
         titleText.setCompoundDrawablesWithIntrinsicBounds(rightIc, 0, 0, 0);
         GridLayout.LayoutParams param =new GridLayout.LayoutParams();
         param.height = LayoutParams.WRAP_CONTENT;
         param.width = LayoutParams.WRAP_CONTENT;
         param.rightMargin = 5;
         param.topMargin = 5;
         param.setGravity(Gravity.CENTER);
         param.columnSpec = GridLayout.spec(c);
         param.rowSpec = GridLayout.spec(r);
         titleText.setLayoutParams (param);
    } 
Share:
29,606
silentsudo
Author by

silentsudo

Software Engineer

Updated on December 28, 2020

Comments

  • silentsudo
    silentsudo over 3 years

    Hi i am trying to add TextView with drawableLeft to GridLayout. I am adding this TextView in an Loop. The TextView are getting added properly but the are not aligned properly. Each textview should take equal width in one horizontal row which is not happening.

    Following is the code i am using

        GridLayout gridLayout = new GridLayout(getContext());
                gridLayout.setAlignmentMode(GridLayout.ALIGN_BOUNDS);
                gridLayout.setColumnCount(2);
                gridLayout.setRowCount(3);
                TextView titleText;
                for (int i = 0; facilities != null && i < facilities.size(); i++) {
                    titleText = new TextView(getContext());
                    titleText.setText(facilities.get(i));
                    gridLayout.addView(titleText, i);
                    titleText.setCompoundDrawablesWithIntrinsicBounds(rightIc, 0, 0, 0);
    }