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>
================================
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.