Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>see this answer <a href="https://stackoverflow.com/questions/7584158/setting-background-color-for-spinner-item-on-selection">here</a> and i will copy and paste it </p> <ol> <li>Create custom View layout (e.g. from TextView)</li> <li>Create Selector and set it as a background of that view</li> <li>Set Spinner with custom view</li> </ol> <p>Selector: custom_selector.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:state_focused="true" android:state_pressed="false" android:drawable="@color/light_grey" /&gt; &lt;item android:state_focused="true" android:state_pressed="true" android:drawable="@color/light_grey" /&gt; &lt;item android:state_focused="false" android:state_pressed="true" android:drawable="@color/light_grey" /&gt; &lt;item android:state_selected="true" android:drawable="@color/light_grey"/&gt; &lt;item android:drawable="@color/white" /&gt; &lt;/selector&gt; </code></pre> <p>Custom View layout: my_simple_item</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:lines="1" android:padding="5dip" android:background="@drawable/custom_selector"/&gt; </code></pre> <p>Initialise Spinner:</p> <pre><code>String[] items = new String[] {"One", "Two", "Three"}; Spinner spinner = (Spinner) findViewById(R.id.mySpinner); ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(this, R.layout.my_simple_item, items); </code></pre> <p>Hope this helps</p>
 

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