Search code examples
androidandroid-studiogradlegrid-layout

GridLayout not showing on my device


This is my xml code:

<ScrollView 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <GridLayout
        android:id="@+id/mainGrid"
        android:columnCount="2"
        android:rowCount="3"
        android:alignmentMode="alignMargins"
        android:columnOrderPreserved="false"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="14dp"
        >
        <android.support.v7.widget.CardView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            app:cardElevation="8dp"
            app:cardCornerRadius="8dp"
            >

            <LinearLayout
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <ImageView
                    android:src="@drawable/icona_monumenti"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="100dp"
                    android:layout_height="100dp" />

                <TextView
                    android:text="Me"
                    android:textAlignment="center"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

            </LinearLayout>

        </android.support.v7.widget.CardView>

        <!-- Column 2 -->
        <android.support.v7.widget.CardView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            app:cardElevation="8dp"
            app:cardCornerRadius="8dp"
            >

            <LinearLayout
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <ImageView
                    android:src="@drawable/icona_piatti_tipici"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="100dp"
                    android:layout_height="100dp" />

                <TextView
                    android:text="Family"
                    android:textAlignment="center"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

            </LinearLayout>

        </android.support.v7.widget.CardView>


        <!-- Row 2 -->

        <!-- Column 1 -->
        <android.support.v7.widget.CardView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            app:cardElevation="8dp"
            app:cardCornerRadius="8dp"
            >

            <LinearLayout
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <ImageView
                    android:src="@drawable/icona_storia"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="100dp"
                    android:layout_height="100dp" />
                <TextView
                    android:text="Lovely"
                    android:textAlignment="center"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>
        </android.support.v7.widget.CardView>
        <android.support.v7.widget.CardView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            app:cardElevation="8dp"
            app:cardCornerRadius="8dp"
            >
            <LinearLayout
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <ImageView
                    android:src="@drawable/icona_informazioni"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="100dp"
                    android:layout_height="100dp" />
                <TextView
                    android:text="Team"
                    android:textAlignment="center"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>
        </android.support.v7.widget.CardView>
    </GridLayout>
</ScrollView>

In the preview of my xml file, the GridLayout is visible, but when I run the application, on my phone the GridLayout is not visible.

gradle: implementation 'com.android.support:cardview-v7:27.1.1'

The code don't give me error or other things, and the same the stack trace

This is the image of my layout:


Solution

  • What is the need of GridLayout here?

    You can use nested LinearLayout this is the blue-print of that:

                     LinearLayout
                      (Vertical)
                          |
               -------------------------
              |                         |
         LinearLayout              LinearLayout
         (Horizontal)              (Horizontal)
              |                         |
        -------------             -------------
       |             |           |             |
    CardView      CardView    CardView      CardView
    

    And the code is:

    <LinearLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        andriod:orientation="vertical">
    
        <LinearLayout
            android:layout_height="0dp"
            android:layout_width="match_parent"
            android:layout_weight="1"
            andriod:orientation="horizontal">
    
            <!--Row 1-->
            <!--Column 1-->
            <android.support.v7.widget.CardView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_marginBottom="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                app:cardElevation="8dp"
                app:cardCornerRadius="8dp">
    
                <LinearLayout
                    android:layout_gravity="center_horizontal|center_vertical"
                    android:layout_margin="16dp"
                    android:orientation="vertical"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">
    
                    <ImageView
                        android:src="@drawable/icona_monumenti"
                        android:layout_gravity="center_horizontal"
                        android:layout_width="100dp"
                        android:layout_height="100dp" />
    
                    <TextView
                        android:text="Me"
                        android:textAlignment="center"
                        android:textSize="18sp"
                        android:textStyle="bold"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />
    
                </LinearLayout>
    
            </android.support.v7.widget.CardView>
    
            <!-- Column 2 -->
            <android.support.v7.widget.CardView
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:layout_marginBottom="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                app:cardElevation="8dp"
                app:cardCornerRadius="8dp">
    
                <LinearLayout
                    android:layout_gravity="center_horizontal|center_vertical"
                    android:layout_margin="16dp"
                    android:orientation="vertical"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">
    
                    <ImageView
                        android:src="@drawable/icona_piatti_tipici"
                        android:layout_gravity="center_horizontal"
                        android:layout_width="100dp"
                        android:layout_height="100dp" />
    
                    <TextView
                        android:text="Family"
                        android:textAlignment="center"
                        android:textSize="18sp"
                        android:textStyle="bold"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />
    
                </LinearLayout>
    
            </android.support.v7.widget.CardView>
    
        </LinearLayout>
    
        <LinearLayout
            android:layout_height="0dp"
            android:layout_width="match_parent"
            android:layout_weight="1"
            andriod:orientation="horizontal">
    
            <!-- Row 2 -->
    
            <!-- Column 1 -->
            <android.support.v7.widget.CardView
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:layout_marginBottom="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                app:cardElevation="8dp"
                app:cardCornerRadius="8dp">
    
                <LinearLayout
                    android:layout_gravity="center_horizontal|center_vertical"
                    android:layout_margin="16dp"
                    android:orientation="vertical"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">
                    <ImageView
                        android:src="@drawable/icona_storia"
                        android:layout_gravity="center_horizontal"
                        android:layout_width="100dp"
                        android:layout_height="100dp" />
                    <TextView
                        android:text="Lovely"
                        android:textAlignment="center"
                        android:textSize="18sp"
                        android:textStyle="bold"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />
                </LinearLayout>
            </android.support.v7.widget.CardView>
    
            <!-- Column 2 -->
            <android.support.v7.widget.CardView
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:layout_marginBottom="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                app:cardElevation="8dp"
                app:cardCornerRadius="8dp">
    
                <LinearLayout
                    android:layout_gravity="center_horizontal|center_vertical"
                    android:layout_margin="16dp"
                    android:orientation="vertical"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">
                    <ImageView
                        android:src="@drawable/icona_informazioni"
                        android:layout_gravity="center_horizontal"
                        android:layout_width="100dp"
                        android:layout_height="100dp" />
                    <TextView
                        android:text="Team"
                        android:textAlignment="center"
                        android:textSize="18sp"
                        android:textStyle="bold"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />
                </LinearLayout>
            </android.support.v7.widget.CardView>
    
        </LinearLayout>
    
    </LinearLayout>