40

The text on my spinners is white, and I have no idea why.

enter image description here

This is my xml, nothing special

 <Spinner
     android:id="@+id/spinner_date"
     android:layout_width="0dp"
     android:layout_height="wrap_content"
     android:layout_weight="1" />

And my code

dateSpinner = (Spinner) findViewById(R.id.spinner_date);
selectedDate = calendar.getTime();
List<String> list = new ArrayList<String>();
list.add(formatter.format(selectedDate));
dateAdapter = new ArrayAdapter<String>(mContext,
            android.R.layout.simple_spinner_item, list);
dateSpinner.setAdapter(dateAdapter);

What could be the reason that my text is displayed in white?

EDIT: I've found the reason, I replaced the mContext parameter which was set in my onCreate.

mContext = getApplicationContext();

Now I use d

ateAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, list);

and it works.

Anirudh Sharma
  • 7,968
  • 13
  • 40
  • 42
Robby Smet
  • 4,649
  • 8
  • 61
  • 104
  • 4
    Ugh, I had the same issue, and it was because someone had `ArrayAdapter.createFromResource( this.getActivity().getApplicationContext(), R.array.security_questions, android.R.layout.simple_spinner_item)` instead of `ArrayAdapter.createFromResource( this.getActivity(), R.array.security_questions, android.R.layout.simple_spinner_item)` The getApplicationContext() was definitely screwing us as well, the application must have a different theme than the activity? – Jeff Jun 19 '13 at 19:18
  • Thank you a lot! After a long search I found your post and could solve my problem. – Antiohia Jun 30 '14 at 15:43
  • @Jeff Thank you so much for the solution, it saved much of my time! – hetsgandhi Jan 27 '20 at 05:42

8 Answers8

107

I have same problem and have found the answer. You dont use application context, instead, just use getActivity() (if you are in fragment) or this (if you are in activity), it will work

 dateAdapter = new ArrayAdapter<String>(**this**,
        android.R.layout.simple_spinner_item, list);
Amit
  • 15,217
  • 8
  • 46
  • 68
Le Hung
  • 1,086
  • 1
  • 8
  • 3
  • I'm using a `SimpleCursorAdapter` but this didn't fix my issue. Any idea? – AdamMc331 Dec 03 '14 at 20:13
  • @Le Hung you are right when we use this instead of getApplication context it looks really nice. But when we use getApplicationContext things are looking vi-eared. Thanks man. – Ahesanali Suthar Dec 09 '16 at 09:44
16

I solved this problem using

getBaseContext()

instead of

getApplicationContext()
smukamuka
  • 1,442
  • 1
  • 15
  • 23
  • yes I used ArrayAdapter inside AsyncTask extended class..and having same problem...getBaseContext() working for me.. – RAVI VAGHELA Jul 25 '16 at 10:57
3

i change it from

new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, some_list);

to new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);

it's fixed, although i don't want to use "this"

Anirudh Sharma
  • 7,968
  • 13
  • 40
  • 42
DayDayHappy
  • 1,679
  • 1
  • 15
  • 26
3

I've changed the text color of that spinner's textview without creating a new layout. I know it's been a long time but it's what worked for me, just thought of sharing it. Best part is you can use it on any default adapter.

Here's the code : (this for Activity & requireActivity for Fragments)

1) Java

arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,groups){
        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            View view = super.getView(position, convertView, parent);
            TextView listItem = view.findViewById(android.R.id.text1);
            listItem.setTextColor(Color.WHITE);
            listItem.setTextSize(20);
            listItem.setElevation(18);
            return view;
        }
    };

2) Kotlin

arrayAdapter = object : ArrayAdapter<String>(requireActivity(), android.R.layout.simple_spinner_item, spinnerCategoriesList) {
        override fun getView(position: Int, @Nullable convertView: View?, parent: ViewGroup): View {
            val view = super.getView(position, convertView, parent)
            val listItem = view.findViewById<TextView>(android.R.id.text1)
            listItem.setTextColor(Color.BLACK)
            listItem.textSize = 16f
            return view
        }
    }
supro_96
  • 570
  • 6
  • 14
2

I also had same problem it was because of my application theme. Which I solved by replacing the

android.R.layout.simple_spinner_item

with

android.R.layout.simple_list_item_1

in my ArrayAdapter. I hope this may solve your problem

Shashanth
  • 4,995
  • 7
  • 41
  • 51
Shakeeb Ayaz
  • 6,200
  • 6
  • 45
  • 64
1

Maybe you have a white android:textColor="@android:color/white" attribute in your simple_spinner_item.xml in the layout folder of your project.

Better use a custom spinner item layout with a good android:textColor="@android:color/COLOR_YOU_WANT_TO_USE" attribute.

zkminusck
  • 1,230
  • 1
  • 12
  • 23
  • 1
    the simple_spinner_item is coming from android, not from the project...I'm not sure that's editable? – Jeff Jun 19 '13 at 19:24
0

I assume you have created your own TextView for your Spinner, e.g.

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="14sp"
    android:textColor="@color/white"
    android:padding="5dip"
    />

and glued it to your Adapter via a call like that

String[] spinnerItems = getResources().getStringArray(R.array.my_array);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.spinner_text, spinnerItems);

This should result in having the selection-text of your spinner including its items within the dropdown view painted in white. Now the background of your dropdown view will be influenced by your app theme, which in most cases, will lead your white text to be rendered on a white background. To avoid this android allows you to set a resource for your spinner dropdown view. You can set your own view or simply use the default drop-down view, which will overwrite your custom textview within the dropdown menu through the call

 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

The complete code could look like this

this.spinner = findViewById(R.id.spinnerView);
String[] spinnerItems = getResources().getStringArray(R.array.my_array);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.spinner_text, spinnerItems);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
this.spinner.setAdapter(adapter);
Martin Nowosad
  • 791
  • 8
  • 15
-1

You can easily style the Spinner. Use this in style.xml :

 <style name="SpinnerThemeLight" >
    <item name="android:colorBackground">@color/black</item>
    <item name="android:textColorPrimary">@color/black</item>
    <item name="android:textColorSecondary">@color/black</item>
    <item name="android:textColorTertiary">@color/black</item>
    <item name="android:textColorPrimaryDisableOnly">@color/black</item>
</style>

In the above xml file, I have simply given black color for time sake. Just play with it and sort out your preferred color.

Define the Spinner in the activity.xml as follows :

 <android.support.v7.widget.AppCompatSpinner
   android:id="@+id/spinner"
   android:layout_width="wrap_content"
   android:layout_height="match_parent"
   android:entries="@array/countrys"
   android:spinnerMode="dropdown"
   android:theme="@style/SpinnerThemeLight"/>
Febin Mathew
  • 991
  • 1
  • 11
  • 20