504

In my Android application, I am using spinner, and I have loaded data from the SQLite database into the spinner, and it's working properly. Here is the code for that.

Spinner spinner = (Spinner) this.findViewById(R.id.spinner1);
List<String> list = new ArrayList<String>();
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>  (this,android.R.layout.simple_spinner_item, list);
cursor.moveToFirst();

list.add("All Lists");

if (cursor.getCount() > 0) {
    for (int i = 0; i < cursor.getCount(); i++) {
        keyList[i] = cursor.getString(cursor.getColumnIndex(AndroidOpenDbHelper.KEYWORD));
        list.add(keyList[i]);
        cursor.moveToNext();
    }
}
Database.close();
cursor.close();
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);

Now I want to change the text color and text size of spinner data. I have used following XML lines to my spinner tag on my XML file, but it is not working.

android:textColor="@android:color/white"
android:textSize="11dp"

How can I change the text color and text size of my spinner?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dinesh Anuruddha
  • 7,137
  • 6
  • 32
  • 45
  • check the following sites [StackoverFlow][1] [stackoverflow ][2] [stackoverflow][3] I think this is duplicate question [1]: http://stackoverflow.com/questions/4880413/text-color-of-a-closed-spinner [2]: http://stackoverflow.com/questions/4989817/set-the-textsize-to-a-text-in-spinner-in-android-programatically [3]: http://stackoverflow.com/questions/5836254/android-change-text-color-of-items-in-spinner – user1203673 Feb 28 '12 at 05:10
  • try this post http://stackoverflow.com/questions/6159113/android-where-is-the-spinner-widgets-text-color-attribute-hiding – Ajay Feb 28 '12 at 05:11
  • 1
    For textSize the correct is to use SP... android:textSize="11sp" – Diego Venâncio Feb 08 '18 at 19:23

25 Answers25

889

Make a custom XML file for your spinner item.

spinner_item.xml:

Give your customized color and size to text in this file.

<?xml version="1.0" encoding="utf-8"?>

<TextView  
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:gravity="left"  
    android:textColor="#FF0000"         
    android:padding="5dip"
    />

Now use this file to show your spinner items like:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_item,list);

You don't need to set the drop down resource. It will take spinner_item.xml only to show your items in spinner.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hiral Vadodaria
  • 19,158
  • 5
  • 39
  • 56
  • 2
    @Hiral text will be white just for current selected item. – nsinvocation Mar 11 '13 at 12:43
  • 3
    @vilpe89 could you modify your above code to show what you mean by this? where does this android:id go? certainly not in the spinner tag, because it has it's own id. but it doesn't make sense to add it in the textview tag you have created above. how do i reference that i want to use this tag directly from the XML? also, when creating the XML from Eclipse, I don't see any option to create an XML file containing only a TextView, how does that work??? – Michael Sep 21 '13 at 23:34
  • Well I have read my comment many times and not sure what I have been thinking by that time :) – vilpe89 Sep 24 '13 at 09:34
  • 2
    This changed the color of the selected item, but when I open the spinner, the content is still default... on my Nexus 7 at least – ChrisBorg Oct 17 '13 at 13:27
  • @Hiral, can we use selector as background of `Spinner`? – Pratik Butani Apr 09 '14 at 04:43
  • 38
    Just a little note. Textsize should be using **sp** instead of **dp/dip** – ymerdrengene May 22 '14 at 07:50
  • 1
    In which folder should I put custom TextView- in drawable, Layout, Values, etc? – GyRo Feb 26 '15 at 12:03
  • 3
    Hi, as @GyRo asked, the .xml file must be located inside your "layout" folder. – bheatcoker Jun 04 '15 at 10:46
  • @Hiral How can you change typeface in that TextView from layout?? – KryNaC Jun 18 '15 at 13:21
  • 1
    Hi, i used this solution but the textcolor is not changing. Do i have to do something else? – vgarzom Nov 25 '15 at 15:21
  • 29
    can't this be done by using style only? I am surprised how small stuff is so difficult to style in android.. this is frustrating.. I wished they had a styling system like css – yeahman Mar 14 '16 at 18:54
  • 3
    If you want to set the dropdown textview too, call `setDropDownViewResource(R.layout.setting_value_spinner_value);` – Scaraux Jul 07 '16 at 15:27
  • 1
    use id: android:id="@android:id/text1" – ashishdhiman2007 Jun 30 '17 at 09:17
  • 1
    but what about ARROW's color? – user924 Apr 21 '18 at 21:04
  • 2
    i think spinner should have this features! its look like weakness – masoud jafari Oct 30 '19 at 10:20
  • I think the method ArrayAdapter.createFromResource should be used and the strings put in array in the strings.xml – Belial Jan 07 '20 at 12:42
  • 1
    @yeahman it can be done through styling as well. You need to define it in the `styles.xml` and then in the layout file in the spinner, you can write `android:theme="@style/Spinner"`. Check out the answers below. – Simran Sharma Jan 21 '21 at 00:58
  • Also, we can get the entries from array like: `mySpinner.adapter = ArrayAdapter(this, R.layout.item_spinner, resources.getStringArray(R.array.array_options) )` – Shailendra Madda Mar 16 '22 at 09:50
