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>
Related
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"/>
I am creating a 2x3 GridLayout in my Android application. The three columns are supposed to have equal width and the TextViews are supposed to be centered in the columns. However, my layout is the image below, where not all the TextViews are horizontally aligned within their columns. This happens despite the fact that I set the gravity to center_horizontal for each of the TextViews.
For example: the TextView with the text "abcd" leans more to the right.
Below is my code:
<?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="wrap_content">
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10sp"
android:id="#+id/overall_statistics"
>
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="15sp"
android:paddingTop="10sp"
android:paddingRight="15sp"
android:paddingBottom="10sp"
android:columnCount="3"
android:rowCount="2"
>
<TextView
android:layout_row="0"
android:layout_rowSpan="1"
android:layout_column="0"
android:layout_columnSpan="1"
android:layout_gravity="fill_horizontal"
android:gravity="bottom|center_horizontal"
android:text="4452"
android:layout_width="0dp"
android:layout_columnWeight="1"
android:textColor="#000000"
android:textSize="24sp" />
<TextView
android:layout_width="0dp"
android:layout_row="1"
android:layout_rowSpan="1"
android:layout_column="0"
android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:layout_gravity="fill_horizontal"
android:gravity="top|center_horizontal"
android:text="abcdef"
android:textColor="#000000"
android:textSize="16sp" />
<TextView
android:layout_row="0"
android:layout_rowSpan="1"
android:layout_column="1"
android:layout_columnSpan="1"
android:layout_gravity="fill_horizontal"
android:gravity="bottom|center_horizontal"
android:paddingLeft="16sp"
android:layout_width="0dp"
android:layout_columnWeight="1"
android:text="2000"
android:textColor="#000000"
android:textSize="24sp" />
<TextView
android:id="#+id/textView"
android:layout_width="0dp"
android:layout_row="1"
android:layout_rowSpan="1"
android:layout_column="1"
android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:layout_gravity="fill_horizontal"
android:gravity="top|center_horizontal"
android:paddingLeft="16sp"
android:text="abcd"
android:textColor="#000000"
android:textSize="16sp" />
<TextView
android:layout_row="0"
android:layout_rowSpan="1"
android:layout_column="2"
android:layout_columnSpan="1"
android:layout_gravity="fill_horizontal"
android:gravity="bottom|center_horizontal"
android:paddingLeft="16sp"
android:layout_columnWeight="1"
android:layout_width="0dp"
android:text="2252"
android:textColor="#000000"
android:textSize="24sp" />
<TextView
android:layout_width="0dp"
android:layout_row="1"
android:layout_rowSpan="1"
android:layout_column="2"
android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:layout_gravity="fill_horizontal"
android:gravity="top|center_horizontal"
android:paddingLeft="16sp"
android:text="abcdefgh"
android:textColor="#000000"
android:textSize="16sp" />
</GridLayout>
</com.google.android.material.card.MaterialCardView>
</LinearLayout>
Remove android:paddingLeft="16sp" of your textViews in 2nd and 3rd columns.
<?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="wrap_content">
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:id="#+id/overall_statistics"
>
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="15dp"
android:paddingTop="10dp"
android:paddingRight="15dp"
android:paddingBottom="10dp"
android:columnCount="3"
android:rowCount="2"
>
<TextView
android:layout_row="0"
android:layout_rowSpan="1"
android:layout_column="0"
android:layout_columnSpan="1"
android:layout_gravity="fill_horizontal"
android:gravity="bottom|center_horizontal"
android:text="4452"
android:layout_width="0dp"
android:layout_columnWeight="1"
android:textColor="#000000"
android:textSize="24sp" />
<TextView
android:layout_width="0dp"
android:layout_row="1"
android:layout_rowSpan="1"
android:layout_column="0"
android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:layout_gravity="fill_horizontal"
android:gravity="top|center_horizontal"
android:text="abcdef"
android:textColor="#000000"
android:textSize="16sp" />
<TextView
android:layout_row="0"
android:layout_rowSpan="1"
android:layout_column="1"
android:layout_columnSpan="1"
android:layout_gravity="fill_horizontal"
android:gravity="bottom|center_horizontal"
android:layout_width="0dp"
android:layout_columnWeight="1"
android:text="2000"
android:textColor="#000000"
android:textSize="24sp" />
<TextView
android:id="#+id/textView"
android:layout_width="0dp"
android:layout_row="1"
android:layout_rowSpan="1"
android:layout_column="1"
android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:layout_gravity="fill_horizontal"
android:gravity="top|center_horizontal"
android:text="abcd"
android:textColor="#000000"
android:textSize="16sp" />
<TextView
android:layout_row="0"
android:layout_rowSpan="1"
android:layout_column="2"
android:layout_columnSpan="1"
android:layout_gravity="fill_horizontal"
android:gravity="bottom|center_horizontal"
android:layout_columnWeight="1"
android:layout_width="0dp"
android:text="2252"
android:textColor="#000000"
android:textSize="24sp" />
<TextView
android:layout_width="0dp"
android:layout_row="1"
android:layout_rowSpan="1"
android:layout_column="2"
android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:layout_gravity="fill_horizontal"
android:gravity="top|center_horizontal"
android:text="abcdefgh"
android:textColor="#000000"
android:textSize="16sp" />
</GridLayout>
</com.google.android.material.card.MaterialCardView>
</LinearLayout>
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 am a beginner and I created 8 buttons on gridLayout.The size of buttons are different here is a xml code:
<GridLayout
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_gravity="fill"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="Hello"
android:id="#+id/hello"
android:onClick="buttonTapped"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="0"
android:layout_gravity="fill"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="How are you"
android:id="#+id/howareyou"
android:onClick="buttonTapped"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="1"
android:layout_gravity="fill"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="good evening"
android:id="#+id/goodevening"
android:onClick="buttonTapped"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="1"
android:layout_gravity="fill"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="please"
android:id="#+id/please"
android:onClick="buttonTapped"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="2"
android:layout_gravity="fill"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="my name is"
android:id="#+id/mynameis"
android:onClick="buttonTapped"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="2"
android:layout_gravity="fill"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="do you
speak english"
android:id="#+id/doyouspeakenglish"
android:onClick="buttonTapped"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="3"
android:layout_gravity="fill"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="welcome"
android:id="#+id/welcome"
android:onClick="buttonTapped"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="3"
android:layout_gravity="fill"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="i live in"
android:id="#+id/ilivein"
android:onClick="buttonTapped"/>
</GridLayout>
and here is the screenShot on Lg g3
real device Lg g3
and here is the screenShot on emulator emulator
what is the problem?
try LinearLayout with weightsum
<?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"
android:weightSum="4"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2"
android:padding="5dp"
>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Hello"
android:id="#+id/hello"
android:layout_marginRight="5dp"
android:layout_marginTop="6dp"
android:layout_marginBottom="3dp"
android:onClick="buttonTapped"
/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="How are you"
android:id="#+id/howareyou"
android:layout_marginLeft="5dp"
android:layout_marginTop="6dp"
android:layout_marginBottom="3dp"
android:onClick="buttonTapped"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2"
android:padding="5dp"
>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="good evening"
android:id="#+id/goodevening"
android:layout_marginRight="5dp"
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp"
android:onClick="buttonTapped"
/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="please"
android:id="#+id/please"
android:layout_marginLeft="5dp"
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp"
android:onClick="buttonTapped"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2"
android:padding="5dp"
>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="my name is"
android:id="#+id/mynameis"
android:layout_marginRight="5dp"
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp"
android:onClick="buttonTapped"
/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="do you
speak english"
android:id="#+id/doyouspeakenglish"
android:layout_marginLeft="5dp"
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp"
android:onClick="buttonTapped"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2"
android:padding="5dp"
>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="welcome"
android:id="#+id/welcome"
android:layout_marginRight="5dp"
android:layout_marginTop="3dp"
android:layout_marginBottom="6dp"
android:onClick="buttonTapped"
/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Hello"
android:layout_marginLeft="5dp"
android:layout_marginTop="3dp"
android:layout_marginBottom="6dp"
android:onClick="buttonTapped"
/>
</LinearLayout>
</LinearLayout>
you should fix your height and width of the button in dimension file for various resolution example:
values folder:
<dimen name="width">90dp</dimen>
<dimen name="height">75dp</dimen>
values-hdpi folder:
<dimen name="width">100dp</dimen>
<dimen name="height">85dp</dimen>
like this do for mdpi,,xhdpi,xxhdpi and put this in your button.
android:layout_width="#dimen/width"
android:layout_height="#dimen/height"
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>