How to change spinner text color
Asked Answered
K

8

15

I saw many topics about how to change spinner's text color,but i couldnt understand how to use

spinner_item.xml

<?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="fill_parent"
    android:gravity="top"
    android:singleLine="true"
    android:textColor="@color/iphone_text" />

What shall i really do in java code:? Any responses will be aprecieted Please answer clearly with as more details as you can

Kinsella answered 19/8, 2013 at 10:54 Comment(0)
P
10

Here You have to set your spinner_item.xml in your array adapter. Add this piece of code in your .java file

Spinner yourSpinner;
ArrayAdapter<String> yourAdapter;
yourSpinner= (Spinner) findViewById(R.id.yourSpinnerID);
yourSpinner.setAdapter(yourAdapter);
yourAdapter= new ArrayAdapter<String>(this, R.layout.spinner_item, value);
//here value is your items in spinner..
Parboil answered 19/8, 2013 at 11:9 Comment(0)
K
21

A complete answer for me would be something like:

public class ee extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ww);
        addListenerOnSpinnerItemSelection();

    }

    public void addListenerOnSpinnerItemSelection() {
        ArrayList<String> array = new ArrayList<String>();
        array.add("item0");
        Spinner spinner1;
        ArrayAdapter<String> mAdapter;
        spinner1 = (Spinner) findViewById(R.id.spinner2);
        mAdapter = new ArrayAdapter<String>(this, R.layout.spinner_item, array);
        spinner1.setAdapter(mAdapter);
    }

}

and in res/layout add new xml file:

(in spinner_item.xml)

<?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="fill_parent"
    android:gravity="top"
    android:singleLine="true"
    android:textColor="#00f0ff" />
Kinsella answered 20/8, 2013 at 9:53 Comment(1)
spinner1 in "spinner1= new ArrayAdapter<String>(this, R.layout.spinner_item, array);" should have been mAdapter.Disarming
P
10

Here You have to set your spinner_item.xml in your array adapter. Add this piece of code in your .java file

Spinner yourSpinner;
ArrayAdapter<String> yourAdapter;
yourSpinner= (Spinner) findViewById(R.id.yourSpinnerID);
yourSpinner.setAdapter(yourAdapter);
yourAdapter= new ArrayAdapter<String>(this, R.layout.spinner_item, value);
//here value is your items in spinner..
Parboil answered 19/8, 2013 at 11:9 Comment(0)
A
4
Spinner spinner = (Spinner) findViewById(R.id.spinner);

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View view,
                int arg2, long arg3) {

            ((TextView) arg0.getChildAt(0)).setTextColor(Color.RED);
        }
        @Override
        public void onNothingSelected(AdapterView<?> arg0) {

        }
}

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                R.layout.list_row, list);
        spinner.setAdapter(adapter);

list_row.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:paddingTop="5dp"
android:textColor="#000000"
android:textSize="20sp" />
Angio answered 10/7, 2014 at 6:7 Comment(0)
I
2

This is the simplest Method which I executed for spinner's text. I know its late but it will help some one.

select_gender.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
  // give the color which ever you want to give to spinner item in this line of code               
            ((TextView)parent.getChildAt(position)).setTextColor(Color.parseColor("#646b99"));
            spinner_selected_gender=gender_list.get(position);
            Toast.makeText(getApplicationContext(),"You selected"+spinner_selected_gender,Toast.LENGTH_SHORT).show();
        }

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

        }
    });
Insuppressible answered 9/5, 2016 at 11:49 Comment(2)
what is textview ? how i use it, ? it give me null objection errorKinder
If you have implemented custom adapter for spinner then textview is the item of that custom layoutInsuppressible
B
0
Spinner spnCategory= (Spinner)findViewById(R.id.my_spinner);
..

ArrayAdapter<String> adptSpnCategory = new ArrayAdapter<String>this,R.layout.custom_spinner_item, alCategoryName);
adptSpnCategory.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnCategory.setAdapter(adptSpnCategory);
spnCategory.setOnItemSelectedListener(new OnItemSelectedListener() 
{
 public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) 
 {
 }
 public void onNothingSelected(AdapterView<?> arg0) 
 {
 }
});
Burschenschaft answered 19/8, 2013 at 11:11 Comment(0)
R
0

I have done this as following: I have used the getDropDownView() and getView() methods.

Use getDropDownView() for opened Spinner.

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
     View view = convertView;
     if (view == null) {
        LayoutInflater vi = (LayoutInflater) activity
                         .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = vi.inflate(R.layout.context_row_icon, null);
     }
     TextView mTitle = (TextView) view.findViewById(R.id.context_label);
     ImageView flag = (ImageView) view.findViewById(R.id.context_icon);               

     mTitle.setText(values[position].getLabel(activity));

     if (!((LabelItem) getItem(position)).isEnabled()) {
          mTitle.setTextColor(activity.getResources().getColor(
               R.color.context_item_disabled));
     } else {
          mTitle.setTextColor(activity.getResources().getColor(
              R.color.context_item));
     }
     return view;
}

And use getView() for closed Spinner.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
     View view = convertView;
     if (view == null) {
         LayoutInflater vi = (LayoutInflater) activity
             .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         view = vi.inflate(R.layout.context_row_icon, null);
     }
     TextView mTitle = (TextView) view.findViewById(R.id.context_label);
     ImageView flag = (ImageView) view.findViewById(R.id.context_icon);

     mTitle.setText(values[position].getLabel(activity));

     mTitle.setTextColor(activity.getResources().getColor(
           R.color.context_item_disabled));

     return view;
}
Reverberation answered 14/12, 2015 at 10:27 Comment(0)
A
0

From the API level 16 and above, you can use following code to change the drop down icon in spinner. just goto onItemSelected in setonItemSelectedListener and change the drawable of textview selected like this.

 spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
      // give the color which ever you want to give to spinner item in this line of code

    //API Level 16 and above only.
            ((TextView)parent.getChildAt(position)).setCompoundDrawablesRelativeWithIntrinsicBounds(null,null,ContextCompat.getDrawable(Activity.this,R.drawable.icon),null);

//Basically itis changing the drawable of textview, we have change the textview left drawable.
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
           }
        });

hope it will help somebody.

Arable answered 10/10, 2016 at 8:3 Comment(0)
A
0

Building on noobProgrammer answer, as singleLine has been deprecated, a quick updated version of the spinner_item.xml for anyone who is looking for a answer for this.

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1">
</TextView>
Anglomania answered 5/6, 2017 at 13:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.