0

I have an spinner which has all it's options in black color. What I want is to change to white the selected item that's showing in the activity, but NOT in the dropDownView, there it has to keep beeing everything black, just when it is beeing displayed as the selected item, I want it white.

My spinner:

dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);

My xml:

<Spinner
  android:id="@+id/spinner"
  android:layout_width="match_parent"
  android:layout_height="34dp"
  android:layout_marginTop="30dp"
  android:background="@drawable/border_thicker" />

I know that the spinner has this method

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
     public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { 
         // Your code here
     } 

     public void onNothingSelected(AdapterView<?> adapterView) {
         return;
     } 
}); 

But I don't really know how to apply it to what I want. Please help!!

juske
  • 95
  • 1
  • 12

3 Answers3

1
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
       public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { 
     ((TextView)parent.getChildAt(0)).setTextColor(Color.parseColor("#FFFFFF")); 
        }   

      public void onNothingSelected(AdapterView<?> adapterView) {
       return;
       } 
 }); 
Asad Jivani
  • 535
  • 1
  • 5
  • 15
1

Create a custom layout say spinner_item.xml with a simple TextView with white Text color

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@android:color/white">
</TextView>

Then set the spinner layout to the new customized item:

ArrayAdapter<Integer> adapter = new ArrayAdapter<>(this, R.layout.spinner_item, myList);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

No need to change anything in setOnItemSelectedListener

Zain
  • 37,492
  • 7
  • 60
  • 84
0

If you want to change the text color of the selected option in a Spinner you have to add this:

yourSpinner.setSelection(0, true);
View view = yourSpinner.getSelectedView();
((TextView) view).setTextColor(yourColor);

It's important to add the true parameter at the setSelection function, otherwise view will be null.

Don't forget to change the color of the text at onItemSelected too:

yourSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 

    @Override 
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
       // Change the selected item's text color 
       ((TextView) view).setTextColor(yourColor);
    } 

    @Override 
    public void onNothingSelected(AdapterView<?> parent) { 

    } 

});

Explained here: https://stackoverflow.com/a/33910094/19401165