210

Simple and crisp...:

private OnItemSelectedListener OnCatSpinnerCL = new AdapterView.OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {

       ((TextView) parent.getChildAt(0)).setTextColor(Color.BLUE);
       ((TextView) parent.getChildAt(0)).setTextSize(5);

    }

    public void onNothingSelected(AdapterView<?> parent) {

    }
};
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ashraf
  • 3,114
  • 3
  • 23
  • 22
  • 24
    This is the best solution for me, because I needed to change the color of the text in the spinner, but not in the appearing list. Thank you. – peter.bartos Sep 22 '12 at 11:27
  • 7
    For me, the item color blinks, going from original color to the color I choosed – Felipe Conde May 28 '14 at 15:03
  • @FelipeConde Did you ever find an answer? I have the same question. My question is here: http://stackoverflow.com/questions/33908564/setting-spinner-text-color-programmatically-lags-is-slow-is-wrong-color-for-sp – Rock Lee Nov 25 '15 at 05:42
  • @FelipieConde For you ,it takes the ascent color at first , then you change into other color manually , So it look so tired. At First It Takes @color/color_primary Then you manually changed by programmatic, So you feel the blink – Ashraf Nov 26 '15 at 06:05
  • The above kept crashing for me so I changed it to TextView tv = (TextView) view; tv.setTextColor(Color.BLUE); – rharvey Apr 04 '16 at 16:08
  • 2
    this worked for me but my problem is when i return from a fragment app crashes as it gets a null pointer on the above code – Akshay Shah Oct 04 '16 at 08:48
  • To avoid null pointer, add a check like if (((TextView) parent.getChildAt(0)) != null) { ((TextView) parent.getChildAt(0)).setTextColor(Color.WHITE); } – bitsabhi Jan 02 '18 at 17:05
  • The app also crashes due to NPE after screen rotation :/ – yital9 Sep 11 '19 at 19:35
157

If all the spinners may have the same text color for their TextView items, another approach is to use a custom style for spinner dropdown items:

In res/values/styles.xml:

<resources>
    <style name="AppBaseTheme" parent="android:Theme.Light">
    </style>

    <style name="AppTheme" parent="AppBaseTheme">
        <item name="android:spinnerDropDownItemStyle">@style/mySpinnerItemStyle</item>
    </style>

    <style name="mySpinnerItemStyle" parent="@android:style/Widget.Holo.DropDownItem.Spinner">
        <item name="android:textColor">@color/my_spinner_text_color</item>
    </style>
</resources>

And define your custom color in res/values/colors.xml:

<color name="my_spinner_text_color">#808080</color>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Whypee
  • 1,571
  • 1
  • 9
  • 2
  • 2
    And how to apply this solution to Appcompt theme? I dtried it by overriding the parent theme "@style/Widget.AppCompat.DropDownItem.Spinner" and it didn't work out – GyRo Feb 26 '15 at 11:58
  • 11
    I would add also add: @style/mySpinnerItemStyle This ensures that the collapsed text has the same color - in case this is the desired behavior. – infinite-loop Oct 30 '15 at 18:41
  • 4
    does not works for me neither, the style is completely ignored – diogo.abdalla Nov 09 '15 at 14:45
  • 1
    @Whypee please edit your answer according to the suggestions of infinite-loop it worked for me perfectly – Chintan Desai Jul 06 '16 at 13:31
  • 5
    For AppCompat theme just remove `android:` like this `@style/mySpinnerItemStyle` – Roman_D Sep 28 '16 at 06:59
  • 1
    these attributes only work when using @android:layout/simple_spinner_item as the item layout https://code.google.com/p/android/issues/detail?id=206244#c2 – netimen Dec 20 '16 at 10:32
  • 4
    To be clear, you have to specify the style as a theme, or it won't work: `` – tir38 May 03 '17 at 19:00
  • @netimen I used the simple_spinner_item instead of simple_list_item1, but still not working – FindOut_Quran Sep 02 '18 at 03:36
