Search code examples
androidxamarinspinnerbackground-colorspannablestring

Set Spinner-Item Backgroundcolor with SpannableString


I am using SpannableStrings to set different background colors for the items in my Spinner.

var spinnerList = new List<SpannableString>();
foreach(var mySpinnerItem in spinnerItems)
{
    var tmpSpannable = new SpannableString(mySpinnerItem.Text);
    tmpSpannable.SetSpan(new BackgroundColorSpan(mySpinnerItem.Color), 0, mySpinnerItem.Text.Length, 0);
    spinnerList.Add(tmpSpannable);
}

var spinnerAdapter = new ArrayAdapter<SpannableString>(Context, Android.Resource.Layout.SimpleSpinnerItem, spinnerList);
spinner.Adapter = spinnerAdapter;

The result looks like this:

enter image description here

I would like to have all items in the same width.

Two possible results for which I find no solution:

  • setting the width of all items to the width of the widest item
  • setting the width of all items to the width of the whole Spinner

How can i fix this problem or what is an alternative approach to set programmatically different background colors to the spinner item?


This is how Result 1 should look like:

enter image description here

Please ignore that the font color of the first element is not set in contrast to the background


Solution

  • According to your code above, it just adds a background color to your textview, not the entire item,you could custom your adapter,then set the color to the itemview,here is a sample which pass the spinnerItems to the adapter:

    [Activity(Label = "SpinnerActivity", MainLauncher = true)]
    public class SpinnerActivity : Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
    
            SetContentView(Resource.Layout.spinner);
    
            Spinner sp = FindViewById<Spinner>(Resource.Id.spinner);
            List<MySpinnerItem> spinnerItems = new List<MySpinnerItem>();
            spinnerItems.Add(new MySpinnerItem("Red-",Color.Red));
            spinnerItems.Add(new MySpinnerItem("Yellow--", Color.Yellow));
            spinnerItems.Add(new MySpinnerItem("Green-----", Color.Green));
            spinnerItems.Add(new MySpinnerItem("Blue---", Color.Blue));
            spinnerItems.Add(new MySpinnerItem("Black--------", Color.Black));
            var spinnerAdapter = new MyAdapter(this, Android.Resource.Layout.SimpleSpinnerItem, spinnerItems);
            sp.Adapter = spinnerAdapter;
        }
    }
    
    class MyAdapter : ArrayAdapter<MySpinnerItem>
    {
        public Context context;
        public List<MySpinnerItem> list;
        public int textViewResourceId;
        public MyAdapter(Context context, int textViewResourceId, List<MySpinnerItem> objects) : base(context, textViewResourceId, objects)
        {
            this.context = context;
            list = objects;
            this.textViewResourceId = textViewResourceId;
        }
    
        public override View GetDropDownView(int position, View convertView, ViewGroup parent)
        {
    
            if (convertView == null)
            {
                LayoutInflater inflater = LayoutInflater.From(context);
                convertView = inflater.Inflate(textViewResourceId, parent, false);
            }
            TextView tv = (TextView)convertView.FindViewById(Android.Resource.Id.Text1);
            tv.Text = list[position].Text;
            convertView.SetBackgroundColor(list[position].Color);
            return convertView;
        }
        public override View GetView(int position, View convertView, ViewGroup parent)
        {
    
            if (convertView == null)
            {
                LayoutInflater inflater = LayoutInflater.From(context);
                convertView = inflater.Inflate(textViewResourceId, parent, false);
            }
    
            TextView tv = (TextView)convertView.FindViewById(Android.Resource.Id.Text1);
            tv.Text = list[position].Text;
            convertView.SetBackgroundColor(list[position].Color);
            return convertView;
        }
    }
    
    class MySpinnerItem
    {
        public string Text;
        public Color Color;
    
        public MySpinnerItem(string text, Color color)
        {
            Text = text;
            Color = color;
        }
    }