Search code examples
javaandroidandroid-studioandroid-spinner

Change text color to selected item on Android Spinner


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!!


Solution

  •     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;
           } 
     });