46

view-text-in-the-center-of-the-spinner-when-select-from-the-drop-down-list

I want to align the view text of spinner to center. I google it but didn't find anything, does anybody knows about this? any help or suggestion are appreciated

Aashish Kumar
  • 2,771
  • 3
  • 28
  • 43
Pratik
  • 30,639
  • 18
  • 84
  • 159

10 Answers10

61

Create a adapter for your spinner like this,

ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, R.layout.my_spinner_style,array_of_values) {

    public View getView(int position, View convertView,ViewGroup parent) {

        View v = super.getView(position, convertView, parent);

        ((TextView) v).setTextSize(16);

        return v;

    }

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

        View v = super.getDropDownView(position, convertView,parent);

        ((TextView) v).setGravity(Gravity.CENTER);

        return v;

    }

};

Now your layout R.layout.my_spinner_style,

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+android:id/text1"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:textColor="#ffffff"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee" />

Now set this adapter to your spinner,

spinner.setAdapter(adapter);
Andro Selva
  • 53,910
  • 52
  • 193
  • 240
  • 1
    several themes might require to set the horizontal padding to 0 (or any other value, as long as both sides are equal) of the spinner as well to override spinner details that might offset the inner layout. – SilverCorvus Oct 19 '15 at 15:05
48

You need to set your own layout for spinner item.

SpinnerAdapter adap = new ArrayAdapter<String>(this, R.layout.spinner_item, new String[]{"A", "B", "C"});
spriner.setAdapter(adap);

Where R.layout.spinner_item is a layout with content:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="fill_parent"
    android:layout_width="fill_parent" 
    android:gravity="center" 
    android:textColor="#000000" 
    android:text="Sample Text" 
    android:paddingBottom="5dp" 
    android:paddingTop="5dp"></TextView>
Andro Selva
  • 53,910
  • 52
  • 193
  • 240
ihrupin
  • 6,932
  • 2
  • 31
  • 47
18

This is very simple. Just create a xml file named as, for example, spinner_item.xml with content as

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#111111"
android:padding="10dp"
android:textAlignment="center"
/>

Now in your Java class, instead of adding Android's default view resource add your custom view resource as

ArrayAdapter<String> adapter= new ArrayAdapter<String>(context, R.layout.spinner_item, myList);
 adapter.setDropDownViewResource(R.layout.spinner_item);

The textAlignment line will do the trick.

rajesh
  • 181
  • 1
  • 4
  • 1
    And that is the right answer. At least for me others with gravity led to nothing. – Ivan Apr 16 '17 at 10:05
14

Setting textAlignment to center will place the text in center.

android:textAlignment="center"

        <android.support.v7.widget.AppCompatSpinner
            android:id="@+id/spinner_areas"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerInParent="true"
            android:visibility="visible"
            android:gravity="center"
            android:textAlignment="center"/>
minhazur
  • 4,928
  • 3
  • 25
  • 27
5

This works without layout changes

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
               ((TextView) adapterView.getChildAt(0)).setGravity(Gravity.CENTER);
        }
 });
Borja
  • 1,269
  • 1
  • 17
  • 30
4

Add this line in Spinner

android:textAlignment="center"

Voila!

Sanjit Prasad
  • 398
  • 2
  • 12
3

Late to the game, but I wanted to add this for future users just because I found it a simple solution for my problem. It allowed me to center simple text without creating a custom layout. The way I did this was using padding

<Spinner
    android:id="@+id/example_spinner"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="2dp"
    android:paddingBottom="2dp" />

I was successful in centering my text within my spinner using this method. This will likely only work for simple cases, but you would probably want to use a custom layout for more complex cases anyway. This is just to keep your xml files down and avoid having to use a custom layout just to center a simple spinner.

zgc7009
  • 3,371
  • 5
  • 22
  • 34
  • 3
    Does this really center the text on all devices? Or does it only work on certain devices with specific screen sizes? – SMBiggs Apr 25 '16 at 05:51
1

Simple as this!

<Spinner
 android:textAlignment="center"
/>
danklad
  • 88
  • 8
0

I found it slightly easier to edit the theme than to create an adapter. In my fragment_first.xml I have my spinner

<Spinner
    android:theme="@style/mySpinnerStyle"
    android:id="@+id/spnnr"
    android:layout_width="350dp"
    android:layout_height="75dp"
    android:layout_marginBottom="32dp"
    android:spinnerMode="dropdown"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="1.0" />

I then created a new @style resource in values (recommended by Kotlin).

<style name="mySpinnerStyle" parent="@android:style/Widget.Holo.DropDownItem.Spinner">
    <item name="android:textSize">@dimen/ssp</item>
    <item name="android:textAlignment">center</item>

</style>

<dimen name="ssp">24sp</dimen>

points to Farid here for pointing me along. https://stackoverflow.com/a/64193198/15078702

trs11
  • 13
  • 5
-14

Let me complete answer. It is also important to make space in Spinner.

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