title: Howto: Implement an OnClickListener for ToggleButtons in AlertDialog?
body: <p>In my android app, i made a custom Alert Dialog. It opens on a Button click. In that dialog there are 4 togglebuttons that should react like radiobuttons. i dont want to use radiobuttons for design reasons. </p>
  
  <p>I can't get it done to implement an onClick Listener for those togglebuttons. I to get them in a way, that only one button can be 'On' and the rest of them should be off. (Like a RadioButton Group)</p>
  
  <p>I can't currently upload an image with my reputation, so here is the xml instead:</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/Dlg_DrvPut"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" &gt;
  &lt;TextView
      android:id="@+id/textView2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:gravity="center_vertical|center_horizontal"
      android:text="@string/DrvPutDlgTitle"
      android:textAppearance="?android:attr/textAppearanceLarge" /&gt;
  &lt;TextView
      android:id="@+id/textView1"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_x="1dp"
      android:layout_y="3dp"
      android:gravity="center_vertical|center_horizontal"
      android:text="@string/DrvPutDlgDrvCap" /&gt;
  &lt;TableLayout
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:shrinkColumns="*"
      android:stretchColumns="*"  &gt;
  &lt;TableRow
          android:id="@+id/tableRow1"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"&gt;"
          &lt;ToggleButton
              android:id="@+id/rb_hit"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
  
              android:textOn="Hit"
              android:textOff="Hit"
              android:layout_column="0" &gt;
          &lt;/ToggleButton&gt;
          &lt;ToggleButton
              android:id="@+id/rb_left"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
  
              android:textOn="Left"
              android:textOff="Left"
              android:layout_column="1" /&gt;
          &lt;ToggleButton
              android:id="@+id/rb_right"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
  
          android:textOn="Right"
              android:textOff="Right"
  
              android:layout_column="2" /&gt;
          &lt;ToggleButton
              android:id="@+id/rb_miss"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
  
              android:textOn="Miss"
              android:textOff="Miss"
              android:layout_column="3" /&gt;
        &lt;/TableRow&gt;
     &lt;/TableLayout&gt;
          &lt;EditText
              android:id="@+id/drvDst"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:ems="10"
              android:inputType="number" &gt;
          &lt;/EditText&gt;
    &lt;TextView
      android:id="@+id/textView1"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_x="1dp"
      android:layout_y="3dp"
      android:gravity="center_vertical|center_horizontal"
      android:text="@string/DrvPutDlgPutCap" /&gt;
  
       &lt;EditText
           android:id="@+id/putDst"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:ems="10"
           android:inputType="numberDecimal" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>How can this be done?</p>
  
  <p>I tried the mentioned onCheckedChangeListener but this seems to be unavailable in AlertDialogBuilder.
  Here is the code which will be run through after the button ist pressed to show the alertdialog.
  This is the now working code with an onCheckedChangeListener for each toggle button. The checked state is set to false for all the other toggle buttons if one of the buttons is checked by the user.</p>
  
  <pre><code>public void pDrvPutClick(View v) {
       final Button b = (Button) v;
  
        LayoutInflater inflater = getLayoutInflater();
       View dstView = inflater.inflate(R.layout.dlg_drvput, null);
  
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
       builder.setView(dstView);
       final EditText edit_ddst = (EditText) dstView.findViewById(R.id.drvDst);
       final EditText edit_pdst = (EditText) dstView.findViewById(R.id.putDst);
       final ToggleButton rb1 = (ToggleButton) dstView.findViewById(R.id.rb_hit); 
       final ToggleButton rb2 = (ToggleButton) dstView.findViewById(R.id.rb_left);
       final ToggleButton rb3 = (ToggleButton) dstView.findViewById(R.id.rb_right);
       final ToggleButton rb4 = (ToggleButton) dstView.findViewById(R.id.rb_miss);
  
      rb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
       @Override
           public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
       {
           if ( isChecked )
           {
               rb2.setChecked(false);
               rb3.setChecked(false);
               rb4.setChecked(false);
           }
       }
       });
  
       rb2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
           @Override
               public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
           {
               if ( isChecked )
               {
                   rb1.setChecked(false);
                   rb3.setChecked(false);
                   rb4.setChecked(false);
               }
           }
           });
       rb3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
           @Override
               public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
           {
               if ( isChecked )
               {
                   rb1.setChecked(false);
                   rb2.setChecked(false);
                   rb4.setChecked(false);
               }
           }
           });
       rb4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
           @Override
               public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
           {
               if ( isChecked )
               {
                   rb1.setChecked(false);
                   rb3.setChecked(false);
                   rb2.setChecked(false);
               }
           }
           });
  
  
  
       builder.setPositiveButton("OK", new DialogInterface.OnClickListener(){
           public void onClick(DialogInterface dialog, int whichButton){
               String ddst;
               ddst = edit_ddst.getText().toString();
               if (ddst=="") {
                  ddst="Drive";    
               }else {
                   if (rb1.isChecked()){
                       ddst="H"+ddst;
                   }
                  if (rb2.isChecked()){
                       ddst="L"+ddst;
                   }
                  if (rb3.isChecked()){
                       ddst="R"+ddst;
                   }
                  if (rb4.isChecked()){
                       ddst="M"+ddst;
                   }
               }
               String pdst;
  
               pdst = edit_pdst.getText().toString();
               if (pdst=="") {
                  pdst="PutDst";   
               }
               b.setText(ddst+"\n"+pdst);
  
  
           }
  
  
       });
  </code></pre>