Programmatically adding TextView to Grid Layout alignment not proper
Asked Answered
M

5

9

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);
}
Messalina answered 29/2, 2016 at 6:10 Comment(0)
V
11

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);

} 
Valladolid answered 29/2, 2016 at 6:17 Comment(4)
what is c and r here?Messalina
c is column index and r is row index of GridLayoutCatabasis
Column and row indices start at 0, if anybody is wondering as well.Northumbria
this might be helpful to someone. to place grid item with equal width and height param.columnSpec = GridLayout.spec(GridLayout.UNDEFINED, 1f) param.rowSpec = GridLayout.spec(GridLayout.UNDEFINED, 1f)Centistere
L
4

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);
} 
Lauber answered 27/12, 2018 at 16:50 Comment(0)
V
2

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);
    } 
Valladolid answered 29/2, 2016 at 6:54 Comment(0)
V
0

That's may be because of your dynamic text length is not fixed so each textview not take same space Check this

int height=getContext().getResources().getDimension(R.dimen.activity_horizontal_margin); //set size of dimen in required resolution

titleText .setLayoutParams(new LinearLayout.LayoutParams(0, height, height));

Varden answered 29/2, 2016 at 6:25 Comment(2)
text can be at most 15 char maxMessalina
You can set min width for textview,or fix the textview width is the easiest way if you have just 15char length in textviewVarden
C
0

My solution:

GridLayout gl = findViewById( R.id.grid_layout );
TextView tv = new TextView( this );
tv.setText( ""+cursor.getInt( column ) ); //for example
GridLayout.LayoutParams lp = new GridLayout.LayoutParams();
lp.columnSpec = GridLayout.spec( GridLayout.UNDEFINED, 1, GridLayout.FILL ); //for stretch a child to column use GridLayout.FILL
gl.addView( tv, lp );
//DO NOT USE lp.setGravity( ... );
//FOR ALIGN TEXT USE tv.setTextAlignment( ... );
Charlinecharlock answered 12/12, 2022 at 11:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.