5

(Android API version 9)I created a spinner with a custom adapter and overrided getView() to inflate it with my xml file which has a text view. But now, my spinner is not closing the dropdown list after user selects an item. Is there anyway to close the spinner dropdown upon selecting an item?

Code

//Code in onCreate function
    Spinner list = (Spinner) findViewById(R.id.spn_purchaseList);
    listAdapter = new ItemListAdapter(this, new MyItemList());
    list.setAdapter(listAdapter);
    listAdapter.item_list.addItem(new MyItem("Test", "Test Item"));
    listAdapter.notifyDataSetChanged();
//onCreate end
//the class below is inside "MainActivity extends Activity"
class ItemListAdapter extends BaseAdapter
{
    Context context;
    MyItemList item_list;
    MyItem selectedItem;


    ItemListAdapter(Context con,MyItemList k)
    {
        super();
        this.context=con;
        this.item_list=k;
        selectedItem=null;
    }

    @Override
    public int getCount() {

        return item_list.getCount();
    }

    @Override
    public MyItem getItem(int arg0) {

        return this.item_list.getList().get(arg0);
    }

    @Override
    public long getItemId(int arg0) {

        return  this.item_list.getPosition(this.item_list.getList().get(arg0));
    }

    @Override
    public View getView(int position, View arg1, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        View spinner_item = inflater.inflate(R.layout.spinner_layout, parent, false);

        TextView tx = (TextView)spinner_item.findViewById(R.id.txt_spinner);
        tx.setId((int) item_list.getPosition(item_list.getList().get(position)));


        tx.setText(this.item_list.getList().get(position).name.toString());
        tx.setBackgroundResource(R.drawable.spinner_item);

        tx.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                selectedItem = item_list.getItem(v.getId());
                list.setSelection(v.getId());



            }
        });

        return spinner_item;
    }

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


        return getView(position,convertView,parent);

    }

}
2
  • 3
    show us the code you tried Commented Nov 26, 2013 at 9:44
  • 1
    I have came up with an answer, but i'm not sure whether it is a good one. I'm still working to find a better answer. Here is my solution. In the getView() method, inside the onClick(view v) function in onClickListener of TextView tx, I added the following code ((View)v.getParent().getParent().getParent().getParent().getParent().getParent()).setVisibility(View.GONE); Commented Nov 26, 2013 at 11:22

3 Answers 3

1

Calling setVisibility(View.GONE) works to hide the dropdown but it seems to cause issues with the Spinner state, i.e. you will be unable to reopen the dropdown after it has been closed.

The preferred way is to get a handle to the Spinner and call its onDetachedFromWindow() from your onClick() listener.

@Override
public void onClick(View v) {
    // code here to get selected item and do something with it

    // hide the spinner dropdown
    Spinner spinner = (Spinner) findViewById(R.id.mySpinner);
    if (spinner != null) {
        try {
            Method method = Spinner.class.getDeclaredMethod("onDetachedFromWindow");
            method.setAccessible(true);
            method.invoke(spinner);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
2
  • Thank you! Saved my day wasted completely in desperate attempts to make it close Commented Sep 17, 2016 at 18:13
  • You can't capture events though Commented Aug 25, 2022 at 18:16
1

Too late, but for my case I had a custom layout for spinner items. The clickable="true" or adding onClickListeners, onItemSelectedListeners didn't work because I was adding them to root layout.

When I changed my code as following, I added android:background="?attr/selectableItemBackground" to child of parent layout, and set OnItemSelectedListener() on Spinner, and it worked. Spinner dialog or dropdown hides when item is tapped.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:background="?attr/selectableItemBackground">

         <!-- your custom spinner item view -->

    </LinearLayout>

</LinearLayout>
0

just call spinner.dismissDropDown() method of spinner, inside on item click of spinner. Your problem will be solved.

1
  • That method does not exist Commented Aug 25, 2022 at 17:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.