Connection Coder Connection Coder - 4 months ago 18
Android Question

XML Error "Grid Layout is Useless" (Android Studio)

I'm currently taking an Android Development course in Udemy, and I'm currently learning how to build a basic phrases app, which consists of a Grid Layout. I followed exactly what the instructor did with the XML part, and I got an error that the instructor didn't get:

This GridLayout layout or its RelativeLayout parent is useless. A layout with children that has no siblings, is not a scrollview or a root layout, and does not have a background, can be removed and have its children moved directly into the parent for a flatter and more efficient layout hierarchy.


This is the XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.jumin.basicphrases.MainActivity">

<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="0"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_gravity="fill"
android:text="Hello"
android:onClick="buttonTapped"
android:id="@+id/button" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="0"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_gravity="fill"
android:text="How are you?"
android:onClick="buttonTapped"
android:id="@+id/howareyou" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="1"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_gravity="fill"
android:text="Good Evening"
android:onClick="buttonTapped"
android:id="@+id/goodevening" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="1"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_gravity="fill"
android:text="Please"
android:onClick="buttonTapped"
android:id="@+id/please" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="2"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_gravity="fill"
android:text="My name is..."
android:onClick="buttonTapped"
android:id="@+id/mynameis" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="2"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_gravity="fill"
android:text="Do you &#10; speak English?"
android:onClick="buttonTapped"
android:id="@+id/doyouspeakenglish" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="3"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_gravity="fill"
android:text="Welcome"
android:onClick="buttonTapped"
android:id="@+id/welcome" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="3"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_gravity="fill"
android:text="I live in..."
android:onClick="buttonTapped"
android:id="@+id/ilivein" />

</GridLayout>
</RelativeLayout>

Answer

The error is very clear: Just remove the RelativeLayout and try again:

<GridLayout
     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">

     <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_column="0"
         android:layout_row="0"
         android:layout_columnWeight="1"
         android:layout_rowWeight="1"
         android:layout_gravity="fill"
         android:text="Hello"
         android:onClick="buttonTapped"
         android:id="@+id/button" />

     <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_column="1"
         android:layout_row="0"
         android:layout_columnWeight="1"
         android:layout_rowWeight="1"
         android:layout_gravity="fill"
         android:text="How are you?"
         android:onClick="buttonTapped"
         android:id="@+id/howareyou" />

     <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_column="0"
         android:layout_row="1"
         android:layout_columnWeight="1"
         android:layout_rowWeight="1"
         android:layout_gravity="fill"
         android:text="Good Evening"
         android:onClick="buttonTapped"
         android:id="@+id/goodevening" />

     <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_column="1"
         android:layout_row="1"
         android:layout_columnWeight="1"
         android:layout_rowWeight="1"
         android:layout_gravity="fill"
         android:text="Please"
         android:onClick="buttonTapped"
         android:id="@+id/please" />

     <Button
         android:layout_width="wrap_content"
              android:layout_height="wrap_content"
         android:layout_column="0"
              android:layout_row="2"
         android:layout_columnWeight="1"
         android:layout_rowWeight="1"
         android:layout_gravity="fill"
         android:text="My name is..."
         android:onClick="buttonTapped"
         android:id="@+id/mynameis" />

     <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_column="1"
         android:layout_row="2"
         android:layout_columnWeight="1"
         android:layout_rowWeight="1"
         android:layout_gravity="fill"
         android:text="Do you &#10; speak English?"
         android:onClick="buttonTapped"
         android:id="@+id/doyouspeakenglish" />

     <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_column="0"
         android:layout_row="3"
         android:layout_columnWeight="1"
         android:layout_rowWeight="1"
         android:layout_gravity="fill"
         android:text="Welcome"
         android:onClick="buttonTapped"
         android:id="@+id/welcome" />

     <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_column="1"
         android:layout_row="3"
         android:layout_columnWeight="1"
         android:layout_rowWeight="1"
         android:layout_gravity="fill"
         android:text="I live in..."
         android:onClick="buttonTapped"
         android:id="@+id/ilivein" />

 </GridLayout>
Comments