82

Here is a link that can help you to change the color of the Spinner:

Click here

<Spinner
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/spinner"
    android:textSize="20sp"
    android:entries="@array/planets"/>

You need to create your own layout file with a custom definition for the spinner item spinner_item.xml:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:textColor="#ff0000" />

If you want to customize the dropdown list items, you will need to create a new layout file. spinner_dropdown_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="?android:attr/spinnerDropDownItemStyle"
    android:maxLines="1"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:ellipsize="marquee"
    android:textColor="#aa66cc"/>

And finally another change in the declaration of the spinner:

ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
R.array.planets_array, R.layout.spinner_item);

adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
spinner.setAdapter(adapter);

That's it.

Codeversed
  • 9,287
  • 3
  • 43
  • 42
Niib Fouda
  • 1,383
  • 1
  • 16
  • 21
  • 2
    This was an automated response that came from reviewing posts by new users. Glad to see that you edited the answer! :) – Marko Aug 18 '15 at 17:42
  • 2
    It seems, that `android:textSize="20sp"`in the first code snippet is useless: the textsize of the control is defined in the `spinner_item.xml` – DenisGL Dec 30 '16 at 18:42
32

If you work with android.support.v7.widget.AppCompatSpinner here is the simplest tested solution using styles:

 <android.support.v7.widget.AppCompatSpinner
                    android:id="@+id/spefcialFx"
                    style="@style/Widget.AppCompat.Spinner.Underlined"
                    android:layout_width="200dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="4dp"
                    android:theme="@style/Spinner"
                    android:entries="@array/special_fx_arrays"
                    android:textSize="@dimen/text_size_normal"></android.support.v7.widget.AppCompatSpinner>

And the style:

<style name="Spinner" parent="Widget.AppCompat.Light.DropDownItem.Spinner">
        <item name="android:paddingStart">0dp</item>
        <item name="android:paddingEnd">0dp</item>
        <item name="android:textColor">@color/white</item>
        <item name="android:backgroundTint">@color/red</item>
        <item name="android:textSize">14sp</item>
    </style>

The only downside is the android:backgroundTint sets color for both the dropdown arrow and the dropdown background.

voytez
  • 1,814
  • 16
  • 16
21

To prevent lagging, you need to not only set the text properties in the onItemSelected listener, but also in the Activity's onCreate method (but it's a little tricky).

Specifically, you need to put this in onCreate after setting the adapter:

spinner.setSelection(0, true);
View v = spinner.getSelectedView();
((TextView)v).setTextColor(backgroundColor);

And then put this in onItemSelected:

((TextView) view).setTextColor(backgroundColor);

Here is a full example:

@Override  
protected void onCreate(Bundle savedInstanceState)
{  
    Spinner spinner = (Spinner) findViewById(R.id.spinner); 

    //Set the choices on the spinner by setting the adapter.  
    spinner.setAdapter(new SpinnerAdapter(toolbar.getContext(), new String[]{"Overview", "Story", "Specifications", "Poll", "Video"}, accentColor, backgroundColor));

    //Set the text color of the Spinner's selected view (not a drop down list view) 
    spinner.setSelection(0, true);
    View v = spinner.getSelectedView();
    ((TextView)v).setTextColor(backgroundColor);

    //Set the listener for when each option is clicked.  
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
    {  

        @Override  
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
        {  
           //Change the selected item's text color  
           ((TextView) view).setTextColor(backgroundColor);
        }  

        @Override  
        public void onNothingSelected(AdapterView<?> parent)
        {  
        }  
    });  

}  

For more details, see my question.

Community
  • 1
  • 1
Rock Lee
  • 9,146
  • 10
  • 55
  • 88
18

If you want the text color to change in the selected item only, then this can be a possible workaround. It worked for me and should work for you as well.

spinner.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                ((TextView) spinner.getSelectedView()).setTextColor(Color.WHITE);
            }
        });
vishal-wadhwa
  • 1,021
  • 12
  • 18
10

we can change style of spinner textview set theme for spinner like this:

styles.xml:

<style name="mySpinnerItemStyle" parent="@android:style/Widget.Holo.DropDownItem.Spinner">
    <item name="android:textSize">@dimen/_11ssp</item>
    <item name="android:textColor">@color/blue</item>
    <item name=...</item>
