Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GridLayout(not GridView) - Spaces between the cells

I am using GridLayout(support) for displaying ImageViews in my application. There are 3 columns and 5 rows. The problem is that the cells in the GridLayout automatically get some space between them. I am not setting any padding or margin for the cells. Please refer to the image below. All cells are added dynamically and here is how I add these cells.

Getting Screen Width and Height:

Point size = new Point();
getWindowManager().getDefaultDisplay().getSize(size);
screenWidth = size.x;
screenHeight = size.y;
rowHeight = (int) (screenHeight * 0.2);

Adding View to GridLayout:

    GridLayout.LayoutParams params = new GridLayout.LayoutParams(
            getSpec(rowColumn[0]), getSpec(rowColumn[1]));

    params.height = rowHeight;

    if (rowColumn[1].equalsIgnoreCase("col_full")) {
        params.width = (int) (screenWidth);
    } else if (rowColumn[1].equalsIgnoreCase("col_two_part")) {
        params.width = (int) (screenWidth * 0.6);
    } else {
        params.width = (int) (screenWidth * 0.3);
    }

    ImageButton image = (ImageButton) imageHashMap
            .get(reOrderedButtonsListCopy.get(i));
    image.setLayoutParams(params);

    gridLayout.addView(image, params);

XML Layout:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.xx.xxx"
    android:id="@+id/scrollView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <android.support.v7.widget.GridLayout
            xmlns:app="http://schemas.android.com/apk/res/com.xx.xxx"
            android:id="@+id/gridlayout_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            app:columnCount="3"
            app:rowCount="5" >
        </android.support.v7.widget.GridLayout>

    </LinearLayout>

</ScrollView>

Current result:

enter image description here

The red lines show the spaces in between the cells. Also, there is some space on the left side of GridLayout. I have only given 2dp as layout_margin. Any reasons why this padding occurs?

[EDIT]

Making the following changes removed the spacings.

gridLayout = (GridLayout) findViewById(R.id.gridlayout_main);
gridLayout.setUseDefaultMargins(false);
gridLayout.setAlignmentMode(GridLayout.ALIGN_BOUNDS);
gridLayout.setRowOrderPreserved(false);

Refer to the image below.

enter image description here

like image 965
Vamsi Challa Avatar asked Jan 30 '14 11:01

Vamsi Challa


People also ask

What is the difference between GridLayout and GridView?

Let us not get confused with GridView and GridLayout to be the same. GridView simply gives us a two-dimensional view to display the items on the screen, under ViewGroup. On the other hand, GridLayout is a layout manager that arranges the views in a grid.

How do you give a space in grid layout?

The grid-gap property defines the size of the gap between the rows and columns in a grid layout, and is a shorthand property for the following properties: grid-row-gap. grid-column-gap.

How do I set GridLayoutManager spacing?

In your source code, add ItemOffsetDecoration to your recyclerview. Item offset value should be half size of the actual value you want to add as space between items. mRecyclerView. setLayoutManager(new GridLayoutManager(context, NUM_COLUMNS); ItemOffsetDecoration itemDecoration = new ItemOffsetDecoration(context, R.

How do Gridlayouts differ from Tablelayouts?

Both layout options can create similar looking tables, but each using a different approach. With the TableLayout , rows and columns are added dynamically as you build the table. With the GridLayout , row and column sizes are defined in the layout definition.


1 Answers

Found the solution.

Making the following changes removed the spacings.

gridLayout = (GridLayout) findViewById(R.id.gridlayout_main);
gridLayout.setUseDefaultMargins(false);
gridLayout.setAlignmentMode(GridLayout.ALIGN_BOUNDS);
gridLayout.setRowOrderPreserved(false);

Refer to the image below.

enter image description here

like image 163
Vamsi Challa Avatar answered Oct 07 '22 19:10

Vamsi Challa