title: Toggle between a RadioGroup of Toggle Buttons
body: <p>So, I have my buttons lined up like below: </p>
  
  <p>Here's an image of what I'm trying to achieve: </p>
  
  <p><img src="https://i.stack.imgur.com/oMQDd.png" alt="enter image description here"></p>
  
  <p>In the image, the first row is just a row of buttons with no button pressed. 
  In the second row, only the first button is pressed. In the third row, I clicked on 4 and only the fourth button is pressed (not the first one anymore). </p>
  
  <pre><code>    &lt;RadioGroup
  
              android:id="@+id/toggleGroup"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
  
              android:background="@color/white"
              android:useDefaultMargins="true"
  
              android:layout_column="0"
              android:columnCount="4"
              android:rowCount="1"
              android:orientation="horizontal"
  &gt;
  
      &lt;ToggleButton
  
                  android:id="@+id/number_zero"
                  android:layout_width="34sp"
                  android:layout_height="40sp"
                  android:textOn="@string/number_zero"
                  android:textOff="@string/number_zero"
                  android:onClick="onToggle"
                  android:checked="true"
                  android:background="@drawable/psc_number_color"
                  android:layout_margin="5sp"
  
                  /&gt;
      &lt;ToggleButton
                  android:id="@+id/number_one"
                  android:layout_width="34sp"
                  android:layout_height="40sp"
                  android:textOn="@string/number_one"
                  android:textOff="@string/number_one"
                  android:onClick="onToggle"
                  android:checked="true"
                  android:background="@drawable/psc_number_color"
                  android:layout_margin="5sp"
  
              /&gt;
  
      &lt;ToggleButton
                  android:id="@+id/number_two"
                  android:layout_width="34sp"
                  android:layout_height="40sp"
                  android:textOn="@string/number_two"
                  android:textOff="@string/number_two"
                  android:background="@drawable/psc_number_color"
                  android:layout_margin="5sp"
                  /&gt;
  &lt;/RadioGroup&gt;
  </code></pre>
  
  <p>I was wondering if it would be possible to toggle between these five buttons in such a way that if I pressed one down, the button would remain pressed. If I click on another button in that group, the button I clicked on before would depress and the new button I clicked on would be pressed down. </p>
  
  <p>Trying to do this inside a pageradapter: </p>
  
  <pre><code>   public void onToggle(View view) {
          ((RadioGroup)view.getParent()).check(view.getId());
          // app specific stuff ..
      }
  
     public Object instantiateItem(ViewGroup parent, int position) {
  
                  one = (ToggleButton) view.findViewById(R.id.number_one);
                  two = (ToggleButton) view.findViewById(R.id.number_two);
                  three = (ToggleButton) view.findViewById(R.id.number_three);
                  four = (ToggleButton) view.findViewById(R.id.number_four);
  
                  final RadioGroup.OnCheckedChangeListener ToggleListener = new RadioGroup.OnCheckedChangeListener() {
                      @Override
                      public void onCheckedChanged(final RadioGroup radioGroup, final int i) {
                          for (int j = 0; j &lt; radioGroup.getChildCount(); j++) {
                              final ToggleButton view = (ToggleButton) radioGroup.getChildAt(j);
                              view.setChecked(view.getId() == i);
                          }
                      }
                  };
  
                  one.setOnClickListener(new View.OnClickListener() {
  
                      @Override
                      public void onClick(View v) {
                          one.toggle();
                      }
                  });
  
                  ((RadioGroup) view.findViewById(R.id.toggleGroup)).setOnCheckedChangeListener(ToggleListener);
  
  }
  </code></pre>