Java – Unable to manage to change text color on Spinner

Unable to manage to change text color on Spinner… here is a solution to the problem.

Unable to manage to change text color on Spinner

I’ve tried a lot of other answers about SO but none seem to have worked for me. I’m not sure what I’m doing wrong.

Here is my xml:

<Spinner
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/product_category_spinner"
    android:theme="@style/AppartmentBlue" />

This is my Java (it’s a DialogFragment extension class):

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
     Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_product_dialog, container, false);
    Spinner productCategory = (Spinner) v.findViewById(R.id.product_category_spinner);
    ArrayAdapter<String> spinnerAdapter =
            new ArrayAdapter<String>(getActivity().getApplicationContext(),
                    android. R.layout.simple_spinner_dropdown_item,
                    getResources().getStringArray(R.array.category_array));
    spinnerAdapter.setDropDownViewResource(android. R.layout.simple_spinner_dropdown_item);
    productCategory.setAdapter(spinnerAdapter);

 setCancelable(false);
    return v;
}

The fact is that the text is very bright gray on white. By default it is like this, if it appears in the black foreground, I don’t need to touch it. Why would it do this? My theme is an extension from Theme.Holo.Light.DarkActionBar.

I

don’t want to set it at runtime, I’ll have a lot of spinner and I want it to be global across the app and I have the option to add themes later.

Solution

Hope this helps:

My spinner :

<Spinner
    android:id="@+id/spinner_cat"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:paddingBottom="0dp"
    android:paddingTop="0dp"
    android:layout_marginStart="@dimen/left_margin_large"
    android:layout_marginLeft="@dimen/left_margin_large"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"/>

Set up adapters:

private final int _singleItemRes = R.layout.simple_spinner_item;
private final int _dropdownRes = R.layout.simple_spinner_dropdown_item;

ArrayAdapter<String> adapter = new ArrayAdapter<String>(_handler.getContext(), _singleItemRes, getTitles());
adapter.setDropDownViewResource(_dropdownRes);

simple_spinner_item.xml:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:textColor="@color/actionbar_text"
    android:textSize="20sp"
    android:textStyle="normal"/>

simple_spinner_dropdown_item.xml:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="?android:attr/spinnerDropDownItemStyle"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:background="@color/primary_dark"
    android:ellipsize="marquee"
    android:layout_centerVertical="true"
    android:singleLine="true"
    android:paddingRight="@dimen/left_margin_small"
    android:paddingLeft="@dimen/left_margin_small"
    android:textColor="@color/actionbar_text"/>

If I miss anything from the answer, let me know and I’ll add it.

Related Problems and Solutions