title: Android getting value from selected radiobutton
body: <p>I have a piece of code with three <code>RadioButton</code>s within a <code>RadioGroup</code>. I want to set an <code>onCheckedListener</code> that will show the value of the <code>RadioButton</code> in a <code>Toast</code>. However what I have gotten so far is not working. How do I get the value of the <code>RadioButton</code> and display it in a <code>Toast</code>? This is my code:</p>
  
  <pre><code>import android.os.Bundle;
  import android.app.Activity;
  import android.view.Menu;
  import android.view.View;
  import android.widget.RadioButton;
  import android.widget.RadioGroup;
  import android.widget.RadioGroup.OnCheckedChangeListener;
  import android.widget.Toast;
  
  public class MainActivity extends Activity {
  
      RadioGroup rg;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1);
          final String value =
              ((RadioButton)findViewById(rg.getCheckedRadioButtonId()))
              .getText().toString();
  
          rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
              public void onCheckedChanged(RadioGroup group, int checkedId) {
                  Toast.makeText(getBaseContext(), value, Toast.LENGTH_SHORT).show();
              }
          });
      }
  }
  </code></pre>
  
  <p>XML file:</p>
  
  <pre><code>&lt;RelativeLayout 
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      tools:context=".MainActivity" &gt;
  
      &lt;RadioGroup
          android:id="@+id/radioGroup1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentTop="true"
          android:layout_centerHorizontal="true"
          android:layout_marginTop="152dp" &gt;
  
          &lt;RadioButton
              android:id="@+id/radio0"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Choose 1" /&gt;
  
           &lt;RadioButton
              android:id="@+id/radio1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Choose 2" /&gt;
  
           &lt;RadioButton
              android:id="@+id/radio2"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Choose 3" /&gt;
       &lt;/RadioGroup&gt;
   &lt;/RelativeLayout&gt;
  </code></pre>