</style>

then

<Spinner
                    android:theme="@style/mySpinnerItemStyle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>

if you want to change spinner textview attribute programtically:

Programatically:

val textView = (view.getChildAt(0) as TextView)
textView.setTextColor(resources.getColor(R.color.dark_mode))
9

For someone who needs only Style way for AppCompat.

Result
enter image description here enter image description here

styles.xml

<resources>
    ... 
    <style name="Spinner" parent="Widget.AppCompat.Light.DropDownItem.Spinner">
        <item name="android:paddingStart">0dp</item>
        <item name="android:paddingEnd">0dp</item>
        <item name="android:textColor">@color/material_grey_700</item>
        <item name="android:textSize">12sp</item>
    </style>
</resources>

your_spinner_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
    ...

    <android.support.v7.widget.AppCompatSpinner
        android:id="@+id/content_spinner"
        style="@style/Widget.AppCompat.Spinner.Underlined"
        android:layout_width="140dp"
        android:layout_height="wrap_content"
        android:entries="@array/shipping_tracking_carrier_names"
        android:spinnerMode="dropdown"
        android:theme="@style/Spinner" />

    <EditText
        android:id="@+id/content_input"
        android:layout_width="140dp"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:maxLines="1"
        android:paddingEnd="8dp"
        android:paddingStart="8dp"
        android:textColor="@color/material_grey_700"
        android:textSize="12sp" />

    ...
</LinearLayout>    

Plus
And if you want to set android:entries programmatically with defined style.
Try this.

AppCompatSpinner spinner = findViewById(R.id.content_spinner);
CharSequence[] entries = getResources().getTextArray(R.array.shipping_tracking_carrier_names);
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<>(spinner.getContext(), android.R.layout.simple_spinner_item, entries);
adapter.setDropDownViewResource(android.support.v7.appcompat.R.layout.support_simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

As in the code, using same Context with the Spinner is most important thing.

spinner.getContext()
wonsuc
  • 3,498
  • 1
  • 27
  • 30
7

For those who want to change DrowDownIcon color you can use like this

spinner.getBackground().setColorFilter(Color.parseColor("#ffffff"), PorterDuff.Mode.SRC_ATOP);
AMAN SINGH
  • 3,491
  • 6
  • 28
  • 44
  • can you try `@color/light_gray` – AMAN SINGH Feb 24 '17 at 08:57
  • put this line in your style `AppTheme` – AMAN SINGH Feb 24 '17 at 08:58
  • This is the code that worked `Drawable spinnerDrawable = spinner.getBackground().getConstantState().newDrawable(); spinnerDrawable.setColorFilter(getResources().getColor(R.color.md_white_1000), PorterDuff.Mode.SRC_ATOP); if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { spinner.setBackground(spinnerDrawable); }else{ spinner.setBackgroundDrawable(spinnerDrawable); } ` – Edijae Crusar Feb 24 '17 at 12:59
7

To change the color of spinner text :

 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            ((TextView) parent.getChildAt(0)).setTextColor(Color.WHITE);}
Hanisha
  • 849
  • 10
  • 8
5

Simplest: Works for me

public class NameActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
    
   ... 
    
   @Override
   public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
      TextView spinnerText = (TextView) spinner.getChildAt(0);
      spinnerText.setTextColor(Color.RED);

      ...

   }

   @Override
   public void onNothingSelected(AdapterView<?> parent) {}

   ...

}
Philip Herbert
  • 4,599
  • 2
  • 37
  • 40
5

The easiest way to re-use/change the android.R.layout resources is just go the definition. In Android Studio, do Ctrl + B on android.R.layout.simple_spinner_item.xml.

It will take you to the resource file. Just copy the resource file and add a new layout in your Package.R.layout folder and change the textColor of textview as you like and then just call it in adapter like this:

ArrayAdapter<String> adapter = new ArrayAdapter<String(Context,R.layout.spinner_item, spinnerlist);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ZygoteInit
  • 485
  • 6
  • 9
5

If you want a simple method, in order to add items to a dropdown, you usually add them to the strings.xml. Here is an example on how to add colour by using the strings.xml file:

SELECT AGE RANGE

<string-array name="age_array">

   <item> 0-6 </item>                               //No custom colour

  <item><font fgcolor='#FF4CD964'> 12+ </font></item> //With custom colour

</string-array>
Jamie A-Reade
  • 107
  • 1
  • 5
  • cute trick... useful and fast for a one off spinner, color needs to be a raw color value string not from xml – me_ Dec 09 '18 at 23:52
