is it possible to change the background color and text color of the dropdown portion of the spinner control?

Min T 41 Reputation points
2020-12-14T13:31:00.153+00:00

i asked a previous question that worked that changed the text and background color of a spinner here:

https://forums.xamarin.com/discussion/185890/changing-the-text-color-of-all-items-in-a-spinner

but that only changes it for the selected view. i am looking to change the dropdown portion of it too to match the selected view but im having a hard time figuring it out.

what im trying to do is, when a user signs into our app, it will retrieve a color profile for them and apply it to the text and background of widgets in the app. some people on other threads mentioned creating a style xml to format it. it looks to work, but i will need to be able to change that per user. does this mean i can change this dynamically at runtime how i am changing it for the selected view?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,291 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,016 Reputation points Microsoft Vendor
    2020-12-15T09:50:38.25+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    You can change the background color and text color of the dropdown portion of the spinner.

    change the background color: you can use spinner.SetPopupBackgroundDrawable(Resources.GetDrawable(Resource.Color.material_blue_grey_800));

    text color of the dropdown portion of the spinner: you need to create an custom adapter to achieve it.

       [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]  
           public class MainActivity : AppCompatActivity  
           {  
               protected override void OnCreate(Bundle savedInstanceState)  
               {  
                   base.OnCreate(savedInstanceState);  
                   Xamarin.Essentials.Platform.Init(this, savedInstanceState);  
                   // Set our view from the "main" layout resource  
                   SetContentView(Resource.Layout.activity_main);  
         
                   Spinner spinner = FindViewById<Spinner>(Resource.Id.spinner);  
                    
                   //change the background color  
                   spinner.SetPopupBackgroundDrawable(Resources.GetDrawable(Resource.Color.material_blue_grey_800));  
                   
                   spinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(spinner_ItemSelected);  
                   List<string> planetNames = new List<string>();  
                   planetNames.Add("Mercury");  
                   planetNames.Add("Venus");  
                   planetNames.Add("Mars");  
                   planetNames.Add("Earth");  
                   planetNames.Add("Saturn");  
         
                   var adapter = new MyAdapter(this, planetNames);   
                     
                  
                  
                     
                   spinner.Adapter = adapter;  
                   spinner.OnItemSelectedListener = new myItemSelectedListener();  
         
         
               }  
               private void spinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)  
               {  
                   //Spinner spinner = (Spinner)sender;  
                    
               }  
               public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)  
               {  
                   Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);  
         
                   base.OnRequestPermissionsResult(requestCode, permissions, grantResults);  
               }  
           }  
         
           internal class myItemSelectedListener : Java.Lang.Object, AdapterView.IOnItemSelectedListener  
           {  
               public void OnItemSelected(AdapterView parent, View view, int position, long id)  
               {  
                   ///change the select item's color  
                   ((TextView)view.FindViewById<TextView>(Resource.Id.MytextView)).SetTextColor(Android.Graphics.Color.Black); //Change selected text color  
         
               }  
         
               public void OnNothingSelected(AdapterView parent)  
               {  
                    
               }  
           }  
         
           internal class MyAdapter:BaseAdapter<string>  
           {  
               private MainActivity mainActivity;  
               private List<string> planetNames;  
         
               public MyAdapter(MainActivity mainActivity, List<string> planetNames)  
               {  
                   this.mainActivity = mainActivity;  
                   this.planetNames = planetNames;  
               }  
         
               public override string this[int position] => planetNames[position];  
         
               public override int Count => planetNames.Count;  
         
               public override long GetItemId(int position)  
               {  
                   return position;  
               }  
         
               public override View GetView(int position, View convertView, ViewGroup parent)  
               {  
                   var item = planetNames[position];  
                   //change the item's color in popup view  
                   var view = mainActivity.LayoutInflater.Inflate(Resource.Layout.layout1, null);  
                   var textView=view.FindViewById<TextView>(Resource.Id.MytextView);  
                   textView.SetTextColor(Android.Graphics.Color.Green);  
                   textView.Text = item;  
         
                   return view;  
               }  
           }  
       }  
    

    My layout1.xml code.

       <?xml version="1.0" encoding="utf-8"?>  
       <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
           android:orientation="vertical"  
           android:layout_width="match_parent"  
           android:layout_height="wrap_content">  
           <TextView    
               android:layout_width="match_parent"   
               android:layout_height="wrap_content"  
               android:textSize="30sp"  
               android:gravity="left"  
              android:id="@+id/MytextView"  
               
                android:text="test"  
               android:padding="5dip"  
           />  
       </LinearLayout>  
    

    And activity_main.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:orientation="vertical"  
           android:layout_width="match_parent"  
           android:layout_height="match_parent">  
         <TextView  
               android:layout_width="fill_parent"  
               android:layout_height="wrap_content"  
               android:layout_marginTop="10dip"  
               android:text="@string/planet_prompt"  
           />  
           <Spinner  
               android:id="@+id/spinner"  
               android:layout_width="fill_parent"  
               android:layout_height="wrap_content"  
               android:prompt="@string/planet_prompt"  
           />  
       </LinearLayout>  
    

    48190-1.png

    48371-2.png

    ================================
    Update================================

    If you get the color by RGB, you could use ColorDrawable to convert the color like following code.

       spinner.SetPopupBackgroundDrawable(new ColorDrawable(Color.Rgb(255, 0, 0)));  
    

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Min T 41 Reputation points
    2020-12-16T22:06:38.043+00:00

    thank you this looks to work for now, question. can i use this idea to also change the background and text color of the default popups?

    ie for Android.Support.V7.App.AlertDialog.Builder