A.Langstaff
A.Langstaff

Reputation: 23

Android - Setting the text colour of specific items in a spinner

I have a spinner which is populated with a list of puzzle names. Some of these puzzle names have been downloaded and some haven't. I want to change the text colour of these items to green if downloaded and red if not. At the moment all names are black.

Does anyone know how to change specific items.

Thanks

Upvotes: 0

Views: 301

Answers (2)

Jackyto
Jackyto

Reputation: 1599

See this question with the following code :

  public class CustomizedSpinnerAdapter extends ArrayAdapter<String> {

  private Activity context;
  String[] data = null;

  public CustomizedSpinnerAdapter(Activity context, int resource, String[] _data) {
      super(context, resource, data2);
      this.context = context;
      this.data = _data;
  }
  ...

  @Override
  public View getDropDownView(int position, View convertView, ViewGroup parent) {   
   View row = convertView;

   if(row == null) {
       //inflate your customlayout for the textview
       LayoutInflater inflater = context.getLayoutInflater();
       row = inflater.inflate(R.layout.spinner_layout, parent, false);
   }
   //put the data in it
   String item = data[position];
   if(item != null) {   
      TextView text1 = (TextView) row.findViewById(R.id.rowText);
      text1.setTextColor(Color.WHITE);
      text1.setText(item);
   }

   return row;
  }
 }

And within the getDropDownView, you can manually set your text color depending on your data

Upvotes: 0

Techierj
Techierj

Reputation: 141

Use custom adapter for your spinner and add selector to TextView's textColor and select it in adapter based on your downloaded value..

Upvotes: 1

Related Questions