android gridlayout not showing in emulator API 22

3k views Asked by At

I have an app that functions exactly as desired on an actual device Nexus 6P running API 23 and an emulated 6P, 5 & 4 running API 24, but is showing odd behaviour wrt GridLayout on emulated Nexus 5 & 4 running API 22.

The app only has a single activity with multiple RelativeLayouts that are programmatically set .VISIBLE & .INVISIBLE. In one such RelativeLayout I have 2 GridLayouts as follows. When emulating API 22, the bottom GridLayout does not show but the top one always does irrespective of API 22, 23 or 24. The bottom GridLayout only shows when I change the emulator setting to API 24 (haven't tried 23). Why might that be? I don't think I'm using anything on GridLayout that requires API 23 or above. This problem means I cannot install this app on a Nexus 5 running API 22 or 21. Thanks for any suggestion.

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/gameOperations"
    android:visibility="invisible">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/launchDisplay"
        android:gravity="center_horizontal">

        <TextView
            android:text="@string/app_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/launchTitle"
            android:textStyle="normal|bold"
            android:textSize="50dp"
            android:textAlignment="center"
            android:fontFamily="casual"
            android:textColor="@android:color/holo_blue_dark"
            android:layout_marginTop="15dp"
            android:layout_weight="1" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/brain_orig_crop_500"
            android:id="@+id/launchImage"
            android:scaleType="centerInside"
            android:layout_weight="2" />

        <TextView
            android:text="@string/launchSubtitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/launchSubtitle"
            android:fontFamily="casual"
            android:textAlignment="center"
            android:textColor="@android:color/holo_blue_dark"
            android:textSize="25dp"
            android:textStyle="normal|bold"
            android:layout_weight="1"
            android:layout_marginTop="15dp" />

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/launchControls"
            android:gravity="center_vertical"
            android:layout_weight="1">

            <Button
                android:text="@string/debug"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/launchSpacerLeft"
                android:layout_weight="1"
                android:background="@color/colourTransparent"
                android:textSize="15dp"
                android:fontFamily="casual"
                android:textAllCaps="false"
                android:visibility="invisible" />

            <Button
                android:text="@string/launchStart"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/launchStart"
                android:textColor="@android:color/holo_green_dark"
                android:textSize="38dp"
                android:textStyle="normal|bold"
                android:onClick="prepareNewGame"
                android:layout_weight="3"
                android:background="@color/colourTransparent"
                android:fontFamily="casual"
                android:textAllCaps="false" />

            <Button
                android:text="@string/layoutNormal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/launchSpacerRight"
                android:layout_weight="1"
                android:textAllCaps="false"
                android:background="@color/colourTransparent"
                android:fontFamily="casual"
                android:textSize="15dp" />

        </LinearLayout>

    </LinearLayout>

    <GridLayout
        android:layout_width="match_parent"
        android:id="@+id/header"
        android:columnCount="6"
        android:rowCount="4"
        android:layout_height="168dp"
        android:useDefaultMargins="true"
        android:layout_alignParentTop="true">

        <ImageView
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:srcCompat="@drawable/brain_orig_500"
            android:id="@+id/imageBrain"
            android:layout_column="0"
            android:layout_row="0"
            android:layout_rowWeight="1"
            android:layout_rowSpan="4"
            android:layout_columnSpan="6"
            android:layout_columnWeight="1"
            android:layout_gravity="fill"
            android:scaleType="fitCenter" />

        <Button
            android:text="@string/debug"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:id="@+id/debug"
            android:layout_column="2"
            android:layout_row="3"
            android:layout_columnSpan="2"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:fontFamily="casual"
            android:textSize="18dp"
            android:textStyle="normal|bold"
            android:textAllCaps="false"
            android:onClick="prepareNewGame"
            android:background="@color/colourTransparent"
            android:layout_gravity="fill"
            android:paddingTop="10dp"
            android:visibility="gone" />

        <TextView
            android:text="@string/blank"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:id="@+id/spacerColumn"
            android:layout_column="0"
            android:layout_columnWeight="1"
            android:layout_columnSpan="2"
            android:layout_gravity="fill"
            android:layout_row="0"
            android:layout_rowSpan="3"
            android:layout_rowWeight="3" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:text="@string/timerDisplay"
            android:id="@+id/timerDisplay"
            android:layout_column="0"
            android:layout_row="3"
            android:layout_columnSpan="2"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:fontFamily="casual"
            android:textSize="30dp"
            android:textStyle="normal|bold"
            android:gravity="start"
            android:layout_gravity="fill"
            android:layout_rowSpan="1" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:id="@+id/rightWrongDisplay"
            android:layout_column="2"
            android:layout_row="3"
            android:layout_columnSpan="2"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:textAlignment="center"
            android:textStyle="normal|bold"
            android:textSize="28dp"
            android:fontFamily="casual"
            android:layout_gravity="fill"
            android:layout_rowSpan="1"
            android:visibility="gone" />

        <TextView
            android:text="@string/scoreDisplay"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:id="@+id/scoreDisplay"
            android:layout_column="4"
            android:layout_row="3"
            android:layout_columnSpan="2"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:fontFamily="casual"
            android:textStyle="normal|bold"
            android:textSize="30dp"
            android:gravity="end"
            android:layout_gravity="fill"
            android:layout_rowSpan="1" />

    </GridLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/scoreBoard"
        android:layout_alignParentTop="false"
        android:layout_below="@+id/header"
        android:visibility="gone">

    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:id="@+id/question"
        android:layout_below="@+id/scoreBoard"
        android:layout_marginTop="15dp"
        android:gravity="center">

        <TextView
            android:text="@string/operandLeft"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/operandLeft"
            android:layout_weight="5"
            android:textSize="60sp"
            android:fontFamily="casual"
            android:textAlignment="textEnd"
            android:textStyle="normal|bold"
            android:paddingEnd="10dp"
            android:paddingStart="0dp"
            android:layout_gravity="center_vertical|end"
            android:paddingLeft="0dp"
            android:paddingRight="10dp"
            android:visibility="gone" />

        <TextView
            android:text="@string/operator"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/operator"
            android:layout_weight="1"
            android:textAlignment="center"
            android:textStyle="normal|bold"
            android:textSize="60sp"
            android:fontFamily="casual"
            android:visibility="gone" />

        <TextView
            android:text="@string/operandRight"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/operandRight"
            android:layout_weight="5"
            android:textAlignment="textStart"
            android:textStyle="normal|bold"
            android:textSize="60sp"
            android:fontFamily="casual"
            android:paddingStart="10dp"
            android:paddingEnd="0dp"
            android:layout_gravity="center_vertical|start"
            android:paddingLeft="10dp"
            android:paddingRight="0dp"
            android:visibility="gone" />

        <TextView
            android:text="@string/sampleQuestion"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/questionView"
            android:layout_weight="1"
            android:gravity="center"
            android:textSize="60dp"
            android:textStyle="normal|bold"
            android:fontFamily="casual" />

    </LinearLayout>

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:layout_below="@+id/question"
        android:layout_marginTop="10dp"
        android:id="@+id/answers"
        android:rowCount="2"
        android:columnCount="2"
        android:useDefaultMargins="true"
        android:layout_above="@+id/footer">

        <Button
            android:text="@string/answer0"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:id="@+id/answer0"
            android:layout_column="0"
            android:layout_row="0"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:onClick="actionAnswer"
            android:textSize="50dp"
            android:textAllCaps="false"
            android:textStyle="normal|bold"
            android:fontFamily="casual"
            android:layout_gravity="fill_horizontal"
            android:background="@color/colourTransparent" />

        <Button
            android:text="@string/answer1"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:id="@+id/answer1"
            android:layout_column="1"
            android:layout_row="0"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:onClick="actionAnswer"
            android:textSize="50dp"
            android:textAllCaps="false"
            android:textStyle="normal|bold"
            android:fontFamily="casual"
            android:layout_gravity="fill_horizontal"
            android:background="@color/colourTransparent" />

        <Button
            android:text="@string/answer2"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:id="@+id/answer2"
            android:layout_column="0"
            android:layout_row="1"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:layout_gravity="fill_horizontal"
            android:onClick="actionAnswer"
            android:textSize="50dp"
            android:textAllCaps="false"
            android:textStyle="normal|bold"
            android:fontFamily="casual"
            android:background="@color/colourTransparent" />

        <Button
            android:text="@string/answer3"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:id="@+id/answer3"
            android:layout_column="1"
            android:layout_row="1"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:layout_gravity="fill_horizontal"
            android:onClick="actionAnswer"
            android:textSize="50dp"
            android:textAllCaps="false"
            android:textStyle="normal|bold"
            android:fontFamily="casual"
            android:background="@color/colourTransparent"  />

    </GridLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="25dp"
        android:id="@+id/footer"
        android:layout_marginTop="15dp"
        android:gravity="center"
        android:visibility="gone"
        android:layout_alignParentBottom="true">

        <TextView
            android:text="@string/footerText"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/footerText"
            android:layout_weight="1"
            android:gravity="center"
            android:textSize="18sp"
            android:fontFamily="casual" />

    </LinearLayout>
</RelativeLayout>
4

There are 4 answers

0
Slartibartfast On

I too had exactly this issue and I finally found a solution by using android.support.v7.widget.GridLayout instead of just GridLayout

I don't understand the details deeply but previous versions of GridLayout don't seem to support the android:layout_columnWeight property, though it does support the android:layout_width property. Given you are setting the width to "0dp" using "layout_width" (which does exist) then attempting to 'stretch' it using "columnWeight" (which does not exist) the widget remains at 0dp and thus is not shown.

To work around this the "support.v7" version of GridLayout does allow you to use 'layout_columnWeight', though you must be sure to prefix it with 'app' as apposed to 'android', as in app:layout_columnWeight = "1". So your layout file should read:

<android.support.v7.widget.GridLayout
        android:layout_width="match_parent"
        android:id="@+id/header"
        android:columnCount="6"
        android:rowCount="4"
        android:layout_height="168dp"
        android:useDefaultMargins="true"
        android:layout_alignParentTop="true">

    <ImageView
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:srcCompat="@drawable/brain_orig_500"
            android:id="@+id/imageBrain"
            android:layout_column="0"
            android:layout_row="0"
            android:layout_rowWeight="1"
            android:layout_rowSpan="4"
            android:layout_columnSpan="6"
            app:layout_columnWeight="1"
            android:layout_gravity="fill"
            android:scaleType="fitCenter" />

    .
    . 
    .

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

The critical changes being adding android.support.v7.widget. to Gridlayout and changing android:layout_columnWeight="1" to app:layout_columnWeight="1"

You will also have to add the version of gridlauout-v7 you wish to use to the dependency section of your manifest. For me I added:

compile 'com.android.support:gridlayout-v7:26.0.0-alpha1'

And finally if you are wanting to include the column weight in a style then omit the 'app' prefix, as in:

<style name="button_digit" parent="button">
    <item name="android:layout_width">0dip</item>
    <item name="layout_columnWeight">1</item>
    .
    .
    .

</style>
0
Muhammed Ziyad On

Change GridLayout to android.support.v7.widget.GridLayout and add

implementation 'com.android.support:gridlayout-v7:28.0.0' 

in app.gradle this Worked for me

and also replace

android:columnCount="2"
android:rowCount="2"

To

app:layout_columnWeight = "2"
app:layout_rowWeight = "2"

and Also change

android:layout_columnWeight="1"
android:layout_rowWeight="1"

To

app:layout_columnWeight = "1"
app:layout_rowWeight = "1"

one More if you are using other tags like Elevation,cardCornerRadius,etc change android: to app

0
PraveenK On

Change GridLayout to androidx.gridlayout.widget.GridLayout by adding dependency

implementation 'androidx.gridlayout:gridlayout:1.0.0'

Change following attributes

android:rowCount="2"
android:columnCount="2"
android:layout_row="0"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_column="0"

To

app:rowCount="2"
app:columnCount="2"
app:layout_row="0"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
app:layout_column="0"
0
Phil On

Alternate Solution : It's gonna look nasty but it works, even I didn't want to use that technique, but someone pointed out to me that the saved memory from not using the v7 GridLayout(no need for extra library) might be worth using this :

at the begging of the activity create a loop that manually set the height and width of every cell like this

  if(imageCell.getMeasuredHeight() == 0){
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        int height = displayMetrics.heightPixels;
        int width = displayMetrics.widthPixels;
        GridLayout.LayoutParams lp;
        lp = (GridLayout.LayoutParams) imageCell.getLayoutParams();
        lp.height = (int) ((height / 9));
        lp.width = (int) (width / 8);
        imageCell.setLayoutParams(lp);
    }

(edit : assuming a fullscreen 8/9 grid)