0

I have a spinner widget in my activity. If I run the app on my device (Pie version) the spinner works fine but if I run it on emulator (which has lollipop version- requirements of work)- spinner doesn't show any items at all.

So this is my activity code for the spinner (inside onCreate):

   spinner = (Spinner) findViewById(R.id.spinner_reminder_times);

    // Create an ArrayAdapter using the string array and a default spinner layout
    ArrayAdapter<CharSequence> spinnerAdapter = ArrayAdapter.createFromResource(getApplicationContext(),
            R.array.reminder_times_array, android.R.layout.simple_spinner_dropdown_item);

    // Specify the layout to use when the list of choices appears
    spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    // Apply the spinnerAdapter to the spinner
    spinner.setAdapter(spinnerAdapter);
    spinner.setOnItemSelectedListener(this);

And this is the implemented method:

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    switch (parent.getSelectedItemPosition()) {
        case 0:
            // do stuff
            break;
        case 1:
            // do stuff
            break;
        case 2:
            // do stuff
            break;
    }
}

The spinner xml in activity_layout:

        <Spinner
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/spinner_reminder_times"
            android:theme="@style/ThemeOverlay.AppCompat.Light"
            android:spinnerMode="dropdown">

        </Spinner>

What may be the problem? how comes it works perfectly on my device but on android lollipop emulator doesn't show anything? Thanks!

  • Does is show empty space in the dropdown menu, as if the dropdown items are present but just invisible? – Gavin Wright Mar 21 '19 at 12:40
  • Yes exactly, the dropdown shows empty space like the items are invisible. – Lian Binyamin Mar 21 '19 at 12:48
  • Try changing the theme you set on the Spinner. Also try creating a custom dropdown view XML resource instead of using android.R.layout.simple_spinner_dropdown_item. My guess is the text is the same color as the dropdown background. – Gavin Wright Mar 21 '19 at 12:50
  • So it seems like the text color is the same as the background like you said. Do you know a way to change the text color of the spinner without making any new custom spinner? Thanks for the help! – Lian Binyamin Mar 21 '19 at 14:18
  • See the answer I just added. – Gavin Wright Mar 21 '19 at 14:27

2 Answers2

0
enter code here
android:popupBackground="#59b0b9" 
    // try this to change the popupBackground color

<Spinner
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/spinner_reminder_times"
            android:theme="@style/ThemeOverlay.AppCompat.Light"
            android:popupBackground="#59b0b9" 
            android:spinnerMode="dropdown"/>
vbijoor
  • 95
  • 1
  • 5
  • My Guess i think text is the same color as the dropdown background. Change the popupBackground color – vbijoor Mar 21 '19 at 13:37
  • Seems like it is the problem since popupBackground did work although I still can't see the item before I press the little arrow and if I change the spinner background- the arrow disappears. Do you know a way to change the text color instead of the backgrounds? Thank you for your help! – Lian Binyamin Mar 21 '19 at 14:13
  • This will help https://stackoverflow.com/questions/9476665/how-to-change-spinner-text-size-and-text-color – vbijoor Mar 22 '19 at 17:56
0

Create a new layout file, called my_spinner_dropdown_list_item.xml (probably change textColor once you confirm it works):

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="?android:attr/spinnerDropDownItemStyle"
    android:singleLine="true"
    android:layout_width="match_parent"
    android:textColor="#FF0000"
    android:layout_height="?android:attr/dropdownListPreferredItemHeight"
    android:ellipsize="marquee"/>

And assign this layout as your Spinner's new dropdown resource:

   spinner = (Spinner) findViewById(R.id.spinner_reminder_times);

    // Create an ArrayAdapter using the string array and a default spinner layout
    ArrayAdapter<CharSequence> spinnerAdapter = ArrayAdapter.createFromResource(getApplicationContext(),
            R.array.reminder_times_array, R.layout.my_spinner_dropdown_list_item);

    // Specify the layout to use when the list of choices appears
    spinnerAdapter.setDropDownViewResource(R.layout.my_spinner_dropdown_list_item);

    // Apply the spinnerAdapter to the spinner
    spinner.setAdapter(spinnerAdapter);
    spinner.setOnItemSelectedListener(this);
Gavin Wright
  • 3,124
  • 3
  • 14
  • 35
  • Great! Thanks! It works when tapping the arrow and the drop down opens and looks great. The only thing now is the text of the selected item- still "invisible" since the text color is white. Funny thing is that I have another spinner in my app which works perfect and it was copy-paste from this spinner. – Lian Binyamin Mar 21 '19 at 14:55
  • See my edit. Note how I changed from `android.R.layout.simple_spinner_dropdown_item` to `R.layout.my_spinner_dropdown_list_item` in the `createFromResource()` method. This changes the selected item layout to our new custom layout. – Gavin Wright Mar 21 '19 at 15:08