Android: image only Spinner – Mea Cup O' Jo
Skip to content


Android: image only Spinner

Android uses a Spinner control to handle drop-down selection. For DroidIn app I wanted Spinner that looks like a regular button but when clicked behaves as a regular Spinner (pops up selection list, etc.).

To turn the Spinner into image button is easy – in XML definition simply add android:background="@drawable/select" where “select” refers to res/drawable/select.png
However you will now have a problem with the text of selected item overlaying Spinner’s background image. To solve this problem you will have to do some Java coding as follows:

Spinner spin = (Spinner) d.findViewById(R.id.searchCriteria);
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
  public void onItemSelected(AdapterView parent, View view, int
position, long id) {
    // hide selection text
    ((TextView)view).setText(null);
    // if you want you can change background here
  }
  public void onNothingSelected(AdapterView parent) {}

});

Posted in Android, Java.


5 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Antoine says

    Thanks! That was what I was looking for ! No xml solution?

  2. RaySchenk says

    When you use this technique, there is still a flash of the text overlay as the spinner closes after a selection has been made. Any thoughts on how to avoid that? Did it on a Droid X, reproduced the issue several times.

  3. Sangita says

    I also have the same problem mentioned above ie flashing of text for a small moment. Any suggestion to avoid that?

  4. John says

    I found a nice solution to this:

    Add both your Spinner and another Button (or ImageButton in your case) to your xml file. Set the Spinner's visibility to "Gone". Now in your code, on the onClick() method of your button, call mySpinner.performClick(). Works beatifully!



Some HTML is OK

or, reply to this post via trackback.