title: Toggle a "ToggleButton" when clicking anywhere outside it
body: <p>I have a list (ExpandableListView), and every item of the list has a ToggleButton that shows/hides the child items when toggled (the child is always 1 and it's a sort of toolbar with two buttons). Thanks to <a href="https://www.captechconsulting.com/blog/matt-serone/android-expandablelistview-magic" rel="nofollow noreferrer">this</a> tutorial I could set a custom button instead of the expandablelistview's indicator and I made it so that I can do something else than showing the toolbar when clicking on a list item. Also, I used the answer to <a href="https://stackoverflow.com/questions/4314777/programmatically-collapse-a-group-in-expandablelistview">this question</a> to automatically close an open toolbar when opening another one.
  So, I need to collapse the currently expanded toolbar when touching anything on the screen that is not its ToggleButton (I would need to collapse it even when clicking on one of the toolbar's buttons, as long as its own "onClick" is sent anyway).</p>
  
  <p>here's an image of the app:</p>
  
  <p><img src="https://i.stack.imgur.com/D6zuB.png" alt="enter image description here"></p>
  
  <p>Here's the ExpandableListView:</p>
  
  <pre><code>&lt;ExpandableListView
      android:id="@+id/normalList"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:groupIndicator="@drawable/toggle_button_selector" &gt;
  &lt;/ExpandableListView&gt;
  </code></pre>
  
  <p>Here's the xml of the group item:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/filesListDrawerLayout"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_margin="2dp" &gt;
  
  &lt;ImageView
      android:id="@+id/img"
      android:layout_width="48dp"
      android:layout_height="48dp"
      android:layout_alignParentLeft="true"
      android:layout_alignParentTop="true"
      android:layout_margin="2dp"
      android:contentDescription="@string/icondescription" /&gt;
  
  &lt;TextView
      android:id="@+id/text"
      android:layout_width="wrap_content"
      android:layout_height="29dp"
      android:layout_alignTop="@+id/img"
      android:layout_marginLeft="15dp"
      android:layout_toRightOf="@+id/img"
      android:gravity="center_vertical"
      android:textAppearance="?android:attr/textAppearanceMedium" /&gt;
  
  &lt;TextView
      android:id="@+id/counter"
      android:layout_width="wrap_content"
      android:layout_height="19dp"
      android:layout_alignBottom="@+id/img"
      android:layout_below="@+id/text"
      android:layout_marginLeft="15dp"
      android:layout_toRightOf="@+id/img"
      android:gravity="center_vertical"
      android:textAppearance="?android:attr/textAppearanceSmall" /&gt;
  
  &lt;ToggleButton
      android:id="@+id/toggle"
      android:layout_width="25dp"
      android:layout_height="25dp"
      android:layout_alignParentRight="true"
      android:layout_below="@+id/text"
      android:layout_marginRight="15dp"
      android:background="@drawable/toggle_button_selector"
      android:focusable="false"
      android:focusableInTouchMode="false"
      android:textColorLink="@android:color/transparent"
      android:textOff=""
      android:textOn="" /&gt;
  </code></pre>
  
  <p></p>
  
  <p>The child item:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/toolbar"
  android:layout_width="match_parent"
  android:layout_height="wrap_content" &gt;
  
      &lt;TextView
          android:id="@+id/details"
          android:layout_width="0dp"
          android:layout_height="57dip"
          android:layout_weight="1"
          android:clickable="true"
          android:drawableTop="@drawable/ic_action_about"
          android:drawablePadding="-12dp"
          android:background="@drawable/toolbar_selector"
          android:gravity="center"
          android:text="@string/details" /&gt;
  
      &lt;TextView
          android:id="@+id/send"
          android:layout_width="0dp"
          android:layout_height="57dip"
          android:layout_weight="1"
          android:clickable="true"
          android:drawableTop="@drawable/ic_action_new_email"
          android:drawablePadding="-12dp"
          android:background="@drawable/toolbar_selector"
          android:gravity="center"
          android:text="@string/send" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>The relevant parts of the adapter:</p>
  
  <pre><code>public class CustomList extends BaseExpandableListAdapter {
  
      private final Activity context;
      public final List&lt;Item&gt; names; //Item is a custom Object
      private final Integer[] imageId;
      private int lastExpandedGroupPosition;
  
      public CustomList(Activity context, List&lt;Item&gt; names, Integer[] imageId) {
  
          this.context = context;
          this.names = names;
          this.imageId = imageId;
      }
  
      public View getGroupView(final int position, boolean isExpanded, View convertView, ViewGroup parent) {
  
          if (convertView == null) {
              LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              convertView = infalInflater.inflate(R.layout.list_single, null);
          }
  
          TextView txtTitle = (TextView) convertView.findViewById(R.id.text);
          TextView txtData = (TextView) convertView.findViewById(R.id.counter);
          ImageView imageView = (ImageView) convertView.findViewById(R.id.img);
          ToggleButton toggle = (ToggleButton) convertView.findViewById(R.id.toggle);
          final ExpandableListView list = (ExpandableListView) parent.findViewById(R.id.normalList);
  
          txtTitle.setText(names.get(position).getName());
  
          txtData.setText(names.get(position).getData());
  
          if(/*some conditions*/) {
  
              imageView.setImageResource(imageId[0]);
              toggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  
                  public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  
                      if(isChecked) {
  
                          list.expandGroup(position, true);
  
                      } else {
  
                          list.collapseGroup(position);
                      }
                  }
              });
  
          } else if(/*other conditions*/) {
  
              imageView.setImageResource(imageId[2]);
              toggle.setVisibility(View.GONE);
  
          } else {
  
              imageView.setImageResource(imageId[1]);
              toggle.setVisibility(View.GONE);
          }
  
          return convertView;
      }
  
      public View getChildView(int groupPosition, int childPosition,
          boolean isLastChild, View convertView, ViewGroup parent) {
  
          if (convertView == null) {
  
              LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              convertView = inflater.inflate(R.layout.list_child, null);
          }
  
  
          TextView details = (TextView) convertView.findViewById(R.id.details);
          TextView send = (TextView) convertView.findViewById(R.id.send);
  
          details.setOnClickListener(new OnClickListener() {
  
              public void onClick(View v) {
  
                  //do something
              }
          });
  
          send.setOnClickListener(new OnClickListener() {
  
              public void onClick(View v) {
  
                  //do something else
              }
          });
  
  
          return convertView;
      }
  
      @Override
      public void onGroupExpanded(int groupPosition) {
  
          ToggleButton button = (ToggleButton) ((ExpandableListView) context
              .findViewById(R.id.normalList)).getChildAt(lastExpandedGroupPosition)
              .findViewById(R.id.toggle);
  
          if(groupPosition != lastExpandedGroupPosition &amp;&amp; button.isChecked()) {
              button.performClick();
          }
  
          super.onGroupExpanded(groupPosition);
          lastExpandedGroupPosition = groupPosition;
      }
  }
  </code></pre>
  
  <p>And finally the important parts in the activity:</p>
  
  <pre><code>lv = (ExpandableListView)findViewById(R.id.normalList);
  lv.setGroupIndicator(null);
  
  lv.setOnGroupClickListener(new OnGroupClickListener() {
  
          public boolean onGroupClick(ExpandableListView parent, View v, int position, long id) {
  
              //do some things
  
              return true; //this tells the list that it mustn't show the child items
          }
  });
  
  adapter = new CustomList(myActivity.this, list, icons);
  
  lv.setAdapter(adapter);
  </code></pre>
  
  <p>P.S.: The code to hide the open toolbars doesn't work properly, but I really can't understand why. But this is another story...</p>
  
  <p>--- EDIT ---</p>
  
  <p>About the P.S. above: <a href="https://stackoverflow.com/a/23677136/3514976">now I know why that happens</a>.</p>