VanAlan
VanAlan

Reputation: 65

How to make spinner dialog display currently selected item?

I know I can set a spinner's currently selected item using mySpinner.setSelection(index);. But when I want to select another item, the spinner's dialog never shows/highlights the currently selected item in the list.

Is there any way we can configure the spinner so we can clearly see in the dialog which item is currently selected (whether with a check mark or by changing the background color of the currently selected item)?

Thanks,

Alain

Upvotes: 3

Views: 1097

Answers (1)

Martin Cazares
Martin Cazares

Reputation: 13705

Unfortunately this behavior is not natively implemented in Spinner component, however, you can always create your own BaseAdapter to show whatever you need weather is in the spinner it self or in the dropdown like this:

private class ExampleAdapter extends BaseAdapter{

    @Override
    public int getCount() {
        return 0;
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
                    //Here is where you actually get the chance to return whatever you want in the spinner component (the single bar with the arrow)
        return yourCommonView;
    }

    @Override
    public View getDropDownView(int position, View convertView,
            ViewGroup parent) {
              //Here is where you get the chance to return whatever you want in the dropdown menu so here you should validate what's the currently selected element and return an image accordingly...
        return yourSelectedView;
    }

}

The important method here is, getDropDownView that is the one that gives you the chance to return an element with a checked CheckBox, or any mark you want to use, of course you have to create your own layout and validate if the element currently created need to be marked or not...

Regards!

Upvotes: 4

Related Questions