neo
neo

Reputation: 126

How to programmatically change color of a Spinner in Android?

I was able to set Color to the first item in the Spinner using the following line of code. But how to give color to the item other than the first item selected by the user.

    List<String> spinnerArray =getContacts();
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(
        this,R.layout.spinner_effect, spinnerArray);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    Spinner contactSpinner = (Spinner) findViewById(R.id.SpinnerchooseContact);
    contactSpinner.setAdapter(adapter);

((TextView) contactSpinner.getChildAt(0)).setTextColor(Color.GRAY);

Upvotes: 1

Views: 524

Answers (1)

Mubashar Javed
Mubashar Javed

Reputation: 749

create a new xml file, named dropdown.xml, define you style in this layout

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="?android:attr/spinnerDropDownItemStyle"
    android:singleLine="true"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:ellipsize="marquee"
    android:textColor="#aa66cc"/>

then set to it your adapter

adapter.setDropDownViewResource(R.dropdown);

Upvotes: 2

Related Questions