Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close Spinner on click outside of Spinner

I want to close an Android spinner once I click outside of the Spinner. Is that even possible?

like image 784
ZXingIT Avatar asked Sep 14 '11 12:09

ZXingIT


People also ask

How do you close a spinner?

Now that being said, calling spin. onDetachedFromWindow(); from anywhere in your Activity should help you to close the spinner programatically.

What is use of spinner control?

Spinners provide a quick way to select one value from a set. In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one.

Which of the following look like a dropdownlist in Android?

Spinner is a widget similar to a drop-down list for selecting items. In this tutorial, we show you how to do the following tasks : Render a Spinner in XML, and load the selection items via XML file also. Render another Spinner in XML, and load the selection items via code dynamically.


2 Answers

I've had some luck with this, even if it doesn't completely work.

The spinner adapter's get view :

public View getView(int position, View v, ViewGroup parent) {
   if (v == null) {
      LayoutInflater mLayoutInflater = mActivity.getLayoutInflater();
      v = mLayoutInflater.inflate(R.layout.user_row, null);
   }

   View tempParent = (View) parent.getParent().getParent().getParent();
   tempParent.setOnTouchListener(new MyOnTouchListener(mActivity));
   mActivity.setOpen(true);

   User getUser = mUsers.get(position);
   return v;
}

The ontouch listener :

public class MyOnTouchListener implements OnTouchListener{
   private MyActivity mOverall;
   public MyTouchListener(MyActivity overall) {
      mOverall = overall;
   }

   public boolean onTouch(View v, MotionEvent event) {
      if (mOverall.getOpen()) {
         mOverall.getWindow().setContentView(R.layout.main); //reset your activity             screen
         mOverall.initMainLayout(); //reset any code.  most likely what's in your oncreate
      }
      return false;
   }  
}

The on item selected listener :

public class MySelectedListener implements OnItemSelectedListener {
   public void onItemSelected(AdapterView<?> parent, View view, int pos,
    long id) {
     setUser(pos); //or whatever you want to do when the item is selected
     setOpen(false);         
  }
  public void onNothingSelected(AdapterView<?> parent) {}
}

The activity

Your activity with the spinner should have a global variable mOpen with get and set methods. This is because the ontouch listener tends to stay on even after the list is closed.

The limitations of this method:

It closes if you touch between the spinner and the options or to the side of the options. Touching above the spinner and below the options still won't close it.

like image 146
RasTheDestroyer Avatar answered Oct 04 '22 03:10

RasTheDestroyer


Try

@Override
public void onFocusChange(View v, boolean hasFocus) {

if (!hasFocus) v.setVisibility(View.GONE);

}
like image 33
Ginger McMurray Avatar answered Oct 04 '22 05:10

Ginger McMurray