23

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

dark_illusion_909099
  • 1,067
  • 2
  • 21
  • 41

10 Answers10

44

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 ;)

Jiyeh
  • 5,187
  • 2
  • 30
  • 31
30

Try this one:

<Spinner
     android:id="@+id/spinnPhoneTypes"
     android:layout_width="0dp"
     style="@android:style/Widget.Spinner.DropDown"
     android:layout_height="@dimen/thirtyFive"
     android:layout_marginLeft="10dp"
     android:layout_weight="1"
     android:background="@drawable/shape_drop_down_normal"
     android:gravity="center_vertical" />

shape_drop_down_normal.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape>
            <solid android:color="@android:color/transparent" />

            <stroke
                android:width="1dp"
                android:color="#6f94c7" />

            <padding
                android:bottom="10dp"
                android:left="2dp"
                android:right="10dp"
                android:top="10dp" />
        </shape>
    </item>
    <item>
        <bitmap
            android:gravity="end"
            android:src="@drawable/drop_arrow" />
    </item>
</layer-list>
RyuZz
  • 581
  • 8
  • 30
23

Check if you are giving a background for your Spinner and if you do remove it and it will solve the issue

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
MFQ
  • 839
  • 8
  • 14
  • What if I have to give a background color and keep the arrow? For e this answer work https://stackoverflow.com/a/56263841/2768515 – Chetan Gaikwad Jun 04 '22 at 15:06
11

Just add theme in Spinner tag and you are good to go.

android:theme="@style/ThemeOverlay.AppCompat.Light"
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Rahul Dange
  • 2,371
  • 20
  • 15
3

To avoid using a drawable, wrap the spinner in a view and set a background color to the wrapper view. For example,

<android.support.v7.widget.CardView
        android:id="@+id/cardView_spinner"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_margin="32dp"
        android:background="@color/white">

        <Spinner
            android:id="@+id/spinner"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </android.support.v7.widget.CardView>
Eugenio Lopez
  • 180
  • 2
  • 8
2
    <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:background="@drawable/edit_bg">
            <androidx.appcompat.widget.AppCompatSpinner
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="@font/railayregular"
                app:adapter="@{adapter}"
                android:onItemSelected="@{click::click}"
                android:textColor="@color/black"
                android:spinnerMode="dropdown"
                android:theme="@style/Theme.AppCompat.Light"
                android:textColorHint="@color/black" />
        </RelativeLayout>
nithin joseph
  • 461
  • 3
  • 7
0

Simplest method is Set background image (with arrow) instead of color.

 spinner.setBackgroundResource(R.drawable.spinner_img);

hope it helps.

manDroid
  • 1,125
  • 3
  • 13
  • 25
  • 2
    I tried this way but then the picture goes all over the spinner. I want the arrow to go at the side of the spinner. So at the minute it look like this ; [https://www.dropbox.com/s/oe59pxiehktwdet/Screenshot_2015-03-12-15-10-42-1.png?dl=0] – dark_illusion_909099 Mar 12 '15 at 15:12
0

remove background color for your spinner like may be you have set white background which will not display drop down icon

Prakash Reddy
  • 944
  • 10
  • 20
0

This is also working for me If you are using Material Components as Theme,

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

And in your code:

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);
DeePanShu
  • 1,236
  • 10
  • 23
0

If you are adding background color or resource. Change it

foreground

instead of

background

Example (Wrong):

<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:background="@drawable/shape_yellow_bordered"
android:fontFamily="@font/palanquin_regular"
android:inputType="text" />

Example (True):

<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:foreground="@drawable/shape_yellow_bordered"
android:fontFamily="@font/palanquin_regular"
android:inputType="text" />