Search code examples
c#xamarinspinner

How do I Import a string-array from resources?


I have a problem with importing a string-array!

When I try the code below, The Spinners stay empty and don't load the array string values. This is my code I use:

//Fill CoinSpinner
Spinner CoinSpinner = FindViewById<Spinner>(Resource.Id.CoinSpinner);
CoinSpinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(CoinSpinner_ItemSelected);
ArrayAdapter<String> CoinSpinnerAdapter = new ArrayAdapter<String>(this, Resource.Array.coin_array, Android.Resource.Layout.SimpleSpinnerItem);
//ArrayAdapter CoinSpinnerAdapter = ArrayAdapter.CreateFromResource(this, Resource.Array.coin_array, Android.Resource.Layout.SimpleSpinnerItem);
CoinSpinnerAdapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
CoinSpinner.Adapter = CoinSpinnerAdapter;

I want to import a string-array from Resources/values/String.xml.... What am I doing wrong?

EDIT:
This is the String.xml file:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">TestApp</string>
    <string name="coins_prompt">Coin:</string>
    <string-array name="coin_array">
        <item>EUR</item>
        <item>USD</item>
    </string-array>
</resources>

Solution

  • R.array.coin_array is for pure native Android development where R is the resource class that has all of the resource IDs for all of your resources. In Xamarin.Android R becomes Resource, so try Resource.Array.coin_array instead.

    EDIT:

    Working code sample to populate a spinner with a string-array in resources:

    Layout AXML:

    <?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="match_parent">
       <Button android:id="@+id/myButton" 
               android:layout_width="match_parent" 
               android:layout_height="wrap_content" 
               android:text="@string/hello" />
        <TextView android:id="@+id/textView" 
                  android:layout_width="match_parent" 
                  android:layout_height="wrap_content"
                  android:text="@string/on_off" />
        <Spinner android:id="@+id/spinner"
                 android:layout_width="match_parent" 
                 android:layout_height="wrap_content"
                 android:prompt="@string/on_off"/>
    </LinearLayout>
    

    strings.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
       <string name="hello">Hello World, Click Me!</string>
       <string name="app_name">SpinnerArray</string>
       <string name="on_off">On or Off</string>
       <string-array name="spinnerArray">
          <item>On</item>
          <item>Off</item>
       </string-array>
    </resources>
    

    C# code:

    Spinner spinner = FindViewById<Spinner>(Resource.Id.spinner);
    spinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>
            (spinner_ItemSelected);
    var spinnerAdapter = ArrayAdapter.CreateFromResource
            (this, Resource.Array.spinnerArray, 
             Android.Resource.Layout.SimpleSpinnerItem);
    spinnerAdapter.SetDropDownViewResource 
            (Android.Resource.Layout.SimpleSpinnerDropDownItem);
    spinner.Adapter = spinnerAdapter;
    

    I have verified that the above works to populate a spinner from a string-array in resources.