13

I am using Spinner for displaying some values. And the strange issue is that

The dropdown is displaying correctly, but when I select any item from dropdown, is not displayed in the box.

And the strange thing is this functionality is working on all Android Operating Systems before 6.0.1(i.e. 6.0.0 and previous). I have also tried AppCompatSpinner and the result is same.

main.xml:

<Spinner
    android:id="@+id/spinner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.3"
    android:entries="@array/values" />

Main.java:

Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setSelection(5); // Not displaying 5th item, Yes! there are more than 5 items.
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        spinner.setSelection(position);
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        spinner.setSelection(5);
    }
});
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
activesince93
  • 1,716
  • 17
  • 37
  • I tested your code under two nexus 5 (6.0.0 and 6.0.1) and I was not able to reproduce your problem. Do your array/values contains special characters ? (I just tested with word like zero, one... six, seven). Are you using a particular phone ? – xiaomi Dec 17 '15 at 09:25
  • Its nexus 5 (OS 6.0.1) – activesince93 Dec 17 '15 at 11:28

1 Answers1

8

It's weird that I am answering my own question. But after doing lots of research finally I found a solution.

Solution:

There is nothing wrong in the code that I previously wrote. It's just issue of inner padding in Android OS 6.0.1

In the release of Android OS 6.0.1 they made some changes in inner padding of a Spinner.

After reading this related question on SO I adjusted my Spinner width and made it visible in all the Android OS versions.

Community
  • 1
  • 1
activesince93
  • 1,716
  • 17
  • 37