The solution that worked for me was to use a CheckedTextView
for the the drop down resource view and then change the color of the checked item using a color selector.
spinner_dropdown_item.xml
in the layout
folder:
<?xml version="1.0" encoding="utf-8"?>
<!-- A `CheckedTextView` allows the color of the text to be changed when it is selected (checked). -->
<CheckedTextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinner_item_textview"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:maxLines="1"
android:ellipsize="end"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:textSize="18sp"
android:textColor="@color/spinner_color_selector"
android:background="@color/spinner_background" />
spinner_color_selector
in the color
folder:
<?xml version="1.0" encoding="utf-8"?>
<!-- Highlight the selected (checked) item when the spinner is open. -->
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="true" android:color="@color/white" />
<item android:color="@color/blue_100" />
</selector>
spinner_dropdown_item.xml
must be set as the DropDownResourceView
for the Adapter
. In my case I am using a ResourceArrayAdapter
that pulls information from multiple sources.
// Setup a `MatrixCursor` for the static entries.
String[] matrixCursorColumnNames = {DatabaseHelper._ID, DatabaseHelper.NAME};
MatrixCursor matrixCursor = new MatrixCursor(matrixCursorColumnNames);
matrixCursor.addRow(new Object[]{-2, getString(R.string.first_spinner_item)});
matrixCursor.addRow(new Object[]{-1, getString(R.string.second_spinner_item)});
// Get a `Cursor` with the list of additional items from the database.
Cursor cursor = DatabaseHelper.getCursor();
// Combine `matrixCursor` and `cursor`.
MergeCursor mergeCursor = new MergeCursor(new Cursor[]{matrixCursor, foldersCursor});
// Create a `ResourceCursorAdapter` for the spinner with `this` context. `0` specifies no flags.;
ResourceCursorAdapter resourceCursorAdapter = new ResourceCursorAdapter(this, R.layout.spinner_item, mergeCursor, 0) {
@Override
public void bindView(View view, Context context, Cursor cursor) {
// Get a handle for the spinner item `TextView`.
TextView spinnerItemTextView = (TextView) view.findViewById(R.id.spinner_item_textview);
// Set the `TextView` to display the name.
spinnerItemTextView.setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.NAME)));
}
};
// Set the `ResourceCursorAdapter` drop drown view resource.
resourceCursorAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
// Get a handle for the `Spinner`.
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Set the adapter for the folder `Spinner`.
spinner.setAdapter(resourceCursorAdapter);
Because ResourceCursorAdapter
uses the same bindView
to populate the spinner when it is open and closed, the id of the TextView
in spinner_dropdown_item.xml
and spinner_item.xml
must be the same.
spinner_item.xml
in the layout
folder:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinner_item_textview"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:maxLines="1"
android:ellipsize="end"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:textSize="18sp"
android:textColor="@color/primaryTextColor" />