androidxmllayoutfragmentspinner

Android Spinner Dropdown arrow not displaying


my background for my fragment is white, and the arrow for the spinner does not display unless I click on it.

This is the snippet from my Java file:

    spinner = (Spinner)v.findViewById(R.id.spinner);
    ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.accounts,R.layout.spinner_item);
    adapter.setDropDownViewResource(R.layout.spinner_dropdown_items);
    spinner.setAdapter(adapter);
    spinner.setPrompt("Select an account");

This is my XMLfor the spinner_item

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:textAlignment="inherit"
android:textSize="16dp"
android:background="#FFFFFFFF"
android:textColor="#ff252525"/>

And this is my layout for my spinner_dropdown_items.

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:textAlignment="inherit"
android:textSize="16dp"
android:background="#FFFFFFFF"
android:textColor="#ff252525"/>

This is how my spinner looks with a white background to my fragment: White Colour Background - Spinner And this is how it looks when I change my background colour to purple: Purple background - Spinner


Solution

  • This works for me, much simpler as well:

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

    And in your class file:

    spinner = (Spinner) view.findViewById(R.id.spinner);
    ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.spinner_data, android.R.layout.simple_spinner_dropdown_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    

    Hope this helps ;)