Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GridLayout. How set space between columns?

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);
}
like image 641
Alex Avatar asked Dec 18 '22 02:12

Alex


1 Answers

In xml GridLayout, add:

android:useDefaultMargins="true"

and you get a distance between columns and rows.

like image 74
Boris Karloff Avatar answered Feb 14 '23 11:02

Boris Karloff