Search code examples
androidandroid-layoutandroid-5.0-lollipop

Android Lollipop: Cannot include an external layout inside of a GridLayout


I am trying to include an external layout file that contains a GridLayout inside a GridLayout. My code works for API 23+, but does not work for API 21 or API 22 (Lollipop). Below is my code for activity_main.xml, in which I include the layout textcomparator.xml.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff"
        android:layout_weight="1"
        android:fillViewport="true">

        <GridLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#ffffff"
            android:columnCount="1"
            android:orientation="vertical"
            android:rowCount="1"
            tools:context=".MainActivity">

            <!-- Text comparison UI -->
            <include
                android:id="@+id/text_comparator"
                layout="@layout/textcomparator"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_row="0"
                android:layout_column="0"
                android:layout_weight="1"
                android:visibility="visible" />

        </GridLayout>
    </ScrollView>
</LinearLayout>

Below is my code for textcomparator.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:rowCount="1"
        android:columnCount="2">

        <EditText
            android:id="@+id/textbox"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_row="0"
            android:layout_column="0"
            android:layout_columnWeight="1"
            android:gravity="left|top"
            android:hint="Text 1"
            android:inputType="textMultiLine"
            android:padding="10dp"
            android:text="" />

        <EditText
            android:id="@+id/textbox2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_row="0"
            android:layout_column="1"
            android:layout_columnWeight="1"
            android:gravity="left|top"
            android:hint="Text 2"
            android:inputType="textMultiLine"
            android:padding="10dp"
            android:text="" />
    </GridLayout>
</LinearLayout>

Below is the screenshot for API 22 (not working): enter image description here Below is the screenshot for API 23 (working): enter image description here


Solution

  • I seem to recall that the implementation of GridLayout in the framework was a little buggy pre API 23. The TextViews have a zero width in APIs 21 and 22. (You can see this in the layout inspector of Android Studio - Tools->Layout Inspector.) This is because the layout weights are not working. You should be using the AndroidX version which actually works better.

    implementation "androidx.gridlayout:gridlayout:1.0.0"
    

    The support library version will also work.

    Here is what you code would look like with AndroidX. I think that the XML for the support library will be the same except you would reference the support library version of GridLayout and not the AndroidX version.

    <LinearLayout 
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#ffffff"
            android:layout_weight="1"
            android:fillViewport="true">
    
            <androidx.gridlayout.widget.GridLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#ffffff"
                app:columnCount="1"
                app:orientation="vertical"
                app:rowCount="1"
                tools:context=".MainActivity">
    
                <!-- Text comparison UI -->
                <include
                    android:id="@+id/text_comparator"
                    layout="@layout/textcomparator"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:layout_row="0"
                    app:layout_column="0"
                    android:layout_weight="1"
                    android:visibility="visible" />
    
            </androidx.gridlayout.widget.GridLayout>
        </ScrollView>
    </LinearLayout>
    

    And the included file:

    <LinearLayout 
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <androidx.gridlayout.widget.GridLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:rowCount="1"
            app:columnCount="2">
    
            <EditText
                android:id="@+id/textbox"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                app:layout_row="0"
                app:layout_column="0"
                app:layout_columnWeight="1"
                android:gravity="left|top"
                android:hint="Text 1"
                android:inputType="textMultiLine"
                android:padding="10dp"
                android:text="" />
    
            <EditText
                android:id="@+id/textbox2"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                app:layout_row="0"
                app:layout_column="1"
                app:layout_columnWeight="1"
                android:gravity="left|top"
                android:hint="Text 2"
                android:inputType="textMultiLine"
                android:padding="10dp"
                android:text="" />
        </androidx.gridlayout.widget.GridLayout>
    </LinearLayout>
    

    A quick test on an emulator running API 22 shows the following:

    enter image description here

    This is what is expected.