I am trying to create a Grid Layout with 2 Columns and 3 Rows. I will use ImageButtons for the grid, so that there's 6 images on the screen, each with the same size. When I add the background to the ImageButton, the button fills the whole screen. Should I add Width and Height properties to fix this? Can anyone help me?
<GridLayout
android:id="#+id/team_grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:rowCount="3"
>
<ImageButton
android:id="#+id/alexandre"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:background=""
android:textSize="18sp"
android:src="#drawable/alexandre"/>
<ImageButton
android:id="#+id/barbara"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:background="#ffffff"
android:textSize="18sp"
android:src="#drawable/barbara"/>
<ImageButton
android:id="#+id/pedro"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:background="#ffffff"
android:textSize="18sp"
android:src="#drawable/goncalo"/>
<ImageButton
android:id="#+id/ImageButton4"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:background="#ffffff"
android:textSize="18sp"
android:src="#drawable/pedro">
<ImageButton
android:id="#+id/ImageButton5"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:background="#ffffff"
android:textSize="18sp"
android:src="#drawable/rafael"/>
<ImageButton
android:id="#+id/ImageButton6"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:background="#ffffff"
android:textSize="18sp"
android:src="#drawable/ricardo"/>
</GridLayout>
So, Your question is a bit confusing because in your XML you have set the background of each ImageButton to white color which seems to have no effect but if you try setting different color for each one then it will set a different color accordingly. And, you can't have an empty value for background like in your first ImageButton. And, try to add some padding and margin for the ImageButton.I tried your code and it work's fine. Here's what I did.
<GridLayout android:id="#+id/team_grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:rowCount="3"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageButton
android:id="#+id/alexandre"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:textSize="18sp"
android:background="#color/black"
android:padding="5dp"
android:layout_margin="5dp"
android:src="#drawable/ic_launcher_background"/>
<ImageButton
android:id="#+id/barbara"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:background="#android:color/holo_blue_bright"
android:textSize="18sp"
android:layout_margin="5dp"
android:src="#drawable/ic_launcher_foreground"/>
<ImageButton
android:id="#+id/pedro"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:background="#color/black"
android:textSize="18sp"
android:padding="5dp"
android:layout_margin="5dp"
android:src="#drawable/ic_launcher_background"/>
<ImageButton
android:id="#+id/ImageButton4"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:background="#color/black"
android:textSize="18sp"
android:padding="5dp"
android:layout_margin="5dp"
android:src="#drawable/ic_launcher_foreground"/>
<ImageButton
android:id="#+id/ImageButton5"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:background="#android:color/holo_blue_dark"
android:textSize="18sp"
android:layout_margin="5dp"
android:padding="5dp"
android:src="#drawable/ic_launcher_background"/>
<ImageButton
android:id="#+id/ImageButton6"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:background="#android:color/holo_orange_dark"
android:textSize="18sp"
android:layout_margin="5dp"
android:padding="5dp"
android:src="#drawable/ic_launcher_foreground"/>
Related
enter image description here
I don't know how to make all button sizes the same... I tried making grid to wrap content, it worked, but button sizes were a bit different depending on the number of characters. Does anyone know how to format this stuff?
I tried adding number of rows and columns, gravity, etc, but it didn't change a thing.
Here is my XML code:
<GridLayout
android:layout_height="match_parent"
android:layout_width="match_parent">
<Button
android:text="Hello"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_column="0"
android:layout_row="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/hello"
android:onClick="buttonTapped"
android:gravity="center"
android:layout_gravity="fill" />
<Button
android:text="Do you speak
English?"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_column="1"
android:layout_row="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/english"
android:onClick="buttonTapped"
android:gravity="center"
android:layout_gravity="fill" />
<Button
android:text="Good Evening"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_column="0"
android:layout_row="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/goodEvening"
android:onClick="buttonTapped"
android:gravity="center"
android:layout_gravity="fill" />
<Button
android:text="Please"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_column="1"
android:layout_row="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/please"
android:onClick="buttonTapped"
android:gravity="center"
android:layout_gravity="fill" />
<Button
android:text="My name is"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_column="0"
android:layout_row="2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/name"
android:onClick="buttonTapped"
android:gravity="center"
android:layout_gravity="fill" />
<Button
android:text="Welcome"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_column="1"
android:layout_row="2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/welcome"
android:onClick="buttonTapped"
android:gravity="center"
android:layout_gravity="fill" />
<Button
android:text="How are you?"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_column="0"
android:layout_row="3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/howAreYou"
android:onClick="buttonTapped"
android:gravity="center"
android:layout_gravity="fill" />
<Button
android:text="I live in"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_column="1"
android:layout_row="3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/iLiveIn"
android:onClick="buttonTapped"
android:gravity="center"
android:layout_gravity="fill" />
</GridLayout>
As you can see in this document . android:layout_gravity="fill" will
Grow the horizontal and vertical size of the object if needed so it
completely fills its container
and here your container size is match_parent.So it is filling your layout . Now make your container(GridLayout) wrap_content , otherwise use gravity attribute properly
Change from match_parent to wrap_content if you plan on using GridLayout:
<GridLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</GridLayout>
If not, then use constrain guidelines or linearlayout
i want that the image fits in the imageview at the gridlayout. When i dont give a source to the imageview the layout is correct. But when i put the source there the width and height of the imageview is wrong. Can somebody help me to solve the problem? I this for a game.
Correct Layout without src
XML without src
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context="com.ktappworks.test.MainActivity">
<GridLayout
android:id="#+id/ph0"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:layout_marginTop="0dp"
android:clipChildren="false"
android:columnCount="5"
android:rowCount="2"
android:src="#mipmap/ic_launcher"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent">
<ImageView
android:id="#+id/ph00"
android:layout_columnWeight="1"
android:layout_margin="2dp"
android:layout_rowWeight="1"
/>
<ImageView
android:id="#+id/ph01"
android:layout_columnWeight="1"
android:layout_margin="2dp"
android:layout_rowWeight="1"
/>
<ImageView
android:id="#+id/ph02"
android:layout_columnWeight="1"
android:layout_margin="2dp"
android:layout_rowWeight="1"
/>
<ImageView
android:id="#+id/ph03"
android:layout_columnWeight="1"
android:layout_margin="2dp"
android:layout_rowWeight="1"
/>
<ImageView
android:id="#+id/ph04"
android:layout_columnWeight="1"
android:layout_margin="2dp"
android:layout_rowWeight="1"
/>
<ImageView
android:id="#+id/ph05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_margin="2dp"
android:layout_rowWeight="1"
/>
<ImageView
android:id="#+id/ph06"
android:layout_columnWeight="1"
android:layout_margin="2dp"
android:layout_rowWeight="1"
/>
<ImageView
android:id="#+id/ph07"
android:layout_columnWeight="1"
android:layout_margin="2dp"
android:layout_rowWeight="1"
/>
<ImageView
android:id="#+id/ph08"
android:layout_columnWeight="1"
android:layout_margin="2dp"
android:layout_rowWeight="1"
/>
<ImageView
android:id="#+id/ph09"
android:layout_columnWeight="1"
android:layout_margin="2dp"
android:layout_rowWeight="1"
/>
</GridLayout>
</android.support.constraint.ConstraintLayout>
Incorrect layout with src
XML with src
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context="com.ktappworks.test.MainActivity">
<GridLayout
android:id="#+id/ph0"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:layout_marginTop="0dp"
android:clipChildren="false"
android:columnCount="5"
android:rowCount="2"
android:src="#mipmap/ic_launcher"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent">
<ImageView
android:id="#+id/ph00"
android:layout_columnWeight="1"
android:layout_margin="2dp"
android:layout_rowWeight="1"
android:scaleType="fitXY"
android:src="#drawable/owl" />
<ImageView
android:id="#+id/ph01"
android:layout_columnWeight="1"
android:layout_margin="2dp"
android:layout_rowWeight="1"
android:scaleType="fitXY"
android:src="#drawable/owl" />
<ImageView
android:id="#+id/ph02"
android:layout_columnWeight="1"
android:layout_margin="2dp"
android:layout_rowWeight="1"
android:scaleType="fitXY"
android:src="#drawable/owl" />
<ImageView
android:id="#+id/ph03"
android:layout_columnWeight="1"
android:layout_margin="2dp"
android:layout_rowWeight="1"
android:scaleType="fitXY"
android:src="#drawable/owl" />
<ImageView
android:id="#+id/ph04"
android:layout_columnWeight="1"
android:layout_margin="2dp"
android:layout_rowWeight="1"
android:scaleType="fitXY"
android:src="#drawable/owl" />
<ImageView
android:id="#+id/ph05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_margin="2dp"
android:layout_rowWeight="1"
android:scaleType="fitXY"
android:src="#drawable/owl" />
<ImageView
android:id="#+id/ph06"
android:layout_columnWeight="1"
android:layout_margin="2dp"
android:layout_rowWeight="1"
android:scaleType="fitXY"
android:src="#drawable/owl" />
<ImageView
android:id="#+id/ph07"
android:layout_columnWeight="1"
android:layout_margin="2dp"
android:layout_rowWeight="1"
android:scaleType="fitXY"
android:src="#drawable/owl" />
<ImageView
android:id="#+id/ph08"
android:layout_columnWeight="1"
android:layout_margin="2dp"
android:layout_rowWeight="1"
android:scaleType="fitXY"
android:src="#drawable/owl" />
<ImageView
android:id="#+id/ph09"
android:layout_columnWeight="1"
android:layout_margin="2dp"
android:layout_rowWeight="1"
android:scaleType="fitXY"
android:src="#drawable/owl" />
</GridLayout>
</android.support.constraint.ConstraintLayout>
Owl Image
You need to define the width and height of each ImageView as 0dp for the column/row weights to take effect. I think the default is wrap_content. You may also want to change the scaleType to something other than fitXY since fitXY will distort the images.
I have also run into an issue with the native implementation of GridLayout when using the weights for certain API levels. (I answered such a question recently and I will post a reference to it here if I can find it.) You may want to think about using the support library version of GridLayout since it seems to have a working implementation of weights. Add the following to your gradle file for this version of GridLayout:
compile 'com.android.support:gridlayout-v7:26.1.0'
Here is a screen shot after making these changes:
I have looked at various solutions, including gravity, but have not found the answer to the problem that I am having, which is that the image buttons inside of the grid layout are not spanning the width of the screen - looking like this picture below. They are more clumped up towards the left. I would like the spacing to be even across the screen. Thanks for the help.
Here is the XML file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="#+id/activity_admin_control"
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.example.myname.myproject.AdminControl">
<Button
android:text="Switch to User"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:id="#+id/userSwitch" />
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/userSwitch"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:rowCount="2"
android:columnCount="3"
android:alignmentMode="alignMargins">
<ImageButton
app:srcCompat="#drawable/common_google_signin_btn_icon_dark"
android:layout_row="0"
android:layout_column="0"
android:layout_width="118dp"
android:layout_height="118dp"
android:scaleType="fitCenter"/>
<ImageButton
app:srcCompat="#drawable/common_google_signin_btn_icon_dark"
android:id="#+id/imageButton5"
android:layout_row="0"
android:layout_column="1"
android:layout_width="118dp"
android:layout_height="118dp"
android:scaleType="fitCenter"/>
<ImageButton
app:srcCompat="#drawable/common_google_signin_btn_icon_dark"
android:id="#+id/imageButton6"
android:layout_row="0"
android:layout_column="2"
android:layout_width="118dp"
android:layout_height="118dp"
android:scaleType="fitCenter"/>
<ImageButton
app:srcCompat="#drawable/common_google_signin_btn_icon_dark"
android:id="#+id/imageButton7"
android:layout_row="1"
android:layout_column="0"
android:layout_width="118dp"
android:layout_height="118dp"
android:scaleType="fitCenter"/>
<ImageButton
app:srcCompat="#drawable/common_google_signin_btn_icon_dark"
android:id="#+id/imageButton8"
android:layout_row="1"
android:layout_column="1"
android:layout_width="118dp"
android:layout_height="118dp"
android:scaleType="fitCenter"/>
<ImageButton
app:srcCompat="#drawable/common_google_signin_btn_icon_dark"
android:id="#+id/imageButton9"
android:layout_row="1"
android:layout_column="2"
android:layout_width="118dp"
android:layout_height="118dp"
android:scaleType="fitCenter"/>
</GridLayout>
</RelativeLayout>
Don't define width yourself. Let GridLayout do this task. Set all width and height to 0dp and all weights to 1.
This might work for you. I didn't check it, but it should work.
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/userSwitch"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:rowCount="2"
android:columnCount="3"
android:alignmentMode="alignMargins">
<ImageButton
app:srcCompat="#drawable/common_google_signin_btn_icon_dark"
android:layout_row="0"
android:layout_column="0"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:scaleType="fitCenter"/>
<ImageButton
app:srcCompat="#drawable/common_google_signin_btn_icon_dark"
android:id="#+id/imageButton5"
android:layout_row="0"
android:layout_column="1"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:scaleType="fitCenter"/>
<ImageButton
app:srcCompat="#drawable/common_google_signin_btn_icon_dark"
android:id="#+id/imageButton6"
android:layout_row="0"
android:layout_column="2"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:scaleType="fitCenter"/>
<ImageButton
app:srcCompat="#drawable/common_google_signin_btn_icon_dark"
android:id="#+id/imageButton7"
android:layout_row="1"
android:layout_column="0"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:scaleType="fitCenter"/>
<ImageButton
app:srcCompat="#drawable/common_google_signin_btn_icon_dark"
android:id="#+id/imageButton8"
android:layout_row="1"
android:layout_column="1"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:scaleType="fitCenter"/>
<ImageButton
app:srcCompat="#drawable/common_google_signin_btn_icon_dark"
android:id="#+id/imageButton9"
android:layout_row="1"
android:layout_column="2"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:scaleType="fitCenter"/>
</GridLayout>
Try this,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/activity_admin_control"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/userSwitch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:text="Switch to User" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_above="#+id/userSwitch"
android:layout_centerHorizontal="true">
<GridLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:alignmentMode="alignMargins"
android:columnCount="3"
android:rowCount="2">
<ImageButton
android:layout_width="118dp"
android:layout_height="118dp"
android:layout_column="0"
android:layout_row="0"
android:scaleType="fitCenter"
app:srcCompat="#drawable/common_google_signin_btn_icon_dark" />
<ImageButton
android:id="#+id/imageButton5"
android:layout_width="118dp"
android:layout_height="118dp"
android:layout_column="1"
android:layout_row="0"
android:scaleType="fitCenter"
app:srcCompat="#drawable/common_google_signin_btn_icon_dark" />
<ImageButton
android:id="#+id/imageButton6"
android:layout_width="118dp"
android:layout_height="118dp"
android:layout_column="2"
android:layout_row="0"
android:scaleType="fitCenter"
app:srcCompat="#drawable/common_google_signin_btn_icon_dark" />
<ImageButton
android:id="#+id/imageButton7"
android:layout_width="118dp"
android:layout_height="118dp"
android:layout_column="0"
android:layout_row="1"
android:scaleType="fitCenter"
app:srcCompat="#drawable/common_google_signin_btn_icon_dark" />
<ImageButton
android:id="#+id/imageButton8"
android:layout_width="118dp"
android:layout_height="118dp"
android:layout_column="1"
android:layout_row="1"
android:scaleType="fitCenter"
app:srcCompat="#drawable/common_google_signin_btn_icon_dark" />
<ImageButton
android:id="#+id/imageButton9"
android:layout_width="118dp"
android:layout_height="118dp"
android:layout_column="2"
android:layout_row="1"
android:scaleType="fitCenter"
app:srcCompat="#drawable/common_google_signin_btn_icon_dark" />
</GridLayout>
</RelativeLayout>
</RelativeLayout>
I find out that it's not possible to center GridView horizontally with .xml
So I had to do it programmatically... This answer helped me a lot:
link
you can try this width and height is harcoded you need to fix either row or column or you can use margin.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="#+id/activity_admin_control"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.myname.myproject.AdminControl">
<Button
android:text="Switch to User"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:id="#+id/userSwitch" />
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/userSwitch"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginLeft="#dimen/activity_vertical_margin"
android:layout_marginRight="#dimen/activity_vertical_margin"
android:rowCount="2"
android:columnCount="3"
android:alignmentMode="alignMargins">
<ImageButton
app:srcCompat="#drawable/common_google_signin_btn_icon_dark"
android:layout_row="0"
android:layout_column="0"
android:layout_width="118dp"
android:layout_height="118dp"
android:scaleType="fitCenter"/>
<ImageButton
app:srcCompat="#drawable/common_google_signin_btn_icon_dark"
android:id="#+id/imageButton5"
android:layout_row="0"
android:layout_column="1"
android:layout_width="118dp"
android:layout_height="118dp"
android:scaleType="fitCenter"/>
<ImageButton
app:srcCompat="#drawable/common_google_signin_btn_icon_dark"
android:id="#+id/imageButton6"
android:layout_row="0"
android:layout_column="2"
android:layout_width="118dp"
android:layout_height="118dp"
android:scaleType="fitCenter"/>
<ImageButton
app:srcCompat="#drawable/common_google_signin_btn_icon_dark"
android:id="#+id/imageButton7"
android:layout_row="1"
android:layout_column="0"
android:layout_width="118dp"
android:layout_height="118dp"
android:scaleType="fitCenter"/>
<ImageButton
app:srcCompat="#drawable/common_google_signin_btn_icon_dark"
android:id="#+id/imageButton8"
android:layout_row="1"
android:layout_column="1"
android:layout_width="118dp"
android:layout_height="118dp"
android:scaleType="fitCenter"/>
<ImageButton
app:srcCompat="#drawable/common_google_signin_btn_icon_dark"
android:id="#+id/imageButton9"
android:layout_row="1"
android:layout_column="2"
android:layout_width="118dp"
android:layout_height="118dp"
android:scaleType="fitCenter"/>
</GridLayout>
</RelativeLayout>
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
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>
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
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>
This warning is because your RelativeLayout contains only GridLayout and both (recyclerView and GridLayout) are set to match_parent
You can remove RelativeLayout and set GridLayout as root layout and it will look exactly like now but better for performance.
<?xml version="1.0" encoding="utf-8"?>
<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"
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">
<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
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>
I'm trying to get an evenly filled grid using GridLayout (API 21). Filling in the horizontal direction works fine using layout_columnWeight attribute. The same with layout_rowWeight attribute fails (see screenshot) . I am pretty clueless.. It seems like the both attributes don't work in the same way.
Also even more simplified layouts show the same behavior (1 row x 2 columns works, 2 rows x 1 column fails). Also explicitly adding of layout_row and layout_column attributes don't change anything.
Please don't answer "use linearLayout". I want to get it working with the GridLayout.
<FrameLayout 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:background="#0099cc"
tools:context=".Locomotion">
<GridLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnCount="2"
android:rowCount="2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="north west"
android:id="#+id/textViewNW"
android:layout_rowSpan="1"
android:layout_rowWeight="1"
android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:layout_gravity="center|fill"
android:background="#fe4141" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="north east"
android:id="#+id/textViewNE"
android:layout_rowSpan="1"
android:layout_rowWeight="1"
android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:layout_gravity="center|fill"
android:background="#51f328" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="south west"
android:id="#+id/textViewSW"
android:layout_rowSpan="1"
android:layout_rowWeight="1"
android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:layout_gravity="center|fill"
android:background="#fefe00" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="south east"
android:id="#+id/textViewSE"
android:layout_rowSpan="1"
android:layout_rowWeight="1"
android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:layout_gravity="center|fill"
android:background="#0080f0" />
</GridLayout>
</FrameLayout>
Thanks in advance for any hint!
Thomas
This was difficult. Had to get creative to solve it. Basically my solution was to nest GridLayouts for each col with a master GridLayout for 1 row. Also each col GridLayout must be in orientation="verticle" with width/height = "wrap_content"
Result:
Here's the code:
<FrameLayout 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:background="#0099cc"
tools:context=".Locomotion">
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:rowCount="1">
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="1"
android:rowCount="2"
android:layout_column="0"
android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:orientation="vertical">
<TextView
android:id="#+id/textViewNW"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_rowSpan="1"
android:layout_rowWeight="1"
android:background="#fe4141"
android:text="north west"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textViewSW"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:layout_row="1"
android:layout_rowSpan="1"
android:layout_rowWeight="1"
android:background="#fefe00"
android:text="south west"
android:textAppearance="?android:attr/textAppearanceLarge" />
</GridLayout>
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="1"
android:rowCount="2"
android:layout_column="1"
android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:orientation="vertical">
<TextView
android:id="#+id/textViewNE"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_rowSpan="1"
android:layout_rowWeight="1"
android:background="#51f328"
android:text="north east"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textViewSE"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:layout_row="1"
android:layout_rowSpan="1"
android:layout_rowWeight="1"
android:background="#0080f0"
android:text="south east"
android:textAppearance="?android:attr/textAppearanceLarge" />
</GridLayout>
</GridLayout>
</FrameLayout>