Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set text color in Android Spinner

I want to change the color of the displayed selected item in my spinner in Android (targeting API level 16 and up). I have tried several solutions posted here on SO, including creating a custom layout for my spinner items and using a ColorStateList as the text color property of the custom layout, but to no avail. The spinner is shown on a semi-transparent background - therefore the custom layout for the items does not work as it adds a color to the spinner. Currently my hack solution is

if (_colorCodeSpinner.getSelectedView() != null) {
    ((TextView) _colorCodeSpinner.getSelectedView()).setTextColor(0xFFFFFFFF);
}

but this only works if the selected view is not null (which it is on orientation change).

I cannot believe that there isn't a simple solution for setting the text color. It seems like something you would often do. The same with changing the color of the arrow, which I currently do by

_colorCodeSpinner.getBackground().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);

Am I missing something? What is the recommended way of changing the colors on a spinner?

Android spinner

As seen in the image, the text color of the displayed selected item in the spinner is black, but I want to change it to be white.

EDIT

To clarify: I'm not looking for some small piece of code that overrides values at runtime (like the two snippets I posted in this question). I'm looking for an actual way to do this properly (like in the XML layout or through themes). To set the text color property once so I don't have to update it every time I e.g. select an item.

like image 877
Daniel Avatar asked Aug 31 '16 10:08

Daniel


Video Answer


3 Answers

Do this :

  spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
      @Override
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
          ((TextView) parent.getChildAt(0)).setTextColor(Color.WHITE); /* if you want your item to be white */
      }

      @Override
      public void onNothingSelected(AdapterView<?> parent) {
      }
  });
like image 188
Hugo Houyez Avatar answered Sep 24 '22 05:09

Hugo Houyez


You can achieve this editing styles.xml layout file. For this answer i use a new project in Android Studio, with minSdkVersion 16 and AppCompatSpinner.

styles.xml layout:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

    <item name="android:spinnerItemStyle">@style/mySpinnerItemSelectedStyle</item>
</style>

<style name="mySpinnerItemSelectedStyle" parent="@android:style/Widget.Holo.TextView.SpinnerItem">
    <item name="android:textColor">@color/spinnerTextColor</item>
</style>

And add this at colors.xml file:

<color name="spinnerTextColor">#ffffff</color>

The solution was taken from the link below. Although it's used for color spinner dropdown items, is mostly the same approach.

https://stackoverflow.com/a/22207394/6514926

like image 20
NetrunnerX Avatar answered Sep 22 '22 05:09

NetrunnerX


This will work for you

public void onItemSelected(AdapterView<?> parent, View arg1, int arg2,
        long arg3) {
    // TODO Auto-generated method stub

    item = (String) parent.getItemAtPosition(arg2);
   ((TextView) parent.getChildAt(0)).setTextColor(0x00000000);



    }

OR
you can use selector for changing color

create one xml named my_selctor.xml

<?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_pressed="true"
           android:color="black" /> <!-- pressed -->
     <item android:state_focused="true"
           android:color="black" /> <!-- focused -->
     <item android:color="white" /> <!-- default -->
 </selector>

and in your text view set it like this way

<TextView ...........
   android:textColor=""@drawable/my_selctor"/>
like image 35
Aditya Vyas-Lakhan Avatar answered Sep 23 '22 05:09

Aditya Vyas-Lakhan