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) {}
});
Thanks! That was what I was looking for ! No xml solution?
None that I was able to find
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.
I also have the same problem mentioned above ie flashing of text for a small moment. Any suggestion to avoid that?
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!