5

Can change the text colour by overriding the getView method as follows:

 new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_dropdown_item, list()){
                @Override
                public View getView(int position, View convertView, @NonNull ViewGroup parent) {
                    View view = super.getView(position, convertView, parent);
                    //change the color to which ever you want                    
                    ((CheckedTextView) view).setTextColor(Color.RED);
                    //change the size to which ever you want                    
                    ((CheckedTextView) view).setTextSize(5);
                    //for using sp values use setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
                    return view;
                }
    }
Madhur
  • 3,303
  • 20
  • 29
5

Just wanted to make a small change on the correct answer at the top. Make a custom XML file for your spinner item inside the layout directory.

spinner_style.xml:

Give your customized color and size to text in this file.

 <?xml version="1.0" encoding="utf-8"?>

     <TextView  
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent" 
            android:layout_height="wrap_content"
            style="?android:attr/spinnerItemStyle"
            android:singleLine="true"
            android:ellipsize="marquee"
            android:textAlignment="inherit"
            android:textSize="15sp"
            android:textColor="#FF0000"         
            android:padding="5dp"
            />

Now use this file to show your spinner items inside your java file:

ArrayAdapter<String> adapter = new ArrayAdapter<>(this,R.layout.spinner_style,list);
adapter.setDropDownViewResource(R.layout.spinner_style);
Akshay Ashok
  • 107
  • 2
  • 9
4

Rather than making a custom layout to get a small size and if you want to use Android's internal small size LAYOUT for the spinner, you should use:

"android.R.layout.simple_gallery_item" instead of "android.R.layout.simple_spinner_item".

ArrayAdapter<CharSequence> madaptor = ArrayAdapter
            .createFromResource(rootView.getContext(),
                                R.array.String_visitor,
                                android.R.layout.simple_gallery_item);

It can reduce the size of spinner's layout. It's just a simple trick.

If you want to reduce the size of a drop down list use this:

madaptor.setDropDownViewResource(android.R.layout.simple_gallery_item);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hemant Shori
  • 2,463
  • 1
  • 22
  • 20
2
<?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:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="#fff"
    android:ellipsize="marquee"
    android:textAlignment="inherit"/>

just use this:

ArrayAdapter<String> adapter_category = new ArrayAdapter<String>(this,
    R.layout.spinner_list_item, categories);
adapter_category
    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
JustinMorris
  • 7,259
  • 3
  • 30
  • 36
santosh
  • 21
  • 1
1

Another variation of Ashraf's solution would be to make sure you're taking into account screen sizes. You'll need to get the spinner in onCreate and set the listener after you set the adapter:

//set your adapter with default or custom spinner cell, then://
serverSpinner.setOnItemSelectedListener(spinnerSelector);
serverSpinner.setSelection(defaultServer);

Then you can start changing the text size of the view that's showing before the spinner is clicked:

private AdapterView.OnItemSelectedListener spinnerSelector = new AdapterView.OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
        boolean largeTablet = getResources().getBoolean(R.bool.isLargeTablet);
        if (tabletSize) { ((TextView)parent.getChildAt(0)).setTextSize(16); }
        else if (largeTablet) { ((TextView)parent.getChildAt(0)).setTextSize(18); }
        else { ((TextView)parent.getChildAt(0)).setTextSize(12); }
    }
    public void onNothingSelected(AdapterView<?> parent) {

    }
};

All you need to do is create layout specific folders like this:

values-sw360dp

values-sw600dp

values-sw800dp

an then add an xml file named "bool.xml" into each of those folders:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <bool name="isTablet">false</bool>
    <bool name="isLargeTablet">false</bool>
</resources>
whyoz
  • 5,168
  • 47
  • 53
1

First we have to create the simple xml resource file for the textview like as below:

<?xml version="1.0" encoding="utf-8"?>

 <TextView  
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:gravity="left"  
    android:textColor="#FF0000"         
    android:padding="5dip"
    />   

and save it. after set on your adapterlist.

Mr.7
  • 2,292
  • 1
  • 20
  • 33
1

Try this method. It is working for me.

@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
    TextView textView = (TextView) view;
    ((TextView) adapterView.getChildAt(0)).setTextColor(Color.RED);
    ((TextView) adapterView.getChildAt(0)).setTextSize(20);
    Toast.makeText(this, textView.getText()+" Selected", Toast.LENGTH_SHORT).show();
}
Scott
  • 1,863
  • 2
  • 24
  • 43
