Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

How to modify the textcolor and textsize of the spinner.

shaywshayw Member ✭✭

Hi
After reading https://stackoverflow.com/questions/9476665/how-to-change-spinner-text-size-and-text-color

I tried to apply but get no result.
Maybe i did something wrong.
Under layout folder i added an xml file named Spinneritem
In mainactivity.cs i write this line code
ArrayAdapter yearsadpater = new ArrayAdapter(this, Android.Resource.Layout.Spinneritem, listyears);
but i get an error
Error CS0117 'Resource.Layout' does not contain a definition for 'Spinneritem' app1

Thanks

Tagged:

Best Answer

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    I created a simple demo and coded according to the link you posted, it works properly.

    The main code is as follows:

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:padding="10dip"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <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>
    

    MainActivity.cs

        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);
                spinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(spinner_ItemSelected);
    
                var adapter = ArrayAdapter.CreateFromResource(
                        this, Resource.Array.planets_array, Resource.Layout.spinner_item);
    
                //adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
                spinner.Adapter = adapter;
            }
    
            private void spinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
            {
                //Spinner spinner = (Spinner)sender;
    
            }
     }
    

    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"
        />
    

    strings.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
      <string name="app_name">HelloSpinner</string>
      <string name="planet_prompt">Choose a planet</string>
      <string-array name="planets_array">
        <item>Mercury</item>
        <item>Venus</item>
        <item>Earth</item>
        <item>Mars</item>
        <item>Jupiter</item>
        <item>Saturn</item>
        <item>Uranus</item>
        <item>Neptune</item>
      </string-array>
    </resources>
    

    Note:

    In mainactivity.cs i write this line code
    ArrayAdapter yearsadpater = new ArrayAdapter(this, Android.Resource.Layout.Spinneritem, listyears);
    but i get an error
    Error CS0117 'Resource.Layout' does not contain a definition for 'Spinneritem' app1

    You should create a layout Spinneritem in folder layout, not refer to the system's layout Android.Resource.Layout.Spinneritem.

  • shaywshayw Member ✭✭

    Thank you
    I found my mistake

    I had writen ArrayAdapter yearsadpater = new ArrayAdapter(this, Android.Resource.Layout.Spinneritem, listyears);
    it should be
    ArrayAdapter yearsadpater = new ArrayAdapter(this, Resource.Layout.Spinneritem, listyears);
    without the word Android

Sign In or Register to comment.