18

I have a spinner with a style. The style only contains a background for the spinner. The problem is,that no matter which image I use for the background, the text of the spinner is always pushed to the left.

This is the declaration of the Spinner in the XML -

<Spinner
 android:layout_height="wrap_content"
 android:layout_toRightOf="@+id/minus"
 android:layout_width="wrap_content"
 android:layout_below="@+id/female"
 android:id="@+id/spin"
 android:gravity="center"
 android:background="@drawable/spin"
 android:layout_marginTop="10dip">
 </Spinner>

Also, I get a warning under the android:gravity attribute that says it's an unknown XML attribute.

I can't figure why it does that.

Thanks

Tofira
  • 1,634
  • 4
  • 27
  • 39
  • Were you to post complete minimal code and resources, e.g., image files, needed to replicate your problem, along with a screenshot of the problem, and maybe even a mockup or screenshot of the goal, it'd probably help others to help you. – Thane Anthem Apr 22 '11 at 22:50
  • Unfortunately, you still haven't provided complete minimal code to replicate your problem, so potential helpers are left to guess what the missing code might look like. Are you using android.R.layout.simple_spinner_dropdown_item and android.R.layout.simple_spinner_item? I don't know, but I'd guess that you are. A solution to your problem involves not using those two simple layouts, but providing a custom layout, instead. – Thane Anthem Apr 23 '11 at 05:02

5 Answers5

38

Continuing from my last comment above...

The following code modifies the Hello Spinner tutorial application to display the spinner text contents centered horizontally, as seen in the following two screenshots.

res/layout/my_spinner_textview.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    style="?android:attr/spinnerItemStyle"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center" />
public class HelloSpinner extends Activity
{
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Spinner spinner = (Spinner) findViewById(R.id.spinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.planets_array,
//        android.R.layout.simple_spinner_item);
        R.layout.my_spinner_textview);
//    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    adapter.setDropDownViewResource(R.layout.my_spinner_textview);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
  }

  //No other modification needed.

This hopefully provides enough direction to fix the problem.

Thane Anthem
  • 4,093
  • 4
  • 26
  • 24
6

Use android:textAlignment="center" tag on spinner

 <android.support.v7.widget.AppCompatSpinner
        android:id="@+id/state"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center"/>
Pramod Garg
  • 2,183
  • 1
  • 24
  • 29
4

Add gravity in your xml file...

<Spinner
 android:gravity="center">
</Spinner>

Or add gravity in your java code...

Spinner spinner = (Spinner)findViewById(R.id.spinner);
spinner.setGravity(17);
//17 = center
Tom O
  • 1,780
  • 2
  • 20
  • 43
  • Did you ever figure out what is missing? The accepted answer also has the gravity attribute, but the code itself seems to be superfluous as my spinners are set up from the XML without needing any code. – Michael Sep 21 '13 at 23:25
1

I had this problem as well and the accepted answer did not work to me.

If this also applies to you, make sure that no parent layout overrides the gravity of the spinner item. In my case, I had to set the gravity of the entire Spinner to right.

0

If you want to see the checkboxes in the dropdown menu while using your own layout:

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            mContext, R.array.room_list, R.layout.spinner_layout);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    yourSpinner.setAdapter(adapter);

Please notice the "android" before "R.layout...." in the line where you put the drop down view resource!

Camino2007
  • 796
  • 10
  • 17