3iL
3iL

Reputation: 2176

How to change background color of spinner items

I'm using this library for spinner. I want to change the color of already selected items from the list in the spinner. How can I do it? This is how I'm populating the data onclick of the spinner:

spinner1.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    Cursor crs = database.rawQuery("SELECT * FROM "+ ItemsTable.TABLE_ZONE +" WHERE "+ ItemsTable.COLUMN_ZONE_ID +"<>"+ zone_id1
                            +" AND "+ ItemsTable.COLUMN_ZONE_ID +"<>"+ zone_id2 +" AND "+ ItemsTable.COLUMN_ZONE_ID +"<>"+ zone_id3 +"", null);
                    Integer[crs.getCount()];
                    List<Zone> listOfZones = new ArrayList<Zone>();

                    while(crs.moveToNext())
                    {
                        String title = crs.getString(crs.getColumnIndex("title"));
                        Integer title_id = crs.getInt(crs.getColumnIndex("id"));

                        listOfZones.add(new Zone(title_id, title));

                    }
                    crs.close();

                    ArrayAdapter<Zone> zoneadapter = new ArrayAdapter<Zone>(getActivity(),
                            android.R.layout.simple_dropdown_item_1line, listOfZones);
                    spinner1.setAdapter(zoneadapter);
                }
                return false;
            }
        });

In the code above I'm removing the items from the list which are already selected but I want to change the background color of the items already selected.

Upvotes: 1

Views: 349

Answers (1)

Sagar Kacha
Sagar Kacha

Reputation: 8464

You can give background color by creating xml file for spinner layout. Follow below steps.

1) You need to create one xml file under layout folder.

2) Create layout which includes one TextView which show item names.

3) Give background color to main rootview layout. For example android:background="@color/anycolor".

And bind this layout in spinner adapter.

Here is custom adapter:

public class CustomAdapter extends BaseAdapter {
Context context;
List<Zone> listOfZones;
LayoutInflater inflter;

public CustomAdapter(Context applicationContext,  List<Zone> listOfZones) {
    this.context = applicationContext;
    this.listOfZones = listOfZones;
    inflter = (LayoutInflater.from(applicationContext));
}

@Override
public int getCount() {
    return flags.length;
}

@Override
public Object getItem(int i) {
    return null;
}

@Override
public long getItemId(int i) {
    return 0;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    view = inflter.inflate(R.layout.your_layout_name, null);
    TextView names = (TextView) view.findViewById(R.id.textView);
    names.setText(listOfZones.get(i).yourObjectName);
    return view;
}}

And bind this to spinner like this:

 CustomAdapter customAdapter=new CustomAdapter(getApplicationContext(),listOfZones);
 your_spinner.setAdapter(customAdapter);

Upvotes: 1

Related Questions