user2702700
user2702700

Reputation: 639

android custom spinner. selected item in blue color

I am using a custom spinner. It is almost similar to default, except that I need to set padding at left of each item ( in the drop down padding in each row before text). I am able to do that. But I also want to show the selected item in a different color when list of values are displayed to user as dropdown.

I have used text view as drop down item.

Can someone please suggest if it can be done. I have tired to achieve this with xml, but I couldn't find any option.

Thanks in advance.

EDIT : Text of selected item in Blue color.

Upvotes: 1

Views: 5837

Answers (3)

Vishal Vaishnav
Vishal Vaishnav

Reputation: 3422

You have to change color of textview at runtime by using spinner's method setOnItemSelectedListener. For Example,

  spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            TextView tv = (TextView) view.findViewById(R.id.tv );
            tv.setTextColor(R.color.green);
        }
        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });

Upvotes: 0

aleb
aleb

Reputation: 2552

You could specify android:dropDownSelector="@color/spinner_selector" for the spinner element and define the spinner_selector color to #800000FF, for example.

See http://developer.android.com/reference/android/widget/Spinner.html#attr_android:dropDownSelector

Upvotes: 3

Vickyexpert
Vickyexpert

Reputation: 3167

You Can Do it by following code.

Just apply style to your textview as background.



// TextStyle is xml file which contain following code.

<item android:state_selected="false"
    android:drawable="@android:color/white" />

<item android:state_selected="true"
    android:drawable="android:drawable="@android:color/blue"" />

Upvotes: 1

Related Questions