Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In my case, although size '2' is displayed in the spinner, nothing happens till some selection is done!</p> <p>I have an xml file (data_sizes.xml) which lists all the spinner values.</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;resources&gt; &lt;string-array name="chunks"&gt; &lt;item&gt;2&lt;/item&gt; &lt;item&gt;4&lt;/item&gt; &lt;item&gt;8&lt;/item&gt; &lt;item&gt;16&lt;/item&gt; &lt;item&gt;32&lt;/item&gt; &lt;/string-array&gt; &lt;/resources&gt; </code></pre> <p>In main.xml file: Spinner element</p> <pre><code>&lt;Spinner android:id="@+id/spinnerSize" android:layout_marginLeft="50px" android:layout_width="fill_parent" android:drawSelectorOnTop="true" android:layout_marginTop="5dip" android:prompt="@string/SelectSize" android:layout_marginRight="30px" android:layout_height="35px" /&gt; </code></pre> <p>Then in my java code, I added:</p> <p>In my activity: Declaration</p> <pre><code>Spinner spinnerSize; ArrayAdapter adapter; </code></pre> <p>In a public void function - initControls(): Definition</p> <pre><code>spinnerSize = (Spinner)findViewById(R.id.spinnerSize); adapter = ArrayAdapter.createFromResource(this, R.array.chunks, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinnerSize.setAdapter(adapter); spinnerSize.setOnItemSelectedListener(new MyOnItemSelectedListener()); </code></pre> <p>My spinner listener:</p> <p>/* Spinner Listener */</p> <pre><code>class MyOnItemSelectedListener implements OnItemSelectedListener { public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int pos, long id) { chunkSize = new Integer(parent.getItemAtPosition(pos).toString()).intValue(); } public void onNothingSelected(AdapterView&lt;?&gt; parent) { // Dummy } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload