Paul
Paul

Reputation: 5223

Two spinners next to eachother

I have this problem when i put two spinners next to eachother. Here is the fragment of the layout xml:

...
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="2" >

    <Spinner
        android:id="@+id/x"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top|left"
        android:layout_weight="1" />

    <Spinner
        android:id="@+id/y"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top|right"
        android:layout_weight="1" />
</LinearLayout>

<RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content">

    <ListView
        android:id="@+id/z"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:cacheColorHint="#FFFFFF" >
    </ListView>
...
...

Here is the result:

spinners

I tried many different things. I've tried changing weights, gravity, changing the parent to RelativeLayout, but the result remained the same.

Please help!

EDIT:

Ok. I got it. Some redundancy, but it solves the problem. Kinda weird why this works and the "normal way" doesn't. Thanks for help everyone.

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="2" >

    <Spinner
        android:id="@+id/x"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <RelativeLayout
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="1" >

        <Spinner
            android:id="@+id/y"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true" />
    </RelativeLayout>
</LinearLayout>

Upvotes: 4

Views: 4852

Answers (6)

roshan posakya
roshan posakya

Reputation: 1030

///// You can use spinner widht match parent or 0dp both works

                    <LinearLayout
                        android:orientation="horizontal"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">

                        <Spinner
                            android:layout_marginRight="5dp"
                            android:id="@+id/spinner1"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight="1" />

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

                    </LinearLayout>

Upvotes: 0

Neha
Neha

Reputation: 255

Alternate solution which work for me. I added android:layout_gravity="center_vertical" to spinner as below :

 <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginTop="10dp"
                    android:orientation="horizontal" >

                    <Spinner
                        android:id="@+id/custSpinner"
                        style="@android:style/Widget.Spinner"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:layout_weight="1"
                        />

                    <Spinner
                        android:id="@+id/busSpinner"
                        style="@android:style/Widget.Spinner"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:layout_weight="1"
                        />
                </LinearLayout>

Upvotes: 0

SirG69
SirG69

Reputation: 61

This works fine for me:

<LinearLayout
    android:id="@+id/x"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="2">
    <Spinner
        android:id="@+id/s1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
    <Spinner
        android:id="@+id/s2"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
</LinearLayout>

Upvotes: 5

Shailendra Singh Rajawat
Shailendra Singh Rajawat

Reputation: 8242

althought i cant see anything wrong with this code , but i think you dont need to set gravity ,because wrap_content height will automatically put linearlayout at top and left and right is not the case because both spinners are of half width of screen .

so use :

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

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

Upvotes: -1

n.st
n.st

Reputation: 946

In a RelativeLayout, you could use android:layout_toRightOf="@+id/x" and android:layout_alignTop="@+id/x":

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout>
    <Spinner
        android:id="@+id/x"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <Spinner
        android:id="@+id/y"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/x"
        android:layout_alignTop="@+id/x" />
</RelativeLayout>

(I've left out the gravity definitions as they didn't seem to have an effect on the text in your spinner.)

Upvotes: 1

Ashterothi
Ashterothi

Reputation: 3282

I have never seen that exact problem, but I do see that you have both filling the parent with their width, which may conflict. I would replace that with android:layout_width="0dip" for all elements using weight to determine its size, that way they will actually be equal.

Upvotes: 0

Related Questions