0

I have a dynamic GridView which is supposed to display data fetched from the server. The length of this data is variable. I have set android:numColumns="4". Now, if the length of the data is divisible by 4, then my layout looks like:

Screenshot for Number of cells divisible by 4

which is how it's supposed to be. But, if the length of the data is not divisible by 4 but is even (of the form 4k+2), then the layout looks like:

Screenshot for Number of cells of the form 4k+2

I had a look at this StackOverflow question and a lot of people suggested insertion of dummy elements to trick the GridView into leaving empty space. This seemed like a hack but I did achieve the layout I wanted:

Screenshot for Number of cells of the form 4k+2, centered

Now, the problem arises when the length of the data is odd. The layout I get by default looks like:

Screenshot for Number of cells odd

What I would like is to get something like this:

Screenshot for Number of cells odd

Notice that the previous workaround will not work here since there isn't a complete extra cell where a dummy could be inserted.

One solution I could come up with was to change the number of columns to 7 and replace every alternate cell with a dummy node, except for the last row where the first cell is a dummy too. This gave me something like:

Screenshot for Number of cells odd, with 7 columns

Even after getting rid of the horizontalSpacing, these cells are too far apart, and the code readability has been severely compromised. Now, I am not looking for workarounds where the size of dummy cells could be reduced. Instead, I would like to get rid of the dummy cells as well because that's not an efficient way to do this, in my opinion.

I have gone through the documentation for GridView where I could not find anything related to my problem. I have also looked at this and this StackOverflow questions in addition to the one linked above but none of them seem to have a valid answer. I also found this GitHub repository where the author claims to have fixed this, but I could not reproduce it.

I would be happy to provide code snippets from my project, but I don't think they'll be useful because:

  • I'm looking for a generic solution, not specific to my particular case.
  • My project is not as simple as placing alphabets in a GridView and it includes a lot of extra details which are not relevant to this problem and hence have been removed in order to avoid confusion.
Sajal Narang
  • 397
  • 1
  • 14

1 Answers1

0

Below linear layout file dynamically changes like the grid layout. You can try this when grid layout is not supported for your requirement. You can find the pattern and create any kind of alignment with this weightSum and layout_weight attribute which are dynamically changing according to android devices.

