Adding a Spinner to a GridLayout seems to "break" the layout. I have prepared a minimal working example to illustrate the issue:
I want a grid with labels on the left and input controls on the right. The controls on the right should take up the remaining space. This is what a simple example looks like:
Replacing one of the input controls with a spinner causes the right column to extend out of the screen boundaries, leading to an ugly layout.
Why does this happen, and how can I avoid it?
Here's the code of the first example:
<?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:columnCount="2" >
<TextView
android:layout_gravity="left"
android:text="TextView" />
<EditText
android:layout_gravity="fill_horizontal"
android:hint="EditText" />
<TextView
android:layout_gravity="left"
android:text="TextView" />
<EditText
android:layout_gravity="fill_horizontal"
android:hint="EditText" />
</GridLayout>
And here's the code for the second image. The only difference is that the first EditText has been replaced by a Spinner:
<?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:columnCount="2" >
<TextView
android:layout_gravity="left"
android:text="TextView" />
<Spinner
android:id="@+id/spinner1"
android:layout_gravity="fill_horizontal" />
<TextView
android:layout_gravity="left"
android:text="TextView" />
<EditText
android:layout_gravity="fill_horizontal"
android:hint="EditText" />
</GridLayout>
layout_gravity
attribute set toFILL_HORIZONTAL
which stretches(even beyond the visible screen) the views to make them have the same width as their parent(From the docs for theFILL_HORIZONTAL
field: Grow the horizontal size of the object if needed so it completely fills its container.). – Ligation