1

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.

3iL
  • 2,146
  • 2
  • 23
  • 47
  • Crate custom Layout for that [link](https://stackoverflow.com/questions/17407626/custom-layout-for-spinner-itema) – ND1010_ Jan 01 '18 at 06:04
  • Follow this tutorial : https://android--code.blogspot.in/2015/08/android-spinner-item-background-color.html – Mahesh Vayak Jan 01 '18 at 06:09
  • 2
    Possible duplicate of [Changing background color for items of Spinner](https://stackoverflow.com/questions/4294111/changing-background-color-for-items-of-spinner) – ADM Jan 01 '18 at 07:05
  • @ADM, The question you posted is asking how to change the background color of every item in the list. That's pretty easy to do. I wouldn't have posted a question if I had the same issue. My problem is I want to change the background of already selected items that can't be done just using xml. – 3iL Jan 01 '18 at 07:10
  • @3iL if you want to change pre-selected item then i think you need to use Custom adapter . Just override the `ArrayAdapter` and validate in `getView()`. – ADM Jan 01 '18 at 11:29

1 Answers1

1

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);
Apurva Kolapkar
  • 1,270
  • 2
  • 16
  • 32
Sagar Kacha
  • 8,338
  • 4
  • 18
  • 27