2

Maybe you will see something that i didn't. I don't understand why it refuses to align. I have 2 spinners in layout and tried to align text to center. In both spinners, the text is aligned to the left. Here is the code.

Xml code :

<LinearLayout
       android:layout_width="fill_parent"
       android:layout_height="36dp"
       android:background="@color/orange"
       android:gravity="bottom|center_vertical" >

       <Spinner
           android:id="@+id/sub_from_category"
           android:layout_width="wrap_content"
           android:layout_gravity="center"
           android:layout_weight="2"
           android:layout_height="35dp"
           android:background="@drawable/spinners" />

       <View
           android:layout_width="2dp"
           android:layout_height="wrap_content"
           android:layout_weight="0"
           android:layout_marginLeft="1dp"
           android:layout_marginRight="1dp"
           android:background="@android:color/darker_gray" />

       <Spinner
           android:id="@+id/sub_to_category"
           android:layout_width="wrap_content"
           android:layout_gravity="center"
           android:layout_weight="2"  
           android:layout_height="35dp"
           android:background="@drawable/spinners" />
   </LinearLayout>

xml code :

<TextView
    android:id="@+id/spinnerTarget"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textColor="#454545"
    android:background="@color/orange"
    android:textSize="1/>

And Java Activity :

subFromCategory = (Spinner)findViewById(R.id.sub_from_category);
        subToCategory = (Spinner)findViewById(R.id.sub_to_category);


        ArrayAdapter<String> adapterF = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,Angle);
        adapterF.setDropDownViewResource(R.layout.sub_spinner);
        subFromCategory.setAdapter(adapterF);

        ArrayAdapter<String> adapterT = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,Angle);
        adapterT.setDropDownViewResource(R.layout.sub_spinner);
        subToCategory.setAdapter(adapterT);

Thank you, Niro

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
Niro
  • 33
  • 1
  • 2
  • 8

2 Answers2

1

Always remember this :

layout gravity - a property which specifies the 'gravity' of an object itself, in its parent view.

gravity - a property which specifies the 'gravity' of the subviews inside the object.

Vinay W
  • 9,912
  • 8
  • 41
  • 47
  • Thanks for the explanation. I've tried it in so many way. When i did the separate xml for spinner it worked good for a single spinner in line, But the problem is that it won't align to the middle when there is 2 spinners in the same line. – Niro Nov 24 '12 at 03:51
  • 5
    the gravity and layout gravity do not work properly with the Spinner. – Eduard Mar 21 '14 at 19:00
1

I had same problem and solution was "simple". You must have "space" for set it in the center.

Wrong:

<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

Right:

<Spinner
android:id="@+id/spinner"
android:layout_width="100dp"
android:layout_height="wrap_content"
/>
Michal
  • 2,071
  • 19
  • 30