Alex Alex - 15 days ago 9
Android Question

Why does the 'layout_column' does not place the element in a GridLayout?

I have the following example xml layout, taken from here (removed some unimportant parts):

<?xml version="1.0" encoding="utf-8"?>
<GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:useDefaultMargins="true"
android:alignmentMode="alignBounds"
android:columnOrderPreserved="true"
android:columnCount="4"
>

<TextView
android:text="Email setup"
android:textSize="32dip"
android:layout_columnSpan="4"
android:layout_gravity="center_horizontal"
/>

<TextView
android:text="Email address:"
android:layout_gravity="right"
/>

<EditText
android:ems="10"
/>

<TextView
android:text="Password:"
android:layout_column="0"
android:layout_gravity="right"
/>

<EditText
android:ems="8"
/>

<Space
android:layout_row="4"
android:layout_column="0"
android:layout_columnSpan="4"
android:layout_gravity="fill"
/>

<Button
android:text="Next"
android:layout_row="5"
android:layout_column="2"
/>
</GridLayout>


I am changing the parameter for
android:layout_column
for the 'Next'-button in the last line from 3 to 2, but this does not change it's position on the layout:

Next button position

In my understanding in a Grid-Layout I have several columns, and the parameter
layout_column
specifies the column in which to place the element. But this does not seem to work. The element's position does not change.

Answer

You first column ends on the right of Password (at least it's android:layout_column="0" indicates that)

This is also the start of column 1. But you have not specified any element in this column. So it spans the rest of the width of GridLayout.

It would be better to set column span only to 2, and set button's span to 2 with android:layout_gravity="center_horizontal"

<?xml version="1.0" encoding="utf-8"?>
<GridLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:useDefaultMargins="true"
    android:alignmentMode="alignBounds"
    android:columnOrderPreserved="true"
    android:columnCount="2">

    <TextView
        android:text="Email setup"
        android:textSize="32dip"
        android:layout_columnSpan="2"
        android:layout_gravity="center_horizontal"/>

    <TextView
        android:text="Email address:"
        android:layout_gravity="right"/>

    <EditText
        android:layout_column="1"
        android:ems="10"/>

    <TextView
        android:text="Password:"
        android:layout_column="0"
        android:layout_gravity="right"/>

    <EditText
        android:layout_column="1"
        android:ems="8"/>

    <Space
        android:layout_row="4"
        android:layout_column="0"
        android:layout_gravity="fill"/>

    <Button
        android:text="Next"
        android:layout_row="5"
        android:layout_gravity="center_horizontal"
        android:layout_columnSpan="2"
        android:layout_column="0"/>

</GridLayout>