Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the TEXT (not background) color of a spinner when an item is selected

Tags:

android

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"));
}
like image 432
drew moore Avatar asked Mar 22 '13 07:03

drew moore


4 Answers

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.

like image 150
Simon Dorociak Avatar answered Nov 19 '22 04:11

Simon Dorociak


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

like image 2
William Kinaan Avatar answered Nov 19 '22 05:11

William Kinaan


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);
like image 2
batsheva Avatar answered Nov 19 '22 03:11

batsheva


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));
        }
}
like image 1
Abhay Pratap Avatar answered Nov 19 '22 05:11

Abhay Pratap