I have Spinner
like this :
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner1"
android:background="@drawable/spinner_bg"
android:popupBackground="@drawable/spinner_bg"/>
this is spinner_bg.xml :
<item>
<layer-list>
<item>
<shape>
<gradient
android:startColor="#ffffff"
android:centerColor="#111111"
android:endColor="#000000"
android:angle="-90" />
<stroke
android:width="2dp"
android:color="#ffffff" />
<corners
android:radius="2dp" />
<padding
android:left="10dp"
android:right="10dp"/>
</shape>
</item>
<item >
<bitmap
android:gravity="right"
android:src="@android:drawable/arrow_down_float" />
</item>
</layer-list>
</item>
this is my code to Custom spinner :
ArrayAdapter<ClassId> adapter = new ArrayAdapter<ClassId>(getActivity(),
R.layout.list_id, idList);
adapter.setDropDownViewResource(R.layout.list_id_select);
this is layout of list_id.xml :
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:singleLine="true"
android:ellipsize="end"
android:textColor="#ff0004"
android:textSize="14sp"
android:paddingTop="10dp"
android:paddingBottom="10dp"/>
this is layout of list_id_select.xml :
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:singleLine="true"
android:ellipsize="end"
android:textColor="#0004ff"
android:textSize="14sp"
android:checked="true"
android:checkMark="@drawable/custom_checkbox"
android:paddingTop="10dp"
android:paddingBottom="10dp"/>
and this is custom_checkbox.xml :
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="@android:drawable/checkbox_on_background" />
<item android:state_pressed="true"
android:drawable="@android:drawable/checkbox_on_background" />
<item android:drawable="@android:drawable/checkbox_off_background" />
this is my result when dropdown of spinner show :
_________________________________________________
__________________________checkbox_______________
____text_________________________________________
that mean text and checkbox not in line (checkbox higher than text).
how to fix it?
For that you have to Create
Custom Adapter
and setTextView
andCheckBox
inside that below way.Define
Spinner
in xmlCreate
spinner_item.xml
file inlayout
folder.Now create
StateVO.java
class that can contain theTextView
andCheckBox
value.Now in your
Activity
inititlize theSpinner
and setCustomAdapter
below way.And Finally Create
CustomAdapter
class like below way.MyAdapter.java
Output :
This post is a great resource. For anyone interested, I made a generic adapter based on the other answer that has worked well for multiple data types. Also lets you click the text to toggle the checkbox.
And the layout
checkable_spinner_item.xml
You would use it by creating a list of items to show, a Set, which will contain the selected items, and the header row text. For example:
An example in use:
EDIT:
MyObject
can be any class or enum, whatever you want to associate with the spinner items. If you are copying this example directly it would have to implement aString getName()
method. Here's a simple example: