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.
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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With