9

Here is The Code :

spinner.xml :

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="40dp"
android:maxLines="1"
android:gravity="center"
android:singleLine="true"
android:textColor="#FFFFFF"
android:textSize="12sp" />

Spinner :

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

Java :

        array = new ArrayAdapter<String>(Activity.this,
                 R.layout.spinner, ArrayList);
        array .setDropDownViewResource(R.layout.spinner);
        Spinner.setAdapter(array );
        Spinner.setOnItemSelectedListener(this);

Problem : Text is still Aligned to the Left

8 Answers8

14

Use following tag on spinner

android:textAlignment="center"
Pramod Garg
  • 2,183
  • 1
  • 24
  • 29
8

Simply add in your default app theme style this two lines :

<item name="android:spinnerItemStyle">@style/spinnerItemStyle</item>
<item name="android:spinnerDropDownItemStyle">
   @style/spinnerDropDownItemStyle
</item>

create the new style :

<style name="spinnerItemStyle">
    <item name="android:gravity">center</item>
</style>

<style name="spinnerDropDownItemStyle">
    <item name="android:gravity">center</item>
</style>

Thats it !

Note that the base theme used here is : Theme.AppCompat.Light and this will be applied as the default spinner styles in your app.

Mouloud
  • 3,715
  • 1
  • 26
  • 20
6
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:ellipsize="marquee"
    android:textAlignment="center"
    android:textSize="17sp"
    tools:ignore="MissingPrefix" />

Use this Textview as the layout passed to your adapter.
android:textAlignment="center" this line is the one who makes the magic

Vishal Yadav
  • 3,642
  • 3
  • 25
  • 42
Oscar Gallon
  • 180
  • 2
  • 7
4

Add this line in your Spinner,

android:textAlignment="center"

Done!

Sanjit Prasad
  • 398
  • 2
  • 12
3

I've followed this : http://nevescheng.blogspot.fr/2013/05/spinner-with-item-text-aligned-to-center.html & this worked fine

I've noticed that i had another spinner xml in value-v11 Folder & this one was Not Modified, i've Modified it & its worked

3

In your layout add "theme" attribute in Spinner like -

             <Spinner
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="@dimen/_10sdp"
                android:theme="@style/CustomSpinnerTheme"
                android:entries="@array/entries"
                android:gravity="center"
                />

and add style (in values/style file) -

<!-- Spinner style -->
<style name="CustomSpinnerTheme">
    <item name="android:textSize">@dimen/_15ssp</item>
    <item name="android:textColor">@color/textMainColor</item>
    <item name="android:gravity">center</item>
    <item name="android:textAlignment">center</item>
</style>

Enjoy :)

Naresh NK
  • 1,036
  • 1
  • 9
  • 16
2

The best answer and was tested is by Mouloud, but in Marshmallow I had to add textAlignment to make the drop down list items centered. So, the full code based in Mouloud code is:

<!-- Spinner style -->
<style name="spinnerItemStyle">
    <item name="android:textColor">@color/colorAccent</item>
    <item name="android:gravity">center</item>
    <item name="android:textAlignment" tools:targetApi="jelly_bean_mr1">center</item>
</style>

<!-- Spinner style drop down style-->
<style name="spinnerDropDownItemStyle">
    <item name="android:textColor">@color/colorPrimaryDark</item>
    <item name="android:gravity">center</item>
    <item name="android:textAlignment" tools:targetApi="jelly_bean_mr1">center</item>
</style>

And add the style now to your theme style:

<!-- Change the spinner style-->
<item name="android:spinnerItemStyle">@style/spinnerItemStyle</item>
<item name="android:spinnerDropDownItemStyle">@style/spinnerDropDownItemStyle</item>
Mohammed AlBanna
  • 2,350
  • 22
  • 25
0
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    TextView label = new TextView(context);
    label.setText(myObjs[position].getText());
    label.setGravity(Gravity.CENTER);
    return label;
}

@Override
public View getDropDownView(int position, View convertView,
                            ViewGroup parent) {

    TextView label = new TextView(context);
    label.setText(myObjs[position].getText());
    label.setGravity(Gravity.CENTER);
    return label;
}
adiga
  • 34,372
  • 9
  • 61
  • 83
Tony
  • 259
  • 3
  • 13