Wrong gravity applied to LinearLayout items

89 Views Asked by At

After applying Gravity properties to my XML items, they don't seem to be positioned in the right place. How can these issues be fixed so that the following conditions are met?:

  1. The 'move up' button needs to be at the top of the screen
  2. The 'move down' button needs to be at the bottom of the screen
  3. The GridView needs to be in the vertical centre of the screen

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/btn_moveup"
        android:text="move up"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_gravity="top"
        />

    <GridView
        android:id="@+id/abslistview_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:numColumns="auto_fit"
        />

    <Button
        android:id="@+id/btn_movedown"
        android:text="move down"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_gravity="bottom"
        />
</LinearLayout>

UPDATE (akshay_shahane's suggestion)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:layout_gravity="center_vertical"
    android:orientation="vertical"
    android:weightSum="100"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/btn_moveup"
        android:text="move up"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_weight="5"
        android:onClick="moveup_click"
        />

    <GridView
        android:id="@+id/abslistview_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="90"
        android:layout_gravity="center_vertical"
        android:numColumns="auto_fit"
        />

    <Button
        android:id="@+id/btn_movedown"
        android:text="move down"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_weight="5"
        android:onClick="movedown_click"
        />
</LinearLayout>

enter image description here

3

There are 3 best solutions below

0
On

Either set the weight of the GridLayout to 1 and layout_height to 0dp like this

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/btn_moveup"
        android:text="move up"
        android:layout_height="wrap_content"
        android:layout_width="match_parent" />

    <GridView
        android:id="@+id/abslistview_main"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:numColumns="auto_fit"
        android:layout_weight="1"
        />

    <Button
        android:id="@+id/btn_movedown"
        android:text="move down"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        />
</LinearLayout>

Or wrap the grid layout in another container like this.

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/btn_moveup"
        android:text="move up"
        android:layout_height="wrap_content"
        android:layout_width="match_parent" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <GridView
            android:id="@+id/abslistview_main"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:numColumns="auto_fit" />

    </LinearLayout>

    <Button
        android:id="@+id/btn_movedown"
        android:text="move down"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        />
</LinearLayout>

The second option would be better because it allows the GridLayout to be centered by using android:layout_gravity="center".

0
On

Try this

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:weightSum="1"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/btn_moveup"
        android:text="move up"
        android:layout_weight="0.1"
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:layout_gravity="top"
        />

    <GridView
        android:id="@+id/abslistview_main"
        android:layout_width="match_parent"
        android:layout_weight="0.8"
        android:layout_height="0dp"
        android:layout_gravity="center_vertical"
        android:numColumns="auto_fit"
        />

    <Button
        android:id="@+id/btn_movedown"
        android:text="move down"
        android:layout_weight="0.1"
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:layout_gravity="bottom"
        />
</LinearLayout>
0
On

Just need to set gridview weight to 1

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
    android:id="@+id/btn_moveup"
    android:text="move up"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:layout_gravity="top"
    />

<GridView
    android:id="@+id/abslistview_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:layout_weight="1"
    android:layout_gravity="center_vertical"
    android:numColumns="auto_fit"
    />
<Button
    android:id="@+id/btn_movedown"
    android:text="move down"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:layout_gravity="bottom"
    />
</LinearLayout>