Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text on spinner is white on a white background

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.

like image 322
Robby Smet Avatar asked May 03 '13 08:05

Robby Smet


6 Answers

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);
like image 152
Le Hung Avatar answered Nov 19 '22 09:11

Le Hung


I solved this problem using

getBaseContext()

instead of

getApplicationContext()
like image 38
smukamuka Avatar answered Nov 19 '22 09:11

smukamuka


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"

like image 3
DayDayHappy Avatar answered Nov 19 '22 10:11

DayDayHappy


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

like image 2
Shakeeb Ayaz Avatar answered Nov 19 '22 08:11

Shakeeb Ayaz


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
        }
    }
like image 2
supro_96 Avatar answered Nov 19 '22 09:11

supro_96


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.

like image 1
zkminusck Avatar answered Nov 19 '22 09:11

zkminusck