Tuesday 12 February 2013

How to get RadioButton selected value in Android ?

We will get selected radio button text in 4 simple steps
----------------------------------------------------------------

1. getting RadioGroup reference
RadioGroup radioGroup = (RadioGroup)      
                                                findViewById(R.id.radioGroup);

2. fetching Checked RadioButton id
int id = radioGroup.getCheckedRadioButtonId();

3. getting RadioButton reference
RadioButton radioButton = (RadioButton) findViewById(id);

4. fetching selected RadioButton text
String selectedValue = radioButton.getText().toString();

-----------------------------------------------------------------------------

Screen Shots:

-----------------------------------------------------------------------------

1. MainActivity
public class MainActivity extends Activity {

          @Override
          protected void onCreate(Bundle savedInstanceState) {
                   super.onCreate(savedInstanceState);
                   setContentView(R.layout.activity_main);

                   // getting Button reference
                   Button button = (Button) findViewById(R.id.buttonGet);

                   button.setOnClickListener(new OnClickListener() {

                             @Override
                             public void onClick(View v) {

                                      // getting RadioGroup reference
                                      RadioGroup radioGroup = (RadioGroup)      
                                                findViewById(R.id.radioGroup);

                                      // fetching Checked RadioButton id
                                      int id =
                                                radioGroup.getCheckedRadioButtonId();

                                      // getting RadioButton reference
                                      RadioButton radioButton = (RadioButton)
                                                findViewById(id);

                                      // fetching selected RadioButton text
                                      String selectedValue =
                                                radioButton.getText().toString();

                                      // show output in Log
                                      Log.e("selected value", selectedValue);

                                      // show output in Toast message
                                      Toast.makeText(getBaseContext(),
                                                          "Selected Value : " +
                                                selectedValue, Toast.LENGTH_SHORT)
                                                         .show();
                             }
                   });

          }
}

2.  activity_main.xml
<LinearLayout 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:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="Select Your Qualification"
        android:textSize="20dp" />

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="30dp" >

        <RadioButton
            android:id="@+id/rb1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="10th" />

        <RadioButton
            android:id="@+id/rb2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Intermediate" />

        <RadioButton
            android:id="@+id/rb3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="UG" />

        <RadioButton
            android:id="@+id/rb4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="PG" />

        <RadioButton
            android:id="@+id/rb5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="PHD" />
    </RadioGroup>

    <Button
        android:id="@+id/buttonGet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:text="Fetch Value"
        android:textSize="20dp"
        android:textStyle="bold" />

</LinearLayout>


************************************

    Download Full Source Code HERE


************************************

No comments:

Post a Comment