Mustafa
Mustafa

Reputation: 981

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 *
                         ************************

Upvotes: 7

Views: 2318

Answers (7)

blueware
blueware

Reputation: 5381

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.

Upvotes: 2

ViH
ViH

Reputation: 447

I found the following solution:

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"/>            
    <Spinner
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
</LinearLayout>

Upvotes: 0

ThundeR
ThundeR

Reputation: 31

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.

Upvotes: 3

Chris
Chris

Reputation: 4593

You should add the following to your editText:

android:layout_gravity="center_vertical"

Upvotes: 1

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.

Upvotes: 3

cap511
cap511

Reputation: 11

Add android:baselineAligned="false" to LinearLayout to algn views. This works in my project:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="false"
    android:orientation="horizontal" >

    <EditText
        android:id="@+id/text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
</LinearLayout>

Upvotes: 0

Entreco
Entreco

Reputation: 12900

In the linearlayout, the orientation is "horizontal". Meaning elements are placed side-by-side. Your EditText and Spinner however, both have android:layout_width="fill_parent". So you want to place your EditText & Spinner side by side, but you also want them to fill_parent...

What would you do if you where given these contradictory conditions??

Obviously, this is a contradiction, and different versions of android give different priorities to these attributes.

I would suggest to change the EditText and Spinner attributes to:

<EditText
    ...
    android:layout_width="0dp"
    ... />
<Spinner
    ...
    android:layout_width="0dp"
    ... />

And add this to your LinearLayout:

<LinearLayout
    android:baselineAligned="true"
    ... />

Upvotes: 0

Related Questions