Search code examples
xamarin.androidandroid-spinnertextcolor

To change the color of the spinner text font


I saw this link: https://learn.microsoft.com/en-us/xamarin/android/user-interface/controls/spinner

I want to change color of the spinner text font:

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:spinnerMode="dropdown"
    android:textColor="#FF0080FF"
    android:popupBackground="#FF553232"
    android:background="#FFD733D7"/>

But in the output, Color of text is always white.


Solution

  • Option 1. add your custom theme to style.xml. This will change the textcolor of items in the dropdown list. enter image description here

    <style name="MySpinnerTheme" parent="android:Theme">
       <item name="android:textColor">@android:color/holo_green_dark</item>
    </style>
    

    layout

    <Spinner
       android:id="@+id/spinner1"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:theme="@style/MySpinnerTheme"
       android:spinnerMode="dropdown"/>
    

    Option 2. If you want to change the selected item only. enter image description here

    
    ...
    
    Spinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(spinner_ItemSelected);
    
    ...
    
    private void spinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e){
       Spinner spinner = (Spinner)sender;
       TextView textView = (TextView)spinner.SelectedView;
       
       textView.SetTextColor(Color.Rgb(0, 235, 0));
    }