Tushar Lathiya
  • 940
  • 9
  • 26
1

you can have this type of adapter for spinner, totally customized:

 ArrayAdapter<String> genderAdapter = new ArrayAdapter<String>(getActivity(), R.layout.spinner_text, genderList) {

        public View getView(int position, View convertView, ViewGroup parent) {
            View v = super.getView(position, convertView, parent);
            ((TextView) v).setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
            ((TextView) v).setTextColor(Color.parseColor("#676767"));
            ((TextView) v).setTypeface(vrFont);
            return v;
        }

        public View getDropDownView(int position, View convertView, ViewGroup parent) {
            View v = super.getDropDownView(position, convertView, parent);
            ((TextView) v).setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
            ((TextView) v).setTypeface(vrFont);
            ((TextView) v).setTextColor(Color.parseColor("#676767"));

            if (position == 0) {
                ((TextView) v).setTextColor(Color.parseColor("#979797"));
            }

            return v;
        }

while R.layout.spinner_text is:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
style="?android:attr/spinnerItemStyle"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:gravity="center_vertical|left"
android:ellipsize="marquee"
android:maxLines="1"
android:textColor="@color/whiteThree" />
blackHawk
  • 6,047
  • 13
  • 57
  • 100
1
    String typeroutes[] = {"Select","Direct","Non Stop"};
    Spinner typeroute;

    typeroute = view.findViewById(R.id.typeroute);

    final ArrayAdapter<String> arrayAdapter5 = new ArrayAdapter<String>(
                getActivity(), android.R.layout.simple_spinner_item, typeroutes) {
            @Override
            public boolean isEnabled(int position) {
                if (position == 0) {
                    // Disable the first item from Spinner
                    // First item will be use for hint
                    return false;
                } else {
                    return true;
                }
            }

            public View getView(int position, View convertView, ViewGroup parent) {
                View v = super.getView(position, convertView, parent);
                ((TextView) v).setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
                ((TextView) v).setTextColor(Color.parseColor("#ffffff"));
                return v;
            }         ---->in this line very important so add this

            @Override
            public View getDropDownView(int position, View convertView,
                                        ViewGroup parent) {
                View view = super.getDropDownView(position, convertView, parent);
                TextView tv = (TextView) view;
                if (position == 0) {
                    // Set the hint text color gray
                    tv.setTextColor(Color.GRAY);
                } else {
                    tv.setTextColor(Color.BLACK);
                }
                return view;
            }
        };

        arrayAdapter5.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        typeroute.setAdapter(arrayAdapter5);

that's all enjoy your coding...

Mahendren Mahisha
  • 1,072
  • 11
  • 9
0

I have done this as following.I have use getDropDownView() and getView() methods.

Use getDropDownView() for opened Spinner.

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
  View view = convertView;
  if (view == null) {
    LayoutInflater vi = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = vi.inflate(R.layout.context_row_icon, null);
  }
  TextView mTitle = (TextView) view.findViewById(R.id.context_label);
  ImageView flag = (ImageView) view.findViewById(R.id.context_icon);                

  mTitle.setText(values[position].getLabel(activity));

  if (!((LabelItem) getItem(position)).isEnabled()) {
    mTitle.setTextColor(activity.getResources().getColor(R.color.context_item_disabled));
  } else {
    mTitle.setTextColor(activity.getResources().getColor(R.color.context_item));
  }
  return view;
}

And Use getView() for closed Spinner.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
  View view = convertView;
  if (view == null) {
    LayoutInflater vi = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = vi.inflate(R.layout.context_row_icon, null);
  }
  TextView mTitle = (TextView) view.findViewById(R.id.context_label);
  ImageView flag = (ImageView) view.findViewById(R.id.context_icon);

  mTitle.setText(values[position].getLabel(activity));
  mTitle.setTextColor(activity.getResources().getColor(R.color.context_item_disabled));

  return view;
}
DaveyDaveDave
  • 9,821
  • 11
  • 64
  • 77
Bhavinkumar Patel
  • 515
  • 1
  • 12
  • 28
-2

just add new style like this:

<style name="mySpinnerItemStyle" parent="ThemeOverlay.AppCompat.Dark">
    <item name="android:textColor">#000</item>
    <item name="android:color">#000</item>
</style>

and use it:

<Spinner
      android:id="@+id/spinnerCategories"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      style="@style/mySpinnerItemStyle"
      android:layout_margin="5dp" />
Elyas Nategh
  • 540
  • 6
  • 5