N Sharma
N Sharma

Reputation: 34517

How to set the item text color to white of Spinner in Android

I am working on demo application in which I am using Spinner and have a black like background color so Spinner selected item color is also by default black so it's in not visible.

I want through xml or update style to change this color to white so that it will be visible over black.

<Spinner
    android:id="@+id/countrySpinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="15dp"
    android:hint="@string/country" />

enter image description here

Any way to do this ? Thanks in advance.

Upvotes: 0

Views: 2517

Answers (2)

NSouth
NSouth

Reputation: 5276

Try handling this in your OnItemSelectedListener. I think the following should work. It takes the view of the selected item, finds the TextView within it (Spinner views have a TextView child with an id of text1), and sets its color.

mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> arg0, View view,
                int position, long id) {

            TextView tmpView = (TextView) mySpinner.getSelectedView().findViewById(android.R.id.text1);
            tmpView.setTextColor(Color.WHITE);
        }

        public void onNothingSelected(AdapterView<?> arg0) {
            // do stuff

        }
    });

Upvotes: 5

CSmith
CSmith

Reputation: 13458

Set a style in your res/values/styles.xml:

<style name="mySpinner" parent="@android:style/Widget.Holo.Dark.ProgressBar" />

and reference this style on your Spinner:

<Spinner style="@style/mySpinner" ... />

The parent style, Widget.Holo.Dark.ProgressBar should get you a spinner appropriately styled for a dark background.

See https://developer.android.com/guide/topics/resources/style-resource.html for info on Styles. You can sniff around the SDK res/ folders to learn about built-in styles.

Upvotes: 0

Related Questions