GridLayout. How set space between columns?
Asked Answered
M

4

10

Android Studio 3.1, Java 1.8, Gradle 4.1

I use GridLayout. All work fine. But I need to set space (e.g. 10dp) between columns and space between rows. How I can do this?

main.xml:

            <GridLayout
                android:id="@+id/categoriesContainer"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:verticalSpacing="10dp"
                app:layout_constraintEnd_toEndOf="@+id/birthDateContainer"
                app:layout_constraintStart_toStartOf="@+id/birthDateContainer"
                app:layout_constraintTop_toBottomOf="@+id/birthDateContainer">


            </GridLayout>

profile_category_active.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <android.support.constraint.ConstraintLayout
        android:id="@+id/profileCategoryContainer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">       


        <TextView
            android:id="@+id/categoryNameTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginEnd="30dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="30dp"
            android:layout_marginStart="5dp"
            android:layout_marginTop="5dp"
            android:ellipsize="end"
            android:maxLines="1"
            android:text="TextView"                />
    </android.support.constraint.ConstraintLayout>
</layout>

Activity: I add row programatically:

 GridLayout gridLayout = findViewById(R.id.categoriesContainer);
    gridLayout.setColumnCount(columnCount);
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    for (int index = 0; index < 10; index++) {
     View profileCategoryActive = inflater.inflate(R.layout.profile_category_active, null, false);
            categoriesGridContainer.addView(profileCategoryActive);
            ConstraintLayout profileCategoryContainer = profileCategoryActive.findViewById(R.id.profileCategoryContainer);
            ViewGroup.LayoutParams profileCategoryContaineParams = profileCategoryContainer.getLayoutParams();
            profileCategoryContaineParams.width = (int) AndroidUtil.dpToPx(this, categoryItemWidth);
            profileCategoryContainer.setLayoutParams(profileCategoryContaineParams);
            TextView categoryNameTextView = profileCategoryActive.findViewById(R.id.categoryNameTextView);
            categoryNameTextView.setText("Ind " + profileCategoryContaineParams.width);
}
Migratory answered 21/12, 2017 at 7:38 Comment(4)
Possible duplicate #21455995Flick
And also here #35270904Flick
what is the parent layout of your main.xml ?Deandeana
Parent is ConstraintLayoutMigratory
P
20

In xml GridLayout, add:

android:useDefaultMargins="true"

and you get a distance between columns and rows.

Persecute answered 21/11, 2018 at 22:48 Comment(3)
Getting on android:useDefaultMargins="true"Saudra
can you configure the distance that this adds?Caracaraballo
You cannot configure this value. It's set by the platform.Bulgarian
C
2

This is how I achieved it, hopefully this helps somebody;

<GridLayout
    android:orientation="horizontal"
    android:columnCount="3"
    android:layout_marginEnd="-10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:background="@color/colorBlack"
        android:textColor="@color/colorWhite"
        android:text="1"
        android:layout_marginEnd="10dp"
        android:layout_marginBottom="10dp"
        android:layout_columnWeight="1"
        android:layout_height="wrap_content"
        android:padding="15dp" />

    <TextView
        android:background="@color/colorBlack"
        android:textColor="@color/colorWhite"
        android:text="2"
        android:layout_marginEnd="10dp"
        android:layout_marginBottom="10dp"
        android:layout_columnWeight="1"
        android:layout_height="wrap_content"
        android:padding="15dp" />

    <TextView
        android:background="@color/colorBlack"
        android:textColor="@color/colorWhite"
        android:text="3"
        android:layout_marginEnd="10dp"
        android:layout_marginBottom="10dp"
        android:layout_columnWeight="1"
        android:layout_height="wrap_content"
        android:padding="15dp" />

    <TextView
        android:background="@color/colorBlack"
        android:textColor="@color/colorWhite"
        android:text="4"
        android:layout_marginEnd="10dp"
        android:layout_marginBottom="10dp"
        android:layout_columnWeight="1"
        android:layout_height="wrap_content"
        android:padding="15dp" />

</GridLayout>

Essentially what it's doing is setting a margin on each item (10dp) to space them inside the GridLayout and then it's shifting the GridLayout by using a negative margin (-10dp) to compensate for the additional width. Giving the following result;

How the GridLayout renders

Carbamidine answered 1/4, 2020 at 15:14 Comment(2)
Saved my day because of your idea ! Thank youIngrowth
Glad I could help someone @Ingrowth :)Carbamidine
K
1

Dynamic Views in GridLayout

I had a slightly different problem. The Views I needed to show in the GridLayout could not be determined ahead of time (i.e. can't create them in XML). I needed to create them programmatically based on info loaded from a database.

for (socialPlatform in platformsWithNoAccount) {
    val socialIcon = ImageView(requireContext())
    socialIcon.setImageDrawable(ImageUtils.getSocialIcon(socialPlatform, false))        
    binding.addSocialLayout.addView(socialIcon)
    (socialIcon.layoutParams as? GridLayout.LayoutParams)?.marginEnd = 48
}

The key is that last line:

(socialIcon.layoutParams as? GridLayout.LayoutParams)?.marginEnd = 48

After the View has been added to the GridLayout, it should have a GridLayout.LayoutParams object that you can alter in code by setting marginStart, marginEnd, etc.

Khz answered 3/9, 2021 at 13:4 Comment(0)
M
-1

I found solution:

  1. Use android.support.v7.widget.GridLayout
  2. Here programatically set margin and full size horizontally

O activity:

            View profileCategoryActive = inflater.inflate(R.layout.profile_category_active, null, false);
            categoriesGridContainer.addView(profileCategoryActive);

            // set ndroid:layout_columnWeight="1" programatically
            GridLayout.LayoutParams params = new GridLayout.LayoutParams(GridLayout.spec(
                    GridLayout.UNDEFINED, GridLayout.FILL, 1f),
                    GridLayout.spec(GridLayout.UNDEFINED, GridLayout.FILL, 1f));

            params.width = (int) AndroidUtil.dpToPx(this, categoryItemWidth);
            params.bottomMargin = (int) tilePreviewLeftRightMarginDp;
            params.topMargin = (int) tilePreviewLeftRightMarginDp;
            params.rightMargin = (int) tilePreviewLeftRightMarginDp;
            params.leftMargin = (int) tilePreviewLeftRightMarginDp;
            ConstraintLayout profileCategoryContainer = profileCategoryActive.findViewById(R.id.profileCategoryContainer);
            profileCategoryContainer.setLayoutParams(params);
            TextView categoryNameTextView = profileCategoryActive.findViewById(R.id.categoryNameTextView);
            categoryNameTextView.setText("Ind " + params.width);
Migratory answered 21/12, 2017 at 15:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.