Set text color in Android Spinner
Asked Answered
S

7

12

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.

Sulamith answered 31/8, 2016 at 10:29 Comment(0)
C
12

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) {
      }
  });
Cespitose answered 31/8, 2016 at 10:32 Comment(1)
this is awesome it's work for me batter then other Thank you so much @HugoHouyezBinns
S
4

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://mcmap.net/q/73930/-how-to-change-spinner-text-size-and-text-color

Shock answered 24/8, 2017 at 11:42 Comment(0)
R
2

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"/>
Renn answered 31/8, 2016 at 11:2 Comment(0)
H
2

try the following code:-

XML:-

   <Spinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_margin="20dp"
        android:popupBackground="#ffffff"
        android:layout_height="match_parent">

    </Spinner>

create a another xml for the textview

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="dshsgv"
android:padding="5dp"
android:textColor="#000000">

</TextView>

then in your activity:-

public class MainActivity extends AppCompatActivity {
Spinner spinner;
String[] cat = {"Automobile", "Automobile"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    spinner = (Spinner) findViewById(R.id.spinner);
    ArrayAdapter<String> adpter = new ArrayAdapter<String>     (MainActivity.this, R.layout.text, cat);
    spinner.setAdapter(adpter);
 }
 }
Horse answered 31/8, 2016 at 12:32 Comment(0)
P
2

declare ArrayAdapter like this and set it to your spinner:

ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this,
                            R.layout.simple_spinner_dropdown_item, your_strings);
adapter_state.setDropDownViewResource(R.layout.simple_spinner_dropdown_item);
_colorCodeSpinner.setAdapter(adapter_state);

and layout xml file simple_spinner_dropdown_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:gravity="left"
    android:textColor="#AAA"
    android:padding="5dp"
    />

this work for me

Pariah answered 6/5, 2017 at 6:15 Comment(0)
S
0

follow this link

private OnItemSelectedListener OnCatSpinnerCL = new AdapterView.OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {

       ((TextView) parent.getChildAt(0)).setTextColor(Color.BLUE);
       ((TextView) parent.getChildAt(0)).setTextSize(12);

    }

    public void onNothingSelected(AdapterView<?> parent) {

    }
};
Satiable answered 31/8, 2016 at 10:43 Comment(0)
P
0

You can use like this. This will change your icon of DropDown menu.

spinner.getBackground().setColorFilter(Color.parseColor("#FFFFFF"), PorterDuff.Mode.SRC_ATOP);

then make one TextView layout name with spinner_text.xml like this

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinnerText"
style="?android:attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:ellipsize="marquee"
android:textColor="#fff" />

and write this code in your MainActivity.java class like

 List<String> categories = new ArrayList<String>();
    categories.add("Automobile");
    categories.add("Business Services");
    categories.add("Computers");
    categories.add("Education");
    categories.add("Personal");
    categories.add("Travel");

    ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.spinner_text, categories);
    spinner.setAdapter(adapter);
    spinner.getBackground().setColorFilter(ContextCompat.getColor(this,R.color.white), PorterDuff.Mode.SRC_ATOP);
    // attaching data adapter to spinner
    spinner.setAdapter(adapter); 
Piston answered 30/1, 2017 at 10:7 Comment(1)
it change spinner background color not text colorTyro

© 2022 - 2024 — McMap. All rights reserved.