Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spinner text color is white?

I use a Spinner inside a layout. I use this code:

...
        <Spinner
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:id="@+id/spin_prod_promo" />
...

Now, when I assign it's adapter I use this code:

...
 ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(mcontext, android.R.layout.simple_spinner_item, mylabels);
 // Drop down layout style
 dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
 Spinner myspinner = (Spinner) V.findViewById(R.id.spin_prod_promo);
 // attaching data adapter to spinner
 myspinner.setAdapter(dataAdapter);
...

My problem is that, the text color of the spinner is white... thus unreadable, and I do not set it to be this way anywhere in the code.

The "funny thing" is that I use the exact same code in a different activity and the Spinner has the right colors. The difference is that in this (BAD) case the spinner is located in a fragment.

  • So if the spinner is in an activity - colors are fine
  • If the spinner is located on a fragment - color is messed up

I tried using a custom layout for the items of the spinner, but all I succeeded was to make the spinner look disabled. Plus there is always an item in the dropdown that has the color of the background (it can only be seen if I click on it, then I can see the content as long as I keep the click on) What is going on here? How can I solve this problem?

Is there something I can do to set the colors of the spinner to be the colors of the theme? All other widgets in the fragment have normal colors...

Thank you

like image 520
user1137313 Avatar asked Nov 28 '22 02:11

user1137313


2 Answers

Context mcontext = getActivity().getApplicationContext(); Is this wrong?

Yes, this is wrong. Only use getApplicationContext() when you know exactly why you are using getApplicationContext(). In particular, never use getApplicationContext() for inflating layouts, as it will screw things up by not using the right theme.

I would delete mcontext entirely, replacing it with getActivity() where needed in your fragment.

like image 161
CommonsWare Avatar answered Dec 11 '22 00:12

CommonsWare


just try this..

String[]array={"A","B","C","D"};
ArrayAdapter<String> adapter=new ArrayAdapter<String>  (**YourClassName.this**,android.R.layout.simple_list_item_checked,array);
listView.setAdapter(adapter);

NOTE YourClassName.this is your current class name.This code is working for me.

like image 29
Nandan Kumar Singh Avatar answered Dec 11 '22 00:12

Nandan Kumar Singh