Fustigador Fustigador - 1 year ago 174
Android Question

Change Spinner background programmatically

I have some Spinners that need to change the background. So, I have this xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/iconodesplegableactivar"
<item android:drawable="@drawable/iconodesplegable"
android:state_enabled="false" android:state_window_focused="false"/>

I have no problems with the Spinners set in layout file, but some spinners are generated dynamically.

How could I use that xml in a spinner generated dinamically? The setBackground() method doesn't allow you to set xml as an argument, and I can't add an ID to the XML.

Thank you!

Answer Source

Taken From this answer, and something which I'm using as well:

If you're creating Spinner dynamically then use this:

// to change background of the popup list

// to change the `Spinner` background

And here is spinner_background.xml

<?xml version="1.0" encoding="utf-8"?>
<solid android:color="#ffffff" />
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download