Question

I have a selection for some items using a spinner widget. At the moment the spinner will load a simple Textview and show the name of the item.
It is possible to define an own view to show inside the spinner row? I would suspect it being similar to a custom List row.

I simply want to show an individual icon left from the spinner text for each item in the list.

Was it helpful?

Solution

Yes, you can do that - as you may know, Spinner is a type of AdapterView, which means that you can adapt your own data to show in such View.

The approach is pretty similar to the one with ListView. Excerpt from ApiDemos:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, INTERPOLATORS);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(adapter);

You can define your own layout and pass it to the SpinnerAdapter subclass that you use.

OTHER TIPS

Yes, it is very easy.

The main thing is that, define TextView as the root element in the layout file.
Do as below:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, INTERPOLATORS);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(adapter);


simple_spinner_dropdown_item.xml file :

<?xml version="1.0" encoding="utf-8"?>
<TextView>
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="50px"
    android:textColor="#000000"
    android:textSize="20px"
    android:gravity="center_vertical" >
</TextView>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top