Search code examples
androidgrid-layoutgravity

Center item in GridLayout


I'm using a Gridlayout which is made in xml with a couple headers in the first line. Now I want to add new rows with items dynamical to the grid. My Problem is the items are not centered in the cell and doesn't match with the headers.

This is the Grid in the xml

<GridLayout
            android:id="@+id/newGrid"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:columnCount="5"
            android:rowCount="4">
            <!-- Grid Header -->
            <Space
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_columnWeight="1"
                android:layout_gravity="center"
                android:text="@string/cellNo" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_columnWeight="1"
                android:layout_gravity="center"
                android:text="@string/desity" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_columnWeight="1"
                android:layout_gravity="center"
                android:text="@string/voltage" />

            <android.support.v7.widget.AppCompatImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:src="@drawable/problemIcon" />

</GridLayout>

This is for example how I try to add a TextView in the second row in the second cell.

    ...
    TextView cellnum = new TextView(getContext());
    cellnum.setGravity(Gravity.CENTER);
    cellnum.setText("9");
    ...
    gridLayout.addView(cellnum);

The text seems to be in the right cell but its not centered right under the headline

This is the xml verison which i tried frist and worked perfect but I need to build it dynamicaly

               <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="9" />

Solution

  • You need to set layout_gravity instead of gravity of textView to make it appear in the centre.

    You are setting layout_gravity in the xml that is why it works, but dynamically you are only setting gravity.

    XmlFile:

    <?xml version="1.0" encoding="utf-8"?>
    <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/newGrid"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:columnCount="5"
    android:rowCount="4">
    <!-- Grid Header -->
    <Space
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnWeight="1"
        android:layout_gravity="center" />
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnWeight="1"
        android:layout_gravity="center"
        android:text="cell number" />
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnWeight="1"
        android:layout_gravity="center"
        android:text="destiny" />
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnWeight="1"
        android:layout_gravity="center"
        android:text="voltage" />
    
    <android.support.v7.widget.AppCompatImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnWeight="1"
        android:layout_gravity="center" />
    

    Activity Code

    public class MainActivity extends AppCompatActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        GridLayout gridLayout = (GridLayout) findViewById(R.id.newGrid);
    
        //Adding new textView at row 1 column 0
        TextView cellNum = new TextView(this);
        cellNum.setText("9");
        gridLayout.addView(cellNum);
    
        GridLayout.LayoutParams params = new GridLayout.LayoutParams(cellNum.getLayoutParams());
        params.setGravity(Gravity.CENTER);
        cellNum.setLayoutParams(params);
    
        //Adding new textView at row 1 column 1
        TextView cellNum1 = new TextView(this);
        cellNum1.setText("10");
        gridLayout.addView(cellNum1);
    
        GridLayout.LayoutParams params1 = new GridLayout.LayoutParams(cellNum1.getLayoutParams());
        params1.setGravity(Gravity.CENTER);
        cellNum1.setLayoutParams(params1);
    
     }
    }