274

Is it possible to populate the options of a Spinner right in the layout xml? This page suggests I should use an ArrayAdapter? It seems awkward not being able to do it..

cambraca
  • 27,014
  • 16
  • 68
  • 99

3 Answers3

641

I'm not sure about this, but give it a shot.

In your strings.xml define:

<string-array name="array_name">
<item>Array Item One</item>
<item>Array Item Two</item>
<item>Array Item Three</item>
</string-array>

In your layout:

<Spinner 
        android:id="@+id/spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="true"
        android:entries="@array/array_name"
    />

I've heard this doesn't always work on the designer, but it compiles fine.

pgSystemTester
  • 8,979
  • 2
  • 23
  • 49
blindstuff
  • 18,298
  • 10
  • 47
  • 48
  • 42
    Just wanted to add that if you want separate values per the selected item, you can specify `android:entryValues="@array/array_name_values"`. – Ε Г И І И О Jun 04 '13 at 16:50
  • 4
    One quick note...from my understanding, Spinners don't support `drawSelectorOnTop` since there isn't a separate selector other than the Spinner itself. The solution definitely works though, thanks! – Jabari Jun 11 '13 at 22:25
  • 13
    @ΕГИІИО Spinner does **NOT** support `android:entryValues`. ListPreference does support it. – TalkLittle Nov 15 '15 at 09:32
  • 1
    @TalkLittle was this a recent deprecation? At the time this was written it did work. – blindstuff Nov 15 '15 at 16:33
  • @blindstuff I'm doubtful that it ever worked. For example see this SO answer written in 2009: http://stackoverflow.com/a/1587136/57490 – TalkLittle Nov 15 '15 at 20:50
  • @TalkLittle it has 4 upvotes vs 18 on this comment here. I believe I also tested this personally. I think this is worth verifying. – blindstuff Nov 16 '15 at 13:58
  • 2
    @blindstuff It's easy to verify. Look at the source code of `Spinner` and `AbsSpinner`. On both API 19 and 23, the AbsSpinner constructor makes use of `R.styleable.AbsSpinner_entries` but nowhere do they use `entryValues`. It's only a coincidence that your code was doing what you expected. (Or maybe some manufacturers' ROMs have a custom Spinner implementation that actually handles `android:entryValues`?) – TalkLittle Nov 16 '15 at 17:43
  • 1
    http://stackoverflow.com/questions/1587028/android-configure-spinner-to-use-array take a look at the @Bostone's answer. Apparently this entryValue cannot be useful in spinner cases. Actually I tried to use it. It compiles fine. But what is the use?! how to access the value in the code?! – Fatima Jan 18 '16 at 10:28
  • @blindstuff how can we pass dynamic string array into entries atribute of spinner? – M.ArslanKhan Mar 29 '19 at 13:02
  • So, there is no other way and I have to make mess in two files? – Kamil Sep 30 '22 at 09:53
  • This is not working for Google Material. (of course it is not a "real spinner") – C.F.G Feb 01 '23 at 07:01
38

Define this in your String.xml file and name the array what you want, such as "Weight"

<string-array name="Weight">
<item>Kg</item>
<item>Gram</item>
<item>Tons</item>
</string-array>

and this code in your layout.xml

<Spinner 
        android:id="@+id/fromspin"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:entries="@array/Weight"
 />

In your java file, getActivity is used in fragment; if you write that code in activity, then remove getActivity.

a = (Spinner) findViewById(R.id.fromspin);

 ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this.getActivity(),
                R.array.weight, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        a.setAdapter(adapter);
        a.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                if (a.getSelectedItem().toString().trim().equals("Kilogram")) {
                    if (!b.getText().toString().isEmpty()) {
                        float value1 = Float.parseFloat(b.getText().toString());
                        float kg = value1;
                        c.setText(Float.toString(kg));
                        float gram = value1 * 1000;
                        d.setText(Float.toString(gram));
                        float carat = value1 * 5000;
                        e.setText(Float.toString(carat));
                        float ton = value1 / 908;
                        f.setText(Float.toString(ton));
                    }

                }



            public void onNothingSelected(AdapterView<?> parent) {
                // Another interface callback
            }
        });
        // Inflate the layout for this fragment
        return v;
    }
Gary
  • 13,303
  • 18
  • 49
  • 71
Tech Zone
  • 491
  • 4
  • 9
-2

In regards to the first comment: If you do this you will get an error(in Android Studio). This is in regards to it being out of the Android namespace. If you don't know how to fix this error, check the example out below. Hope this helps!

Example -Before :

<string-array name="roomSize">
    <item>Small(0-4)</item>
    <item>Medium(4-8)</item>
    <item>Large(9+)</item>
</string-array>

Example - After:

<string-array android:name="roomSize">
    <item>Small(0-4)</item>
    <item>Medium(4-8)</item>
    <item>Large(9+)</item>
</string-array>
CPUSCIMAJOR
  • 15
  • 1
  • 5
  • 1
    Doesn't work. The XML file want his `string-array` to have a `name` attribute, and doesn't recognize the `android:name` one. – Dan Chaltiel Aug 29 '17 at 15:24