Spinner inside GridLayout

Adding Spinner to GridLayout seems to break the layout. I prepared a minimal working example to illustrate the problem:

I need a grid with labels on the left and controls on the right. The controls on the right should occupy the remaining space. Here is a simple example:

Grid with edittexts

Replacing one of the I / O controls causes the right column to go beyond the borders of the screen, resulting in an ugly layout.

Grid with spinner

Why is this happening, and how can I avoid it?


Here is the code for 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 is the code for the second image. The only difference is that the first EditText was replaced by 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> 
+8
android layout android-gridlayout
source share
2 answers

Apparently, the problem can be avoided by setting the layout_width Spinner to zero:

 <Spinner android:id="@+id/Spinner1" android:layout_gravity="fill_horizontal" android:layout_width="0dp" /> 

I will mark this as an accepted answer, as it easily fixes the problem, but I will gladly change it if anyone can come up with an explanation for this behavior.

+16
source share
 try this code <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TableRow android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /> <Spinner android:id="@+id/spinner1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_weight="1" /> </TableRow> <TableRow android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_weight="1" android:hint="EditText" /> </TableRow> </TableLayout> 
+1
source share

All Articles