3

I have some spinners in my app, which is relatively simple, but for some reason the text on the spinners is white when the background of the app is white. The theme I'm trying to use is android:Theme.Light.

Spinner declaration in XML looks like this:

        <Spinner
            android:id="@+id/selection_spinner"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:paddingBottom="4dp"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:paddingTop="4dp"
             />

For the spinner declaration, I'm doing this:

destinationsSpinnerAdapter = new ArrayAdapter<Destination>(getActivity(), android.R.layout.simple_spinner_item);

Finally, I tried to follow the suggestions on this post but without success.

Community
  • 1
  • 1
Rui
  • 5,900
  • 10
  • 38
  • 56

5 Answers5

2

Try changing 'thisActivity()' to the 'this' pointer in your activity. i.e. change

destinationsSpinnerAdapter = new ArrayAdapter<Destination>(getActivity(), android.R.layout.simple_spinner_item);

to

destinationsSpinnerAdapter = new ArrayAdapter<Destination>(this, android.R.layout.simple_spinner_item);

It worked for me.

Varun
  • 1,938
  • 18
  • 19
1

Try this:

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos,
        long id) {
    // TODO Auto-generated method stub
    ((TextView) parent.getChildAt(0)).setTextColor(Color.MAGENTA);
    ((TextView) parent.getChildAt(0)).setTextSize(12);
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

   }
});
Anirudh Sharma
  • 7,968
  • 13
  • 40
  • 42
Anton Kashpor
  • 1,255
  • 2
  • 18
  • 34
0

If you really get stuck, you can create a custom ArrayAdapter. The example below uses white colour but you can change it in SetColourWhite method (and rename the method ;)

This type of adapter should work everywhere. I had hard time to style a spinner in the action bar - this helped.

public class WhiteTextArrayAdapter<T> extends ArrayAdapter<T> {

    public WhiteTextArrayAdapter(Context context, int textViewResourceId, T[] objects) {
        super(context, textViewResourceId, objects);
    }

    public static WhiteTextArrayAdapter<CharSequence> createFromResource(Context context, int textArrayResId, int textViewResId) {
        CharSequence[] strings = context.getResources().getTextArray(textArrayResId);
        return new WhiteTextArrayAdapter<CharSequence>(context, textViewResId, strings);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return SetColourWhite(super.getView(position, convertView, parent));
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        return SetColourWhite(super.getView(position, convertView, parent));
    }

    private View SetColourWhite(View v) {
        if (v instanceof TextView) ((TextView)v).setTextColor(Color.rgb(0xff, 0xff, 0xff)); // white
        return v;
    }

}
Szymon
  • 42,577
  • 16
  • 96
  • 114
0

Change your ArrayAdapter to have Android.R.Simple_List_item_1

That won't fix your styling problem (check your values folders, you should have 3 style.XML documents) but it will solve your immediate problem.

easycheese
  • 5,859
  • 10
  • 53
  • 87
0

You use the following style for the Spinner. Use this in style.xml :

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

if you have already created a style, add the only this item into it:

<item name="android:textColorPrimaryDisableOnly">@color/black</item>

Thanks. :)

Febin Mathew
  • 991
  • 1
  • 11
  • 20