13

I have a spinner with several options, each displaying a simple string. Initially, the text is all white. However, if the user selects an option (causing it to become what is displayed on top), I would like that text to become red.

How can I do this?

EDIT : solved

public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
   ((TextView) arg1).setTextColor(Color.parseColor("#E3170D"));
}
SripadRaj
  • 1,687
  • 2
  • 22
  • 33
drew moore
  • 31,565
  • 17
  • 75
  • 112

6 Answers6

22

if the user selects an option (causing it to become what is displayed on top), I would like that text to become red.

So you most likely created OnItemSelectedListener() for your Spinner. So in onItemSelected() method you can simply change text color.

Pseudocode:

public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
   TextView selectedText = (TextView) parent.getChildAt(0);
   if (selectedText != null) {
      selectedText.setTextColor(Color.RED);
   }
}

Hope it helps.

Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
  • this definitely works, I actually figured it out a few mutes ago but hadn't posted it yet. One minor modification though, see my edit if youre curious – drew moore Mar 22 '13 at 09:02
  • 1
    This doesn't work right when you rotate it though. See my question here: http://stackoverflow.com/questions/33747884/android-nullpointerexception-spinner-onitemselected-view-parameter-is-null-a – Rock Lee Nov 17 '15 at 04:45
2

see this answer here and i will copy and paste it

  1. Create custom View layout (e.g. from TextView)
  2. Create Selector and set it as a background of that view
  3. Set Spinner with custom view

Selector: custom_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" 
          android:state_pressed="false" 
          android:drawable="@color/light_grey" />
    <item android:state_focused="true" 
          android:state_pressed="true"
          android:drawable="@color/light_grey" />
    <item android:state_focused="false" 
          android:state_pressed="true"
      android:drawable="@color/light_grey" />
    <item android:state_selected="true" android:drawable="@color/light_grey"/>
    <item android:drawable="@color/white" />
</selector>

Custom View layout: my_simple_item

<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:lines="1"
android:padding="5dip"
android:background="@drawable/custom_selector"/>

Initialise Spinner:

String[] items = new String[] {"One", "Two", "Three"};
Spinner spinner = (Spinner) findViewById(R.id.mySpinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.my_simple_item, items);

Hope this helps

Community
  • 1
  • 1
William Kinaan
  • 28,059
  • 20
  • 85
  • 118
  • I'm a little confused. I create the "selector" (the item on top), in the styles xml, right? Then where do I apply that style to the my_simple_item? – drew moore Mar 22 '13 at 07:19
  • I think this will change background of textview but not textcolor – nidhi_adiga Mar 22 '13 at 07:20
  • on your spinner, just use this line `android:background="@drwable/custom_selector"` – William Kinaan Mar 22 '13 at 07:21
  • also your custom_selector has to be in the drawable folder – William Kinaan Mar 22 '13 at 07:23
  • now please try it your self and if the results was n't as you expected then a lettel change in the code will make it work, now just tell me if you can set a background to the spinner – William Kinaan Mar 22 '13 at 07:26
  • 1
    @WilliamKinaan: I tweaked this code (`android:textcolor` instead of `android:background`), and it worked in changing the color of the text while the spinner is down, but what I wanted was to change the color of the top item when one is selected. Hence the answer posted above.. Thanks for the help though, +1 – drew moore Mar 22 '13 at 09:06
2

some of you that using MaterialBetterSpinner and Binding your Layouts, all the above won't help, try this, hope it helps you:

public class MyAdapter extends ArrayAdapter<String> {      

    public MyAdapter(Context context, int textViewResourceId, List<String> objects) {
        super(context, textViewResourceId, objects);           

    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        return getCustomView(position, convertView, parent);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return getCustomView(position, convertView, parent);
    }

    public View getCustomView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final YourXMLBinding rowBinding = DataBindingUtil.inflate(inflater, R.layout.yourXML, parent,false);
        rowBinding.tv1.setText(mMy.getValues().get(position));
        if(position == mMy.getCurrentIndex()) {
            rowBinding.tv1.setTypeface(Typer.set(getContext()).getFont(Font.ROBOTO_BOLD));//change font
            rowBinding.tv1.setTextColor(ContextCompat.getColor(getContext(), R.color.yourColor));//change color
        }
        return rowBinding.getRoot();
    }
}

yourXML is something like this:

<?xml version="1.0" encoding="utf-8"?>
<layout>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@color/colorBackgroundStart">
    <TextView
        android:id="@+id/tv1"
        android:layout_width="0dp"
        android:layout_weight="0.7"
        android:layout_height="30dp"
        android:textColor="#fff"
        android:textSize="16dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="8dp"/>

</layout>

create a spinner with this adapter and yourXML :

final MyAdapter adapter = new MyAdapter(getContext(), R.layout.yourXML, s.getValues());

final MaterialBetterSpinner spinner = new MaterialBetterSpinner(getContext());
spinner.setAdapter(adapter);
Prasad Jadhav
  • 5,090
  • 16
  • 62
  • 80
batsheva
  • 2,175
  • 1
  • 20
  • 32
1

use this to change the text of selected Text

YOUR_SPINNER.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

     public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
         TextView selectedText=  view.findViewById(R.id.text_view_name_in_Adapter);
         selectedText.setTextColor(getResources().getColor(R.color.YOUR_COLOR));
        }
}
Abhay Pratap
  • 1,886
  • 11
  • 15
0

create like:

 <?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:state_pressed="false" android:drawable="@color/red" />
 <item android:drawable="@android:color/transparent" />
  </selector>

and in your activity xml:

 <Spinner...............
 android:drawSelectorOnTop="true"
  android:background="@drawable/sample"/>  
Avadhani Y
  • 7,566
  • 19
  • 63
  • 90
0

Just add the OnItemSelectedListener to your spinner.

qtySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            ((TextView) view).setTextColor(Color.BLACK); //Change selected text color
        }

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

        }
    });
Jegan
  • 597
  • 3
  • 18