Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use string array to populate Spinner

I'm in the rookie leagues when it comes to Android apps and am looking to populate a Spinner with an Array or strings (it's a converter app) below is an extract from my XML file and I'm looking to populate the the Spinner:

......
<string name="TemperatureString">Temperature</string>
<string name="WeightString">Weight</string>
<string name="VolumeString">Volume</string>
<string name="SpeedString">Speed</string>
<string name="LengthString">Length</string>
<string name="AreaString">Area</string>
<string name="EnergyString">Energy</string>
<string name="PresureString">Presure</string>
<string name="MemoryString">Memory</string>

<string-array name="Convert_Type">
    <item>@string/TemperatureString</item>
    <item>@string/WeightString</item>
    <item>@string/VolumeString</item>
    <item>@string/SpeedString</item>
    <item>@string/LengthString</item>
    <item>@string/AreaString</item>
    <item>@string/EnergyString</item>
    <item>@string/PresureString</item>
    <item>@string/MemoryString</item>
</string-array>

From this, I'm trying to populate my spinner (@+id/MainSpinner) - I'm not sure what I'm doing here but for the activity_main.xml I have the following:

    <Spinner
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/MainSpinner"
        tools:listitem="@layout/support_simple_spinner_dropdown_item"/>

I know there's a way of doing this via Java but I'm even worse at Java! For this reason, I'd like to keep this within xml if possible.

Also, If someone can want's to point me towards links to bring on my Java and xml skills that would be great - I've started with Udacity and have found them good but there's a lot to take in for a non-IT graduate (I work in finance but find this kinda thing really interesting!)

Thanks in advance!

like image 486
Jeremy Avatar asked Sep 24 '16 15:09

Jeremy


People also ask

How to add String Array to spinner in android?

add("Horse"); //create an ArrayAdaptar from the String Array ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android. R. layout. simple_spinner_item, list); //set the view for the Drop down list dataAdapter.


1 Answers

Simplest way to bind ListView and Spinner control with String Array is

android:entries = "@array/nameofarray"

<Spinner
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/MainSpinner"
        tools:listitem="@layout/support_simple_spinner_dropdown_item"
        android:entries="@array/Convert_Type"/>

If you want to change the theme of each item of Spinner then put below style into res/values/styles.xml

<style name="ItemTextAppearance">
    <item name="android:textColor">#f00</item>
    <item name="android:textStyle">bold</item>
    <item name="android:typeface">monospace</item>
</style>

and the set

android:theme="@style/ItemTextAppearance"

of spinner.

like image 94
asissuthar Avatar answered Sep 23 '22 17:09

asissuthar