Share via


changing the text color of all items in a spinner

Question

Friday, October 30, 2020 8:35 PM

i managed to get some code that changes the color of the text for a spinner's selected item

void loadColors() { ... Color color_val = Color.ParseColor("#" + hexval); View v = _spinUPLanguage .SelectedView; ((TextView)v).SetTextColor(color_val); {

however, when you choose another item in the spinner, the color doesnt propagate to the other items in the spinner. was wondering if theres a way to do all items at once or if i have to do something like use the ItemSelected event handler to change the color manually every time they choose

``` lstSelectLabel.ItemSelected += new EventHandler(lstSelectLabelItemSelected);

private void lstSelectLabelQty_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e) { loadColors(); }

```

All replies (6)

Monday, November 2, 2020 6:42 AM

We can make a custom XML file for the spinner item.

1.Give your customized color to text in this file.

spinner_item.xml

<?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="30sp" android:gravity="left"
android:textColor="#FF0000"
android:padding="5dip" />

2.use this file to show your spinner items like:

        Spinner spinner = FindViewById<Spinner>(Resource.Id.spinner);
        spinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(spinner_ItemSelected);

        var adapter = ArrayAdapter.CreateFromResource(
                this, Resource.Array.planets_array, Resource.Layout.spinner_item);

        spinner.Adapter = adapter;

For more information, you can check thread:https://forums.xamarin.com/discussion/185739/how-to-modify-the-textcolor-and-textsize-of-the-spinner


Wednesday, November 4, 2020 8:15 AM

Hi @min_t , have you resolved your problem ?


Wednesday, November 4, 2020 12:45 PM

@jezh not quite. i wont be using an external xml file for the spinner layout. i will need to change them programatically at runtime.

i want to be able to allow the user to change the colors as they choose while running the app


Thursday, November 5, 2020 9:24 AM

i managed to get some code that changes the color of the text for a spinner's selected item

You can try the following code:

Spinner spinner = FindViewById<Spinner>(Resource.Id.spinner); spinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(spinner_ItemSelected);

function 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));
    }

Thursday, November 5, 2020 9:44 PM

@jezh yea i am currently doing that now, just was curious is if there was a way to do it once with the first render rather than having to change the color every time they pick something on the spinner via ItemSelected


Wednesday, December 9, 2020 4:39 PM

@jezh said:

i managed to get some code that changes the color of the text for a spinner's selected item

You can try the following code:

Spinner spinner = FindViewById<Spinner>(Resource.Id.spinner); spinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(spinner_ItemSelected);

function 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));
    }

is there a way to change the color of the spinner's dropdown also without using a sepereate style xml? ie through the code similar to this?