Android spinner highlight selected item
Asked Answered
P

4

9

I've got basic android spinner and I would like to have, after clicking it, the list with items with one of them highlighted, the one, that was originally selected.

Like it's done here: http://www.warriorpoint.com/blog/wp-content/uploads/2009/05/05spinner-thumb.png

But with my own layout of the items and not with the radio box, but with my own background instead.

How can I achieve this? Is there anything of use in the selector, or do I have to do it programatically?

Any help is appreciated.

Pendergast answered 13/9, 2012 at 12:39 Comment(0)
P
21

Below is an solution I tested and verified. You can use setSelection(N) to highlight the item you want.

class HighLightArrayAdapter extends ArrayAdapter<CharSequence> {

        private int mSelectedIndex = -1;


        public void setSelection(int position) {
            mSelectedIndex =  position;
            notifyDataSetChanged();
        }

        public HighLightArrayAdapter(Context context, int resource, CharSequence[] objects) {
            super(context, resource, objects);
        }


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

            if (position == mSelectedIndex) {
                itemView.setBackgroundColor(Color.rgb(56,184,226));
            } else {
                itemView.setBackgroundColor(Color.TRANSPARENT);
            }

            return itemView;
        }
    }
Panslavism answered 29/10, 2015 at 3:44 Comment(2)
Great answer! For clarity though, setSelection(pos) would need to be added in the onItemSelected() listener of the spinner.Distilled
It works only for the first display of items. If you select an different item then open the spinner again, the initially selected item will be highlighted still.Bircher
S
6
public class MainActivity extends Activity {

Spinner mySpinner;
String[] myArray={"1","2","3","4","5"};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    mySpinner = (Spinner)findViewById(R.id.spinner_test);

    mySpinner.setAdapter(new MyAdapter(this,android.R.layout.simple_spinner_item, myArray));

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public class MyAdapter extends ArrayAdapter<String>{

    public MyAdapter(Context context, int textViewResourceId,
            String[] objects) {
        super(context, textViewResourceId, objects);
        // TODO Auto-generated constructor stub
    }

    @Override
    public View getDropDownView(int position, View cnvtView, ViewGroup prnt){
        LayoutInflater inflater = getLayoutInflater();
        View spinnerItem = inflater.inflate(android.R.layout.simple_spinner_item, null);

        TextView mytext= (TextView)spinnerItem.findViewById(android.R.id.text1);
        mytext.setText(myArray[position]);

        //int selected = Spinner.
        int selected = mySpinner.getSelectedItemPosition();
        if(position == selected){
            spinnerItem.setBackgroundColor(Color.BLUE);
        }
        return spinnerItem;

    }

}

}

This should help.

Shirleneshirley answered 27/12, 2013 at 21:43 Comment(1)
if I have one adapter and two spinners, how do I find mySpinner?Mcloughlin
C
4

The Kotlin version of this is more straighforward:

    val spinner = Spinner(context)
    val adapter = object: ArrayAdapter<Any>(context, android.R.layout.simple_spinner_item, choices.toTypedArray()) {
        override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View {
            return super.getDropDownView(position, convertView, parent).also { view ->
                if(position == spinner.selectedItemPosition)
                    view.setBackgroundColor(Color.rgb(204, 255, 255))
            }
        }
    }
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item
    spinner.adapter = adapter

You can obviously set other properties of the view - the view returned by android.R.layout.simple_spinner_dropdown_item is a subclass of TextView so you can set text alignment and similar properties, e.g. to right-justify the text:

                (view as? TextView)?.textAlignment = TEXT_ALIGNMENT_VIEW_END
Cheddar answered 9/7, 2019 at 22:24 Comment(0)
P
-3

To answer my question, you need to have something like this:

public class mySpinnerAdapter extends SimpleCursorAdapter implements SpinnerAdapter {

public OrderSpinnerAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
    super(context, layout, c, from, to);
    // TODO whatever you need
}

public View getDropDownView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = mInflater.inflate(android.R.layout.simple_dropdown_item_1line, parent, false);
    }

    // TODO set the background color of the convertView
    // depending on your wishes

    return convertView;
}
}

So that we can control creating of the dropdown list like this. If you need different selectors, you can do it easily in the XML file.

Then, when you just simply create the adapters and bind them to the spinner with method setAdapter.

Pendergast answered 18/9, 2012 at 12:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.