Text color of a closed spinner
Asked Answered
M

6

23

I understand that the closed spinner is actually a View, I think. But I am guessing it has a TextView there somewhere to show the text. How do I get access to that TextView so I can change the textcolor?

EDIT: I need to change this programatically on the fly, not in the XML.

TextView v = (TextView) getView(mySpinner);

v.setTextColor(.....

This doesnt work...

Thank you!

    array_typ=new String[5];
    array_typ[0]="Pressure";
    array_typ[1]="Level";

    array_typ[2]="Overage";
    array_typ[3]="Under";
    array_typ[4]="Taken";


    adaptertyp = new ArrayAdapter<Object>(this,R.layout.simple_spinner_item, array_typ);
    typ.setAdapter(adaptertyp);
Moorefield answered 2/2, 2011 at 22:2 Comment(1)
Couldn't you define the color in its XML file?Osteoblast
C
9

I understand that the closed spinner is actually a View, I think.

Yes. Specifically, it is whatever you told your SpinnerAdapter to create.

But I am guessing it has a TextView there somewhere to show the text.

That would depend on what you told your SpinnerAdapter to create.

How do I get access to that TextView so I can change the textcolor?

Ideally, you don't -- you give it the right color in the first place, via whatever you told your SpinnerAdapter to create. If the color varies, override getView() in your SpinnerAdapter and change the color at that point.

In a pinch, you can try calling getSelectedView() to get the current View being shown by the closed Spinner, but whatever change you make here may be eliminated on the user's next selection, and the alternate color may return later on if the earlier View gets recycled.

Caulicle answered 2/2, 2011 at 22:37 Comment(4)
I added how I am creating the spinner above. I am really new to Android and Java so even though your answer sounds like there is an answer I dont know what to enter. Can you show me a couple line of code that will let me change the text color? thanks!!!Moorefield
@Mark Worsnop: "I added how I am creating the spinner above." -- take a look at your R.layout.simple_spinner_item resource and change the color in there.Caulicle
@Mark Worsnop: Then you need to override getView() and customize the color of your TextView as part of that process. At the end of this comment is a link to a free excerpt from one of my books that discusses this process. It does so in the context of ListView, but the same concept will hold here. commonsware.com/Android/excerpt.pdfCaulicle
Thank you I sure do appreciate you help! thanks for taking the time!'Moorefield
H
34

To modify the text color create a new xml file in your res/layout folder (for example my_spinner_text.xml). Add a text view to the new xml file and modify how you want:

<TextView android:id="@+id/spinnerText" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true"
    android:textColor="#CCCCCC" 
    android:textSize="20dp" 
    xmlns:android="http://schemas.android.com/apk/res/android"/>

Create an ArrayAdapter that uses the new TextView and set it to your spinner:

    Spinner localSpinner = (Spinner)findViewById(R.id.mySpinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.spinner_array,
                R.layout.my_spinner_text);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    localSpinner.setAdapter(adapter);
Hangout answered 2/2, 2011 at 23:27 Comment(4)
for me, I had to add a namespace in the xml file <?xml version="1.0" encoding="utf-8"?> <TextView android:id="@+id/spinnerText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:textColor="#CCCCCC" android:textSize="20dp" xmlns:android="schemas.android.com/apk/res/android">Infective
Your refered website seems to be offline.Chapter
@Infective please fix the link in your comment.Hege
@Hege if you're referring to schemas.android.com/apk/res/android. It's not a URI link, but rather just a schema namespace for the XML element, and it is not meant to point to any website. Adding a namespace was suggested by Eclipse to resolve the issue I had. I updated the answer of Vito to include the namespace. However, If you're referring to designerandroid.com/?p=28, I did not suggest this link, I'm afraid. Maybe Vito can help in this.Infective
S
23

You can change the text colour or can access the textview in setOnItemSelectedListener event,

            spinnerObject.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                 ((TextView)parentView.getChildAt(0)).setTextColor(Color.rgb(249, 249, 249));   

            }
Sclerenchyma answered 31/1, 2012 at 11:29 Comment(2)
this works ! What Budius suggested didn't because I couldn't set the text color.Generalization
This seems inefficient - setting color every time. You can set the color on all items in the spinner at once by extending ArrayAdapter and overriding getDropDownView().Archfiend
C
9

I understand that the closed spinner is actually a View, I think.

Yes. Specifically, it is whatever you told your SpinnerAdapter to create.

But I am guessing it has a TextView there somewhere to show the text.

That would depend on what you told your SpinnerAdapter to create.

How do I get access to that TextView so I can change the textcolor?

Ideally, you don't -- you give it the right color in the first place, via whatever you told your SpinnerAdapter to create. If the color varies, override getView() in your SpinnerAdapter and change the color at that point.

In a pinch, you can try calling getSelectedView() to get the current View being shown by the closed Spinner, but whatever change you make here may be eliminated on the user's next selection, and the alternate color may return later on if the earlier View gets recycled.

Caulicle answered 2/2, 2011 at 22:37 Comment(4)
I added how I am creating the spinner above. I am really new to Android and Java so even though your answer sounds like there is an answer I dont know what to enter. Can you show me a couple line of code that will let me change the text color? thanks!!!Moorefield
@Mark Worsnop: "I added how I am creating the spinner above." -- take a look at your R.layout.simple_spinner_item resource and change the color in there.Caulicle
@Mark Worsnop: Then you need to override getView() and customize the color of your TextView as part of that process. At the end of this comment is a link to a free excerpt from one of my books that discusses this process. It does so in the context of ListView, but the same concept will hold here. commonsware.com/Android/excerpt.pdfCaulicle
Thank you I sure do appreciate you help! thanks for taking the time!'Moorefield
S
1

to do it programatically you have to extend the adapter class, something like:

    ArrayAdapter<CharSequence> adapter = new ArrayAdater(this){
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
           View v = super.getView(position, convertView, parent);           
           // change the color here of your v
           v.  ... etc. etc        
        }
    }
Stephniestepladder answered 31/8, 2011 at 6:4 Comment(1)
I see a setBackgroundColor(), but no setTextColor()Generalization
F
0

For Change Textcolor of closed Spinner I have do in this way it works

@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;
}

@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;
}
Filthy answered 14/12, 2015 at 10:16 Comment(0)
M
-1

Just try this:

public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { ((TextView)parent.getChildAt(0)).setTextColor(getResources().getColor(R.color.colorPrimary));

Muskeg answered 20/9, 2019 at 13:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.