Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spinner floats down when horizontal aligned

If I add spinner in a horizontal LinearLayout or in a table row for example like this:

   <LinearLayout
        android:id="@+id/linearLayout3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/editText1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >
        </EditText>

        <Spinner
            android:id="@+id/spinner1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </LinearLayout>

The spinner floats down. It floats down on android 2.2, android 2.3 and android 3.2 but it works well in android 4.0 they fixed it. But is there way to make it align with the other Views like the EditText on android 2.3.

By floats down I mean:

***********************
* if EditText is here *  ************************
***********************  * Spinner will be here *
                         ************************
like image 991
Mustafa Avatar asked Dec 20 '11 20:12

Mustafa


4 Answers

Old post, but it may help :

setting baselineAligned to true is unecessary as it's its default value (explains here : http://developer.android.com/reference/android/widget/LinearLayout.html#attr_android:baselinealigned)

I've fixed it by hardcoding a height to both textview and spinner, and setting baselineAligned to FALSE.

like image 188
Sylvain Hébuterne Avatar answered Nov 15 '22 20:11

Sylvain Hébuterne


Spinner is smaller than EditText, so a way to align both it's to add android:layout_height="match_parent" to Spinner, and keep LinearLayout and EditText with wrap_content.

like image 40
ThundeR Avatar answered Nov 15 '22 20:11

ThundeR


I faced the same trouble before and I figured out the answer after trying and modifying my xml, here is my xml code:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/linearlayout1"
    android:orientation="horizontal"
    android:gravity="bottom">

<android.support.design.widget.TextInputLayout
    android:id="@+id/text_input_layout"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="50">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:textSize="20dp"
        android:hint="Duration"
        android:layout_gravity="left"
        android:gravity="center"
        />
</android.support.design.widget.TextInputLayout>
<Spinner
    android:id="@+id/spinner"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:layout_weight="50"
    />

Just set the gravity of your LinearLayout to bottom and will solve your problem.

like image 40
blueware Avatar answered Nov 15 '22 18:11

blueware


You should add the following to your editText:

android:layout_gravity="center_vertical"
like image 1
Chris Avatar answered Nov 15 '22 20:11

Chris