**I removed some android:layout_width="match_parent" android:layout_height="match_parent" attributes in this code because maximum word count exceeds. please put this two attribute in all missing places in linear layout tags.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

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

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

            <!-- first row -->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:weightSum="4">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal"
                    android:layout_weight="3">

                    <!-- 1st column -->

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_gravity="center"
                        android:gravity="center"
                        android:text="A"/>

                </LinearLayout>
                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_weight="1"
                    android:weightSum="3">

                    <LinearLayout
                        android:orientation="horizontal"
                        android:layout_weight="2">

                        <!-- second column -->

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_gravity="center"
                            android:gravity="center"
                            android:text="B"/>

                    </LinearLayout>
                    <LinearLayout
                        android:orientation="horizontal"
                        android:layout_weight="1"
                        android:weightSum="2">

                        <LinearLayout
                            android:orientation="horizontal"
                            android:layout_weight="1">

                            <!-- third column -->

                            <TextView
                                android:layout_gravity="center"
                                android:gravity="center"
                                android:text="C"/>

                        </LinearLayout>

                        <LinearLayout

                            android:orientation="horizontal"
                            android:layout_weight="1">

                            <!-- fourth column -->
                            <TextView

                                android:layout_gravity="center"
                                android:gravity="center"
                                android:text="D"/>

                        </LinearLayout>

                    </LinearLayout>

                </LinearLayout>

            </LinearLayout>

        </LinearLayout>

        <LinearLayout

            android:orientation="vertical"
            android:layout_weight="1"
            android:weightSum="5">

            <LinearLayout

                android:orientation="vertical"
                android:layout_weight="4">

                <!-- second row -->

                <LinearLayout

                    android:orientation="horizontal"
                    android:weightSum="4">

                    <LinearLayout

                        android:orientation="horizontal"
                        android:layout_weight="3">

                        <!-- 1st column -->

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_gravity="center"
                            android:gravity="center"
                            android:text="A"/>

                    </LinearLayout>
                    <LinearLayout

                        android:orientation="horizontal"
                        android:layout_weight="1"
                        android:weightSum="3">

                        <LinearLayout

                            android:orientation="horizontal"
                            android:layout_weight="2">

                            <!-- second column -->

                            <TextView
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:layout_gravity="center"
                                android:gravity="center"
                                android:text="B"/>

                        </LinearLayout>
                        <LinearLayout

                            android:orientation="horizontal"
                            android:layout_weight="1"
                            android:weightSum="2">

                            <LinearLayout

                                android:orientation="horizontal"
                                android:layout_weight="1">

                                <!-- third column -->

                                <TextView
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:layout_gravity="center"
                                    android:gravity="center"
                                    android:text="C"/>

                            </LinearLayout>

                            <LinearLayout

                                android:orientation="horizontal"
                                android:layout_weight="1">

                                <!-- fourth column -->
                                <TextView
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:layout_gravity="center"
                                    android:gravity="center"
                                    android:text="D"/>

                            </LinearLayout>

                        </LinearLayout>

                    </LinearLayout>

                </LinearLayout>

            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:layout_weight="1"
                android:weightSum="4">

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

                    <!-- third row -->

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="horizontal"
                        android:weightSum="4">

                        <LinearLayout

                            android:orientation="horizontal"
                            android:layout_weight="3">

                            <!-- 1st column -->

                            <TextView
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:layout_gravity="center"
                                android:gravity="center"
                                android:text="A"/>

                        </LinearLayout>
                        <LinearLayout

                            android:orientation="horizontal"
                            android:layout_weight="1"
                            android:weightSum="3">

                            <LinearLayout

                                android:orientation="horizontal"
                                android:layout_weight="2">

                                <!-- second column -->

                                <TextView
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:layout_gravity="center"
                                    android:gravity="center"
                                    android:text="B"/>

                            </LinearLayout>
                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:orientation="horizontal"
                                android:layout_weight="1"
                                android:weightSum="2">

                                <LinearLayout

                                    android:orientation="horizontal"
                                    android:layout_weight="1">

                                    <!-- third column -->

                                    <TextView
                                        android:layout_width="match_parent"
                                        android:layout_height="match_parent"
                                        android:layout_gravity="center"
                                        android:gravity="center"
                                        android:text="C"/>

                                </LinearLayout>

                                <LinearLayout

                                    android:orientation="horizontal"
                                    android:layout_weight="1">

                                    <!-- fourth column -->
                                    <TextView
                                        android:layout_width="match_parent"
                                        android:layout_height="match_parent"
                                        android:layout_gravity="center"
                                        android:gravity="center"
                                        android:text="D"/>

                                </LinearLayout>

                            </LinearLayout>

                        </LinearLayout>

                    </LinearLayout>

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    android:layout_weight="1"
                    android:weightSum="3">

                    <LinearLayout

                        android:orientation="vertical"
                        android:layout_weight="2">

                        <!-- fourth row -->

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:orientation="horizontal"
                            android:weightSum="4">

                            <LinearLayout

                                android:orientation="horizontal"
                                android:layout_weight="3">

                                <!-- 1st column -->

                                <TextView
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:layout_gravity="center"
                                    android:gravity="center"
                                    android:text="A"/>

                            </LinearLayout>
                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:orientation="horizontal"
                                android:layout_weight="1"
                                android:weightSum="3">

                                <LinearLayout

                                    android:orientation="horizontal"
                                    android:layout_weight="2">

                                    <!-- second column -->

                                    <TextView
                                        android:layout_width="match_parent"
                                        android:layout_height="match_parent"
                                        android:layout_gravity="center"
                                        android:gravity="center"
                                        android:text="B"/>

                                </LinearLayout>
                                <LinearLayout

                                    android:orientation="horizontal"
                                    android:layout_weight="1"
                                    android:weightSum="2">

                                    <LinearLayout

                                        android:orientation="horizontal"
                                        android:layout_weight="1">

                                        <!-- third column -->

                                        <TextView
                                            android:layout_width="match_parent"
                                            android:layout_height="match_parent"
                                            android:layout_gravity="center"
                                            android:gravity="center"
                                            android:text="C"/>

                                    </LinearLayout>

                                    <LinearLayout
                                        android:layout_width="match_parent"
                                        android:layout_height="match_parent"
                                        android:orientation="horizontal"
                                        android:layout_weight="1">

                                        <!-- fourth column -->
                                        <TextView
                                            android:layout_width="match_parent"
                                            android:layout_height="match_parent"
                                            android:layout_gravity="center"
                                            android:gravity="center"
                                            android:text="D"/>

                                    </LinearLayout>

                                </LinearLayout>

                            </LinearLayout>

                        </LinearLayout>

                    </LinearLayout>
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical"
                        android:layout_weight="1"
                        android:weightSum="2">

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

                            <!-- fifth row -->

                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:orientation="horizontal"
                                android:weightSum="4">

                                <LinearLayout
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:orientation="horizontal"
                                    android:layout_weight="3">

                                    <!-- 1st column -->

                                    <TextView
                                        android:layout_width="match_parent"
                                        android:layout_height="match_parent"
                                        android:layout_gravity="center"
                                        android:gravity="center"
                                        android:text="A"/>

                                </LinearLayout>
                                <LinearLayout
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:orientation="horizontal"
                                    android:layout_weight="1"
                                    android:weightSum="3">

                                    <LinearLayout
                                        android:layout_width="match_parent"
                                        android:layout_height="match_parent"
                                        android:orientation="horizontal"
                                        android:layout_weight="2">

                                        <!-- second column -->

                                        <TextView
                                            android:layout_width="match_parent"
                                            android:layout_height="match_parent"
                                            android:layout_gravity="center"
                                            android:gravity="center"
                                            android:text="B"/>

                                    </LinearLayout>
                                    <LinearLayout
                                        android:layout_width="match_parent"
                                        android:layout_height="match_parent"
                                        android:orientation="horizontal"
                                        android:layout_weight="1"
                                        android:weightSum="2">

                                        <LinearLayout
                                            android:layout_width="match_parent"
                                            android:layout_height="match_parent"
                                            android:orientation="horizontal"
                                            android:layout_weight="1">

                                            <!-- third column -->

                                            <TextView
                                                android:layout_width="match_parent"
                                                android:layout_height="match_parent"
                                                android:layout_gravity="center"
                                                android:gravity="center"
                                                android:text="C"/>

                                        </LinearLayout>

                                        <LinearLayout
                                            android:layout_width="match_parent"
                                            android:layout_height="match_parent"
                                            android:orientation="horizontal"
                                            android:layout_weight="1">

                                            <!-- fourth column -->
                                            <TextView
                                                android:layout_width="match_parent"
                                                android:layout_height="match_parent"
                                                android:layout_gravity="center"
                                                android:gravity="center"
                                                android:text="D"/>

                                        </LinearLayout>

                                    </LinearLayout>

                                </LinearLayout>

                            </LinearLayout>

                        </LinearLayout>

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

                            <!-- sixth row -->

                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:orientation="horizontal"
                                android:weightSum="3">

                                <LinearLayout
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:orientation="horizontal"
                                    android:layout_weight="2"
                                    android:background="@color/colorPrimaryDark">

                                    <!-- first column -->

                                    <TextView
                                        android:layout_width="match_parent"
                                        android:layout_height="match_parent"
                                        android:layout_gravity="center"
                                        android:gravity="center"
                                        android:text="U"/>

                                </LinearLayout>
                                <LinearLayout
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:orientation="horizontal"
                                    android:layout_weight="1"
                                    android:weightSum="2">

                                    <LinearLayout
                                        android:layout_width="match_parent"
                                        android:layout_height="match_parent"
                                        android:orientation="horizontal"
                                        android:layout_weight="1"
                                        android:background="@color/colorAccent">

                                        <!-- second column -->
                                        <TextView
                                            android:layout_width="match_parent"
                                            android:layout_height="match_parent"
                                            android:layout_gravity="center"
                                            android:gravity="center"
                                            android:text="V"/>

                                    </LinearLayout>

                                    <LinearLayout
                                        android:layout_width="match_parent"
                                        android:layout_height="match_parent"
                                        android:orientation="horizontal"
                                        android:layout_weight="1"
                                        android:background="@color/colorPrimary">

                                        <!-- third column -->

                                        <TextView
                                            android:layout_width="match_parent"
                                            android:layout_height="match_parent"
                                            android:layout_gravity="center"
                                            android:gravity="center"
                                            android:text="W"/>


                                    </LinearLayout>

                                </LinearLayout>

                            </LinearLayout>

                        </LinearLayout>

                    </LinearLayout>

                </LinearLayout>

            </LinearLayout>


        </LinearLayout>

    </LinearLayout>

</LinearLayout>
PushpikaWan
  • 2,437
  • 3
  • 14
  • 23