In this example showing Radio buttons with a radio group and determining which radio button is selected.

Radio buttons are normally used together in a RadioGroup. When several radio buttons live inside a radio group, checking one radio button unchecks all the others.

we will use “android.widget.RadioButton” class to render radio button, and those radio buttons are usually grouped by android.widget.RadioGroup. If RadioButtons are in group, when one RadioButton within a group is selected, all others are automatically deselected.

 

Project Structure :


radio button project sketch

 

File : res/layout/activity_radio_button_example.xml

 


<?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
            <RadioGroup
                android:id="@+id/radioGender"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >
        
                <RadioButton
                    android:id="@+id/genderMale"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Male"
                    android:checked="true" />
        
                <RadioButton
                    android:id="@+id/genderFemale"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Female" />
        
            </RadioGroup>
        
            <Button
                android:id="@+id/btnDisplay"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Click Here" />
    
     </LinearLayout>

 

File : RadioButtonExample.java

 


    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;
    import android.widget.Toast;
    
    public class RadioButtonExample extends Activity {
    
                private RadioGroup radioSexGroup;
                private RadioButton radioSexButton;  
                private Button btnDisplay;
            
                @Override
                public void onCreate(Bundle savedInstanceState) {
    
    
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_radio_button_example);
            
                    addListenerOnButton();
            
                }
            
                public void addListenerOnButton() {
            
                    radioSexGroup = (RadioGroup) findViewById(R.id.radioGender);
                    btnDisplay = (Button) findViewById(R.id.btnDisplay);
            
                    btnDisplay.setOnClickListener(new OnClickListener() {
            
                        @Override
                        public void onClick(View v) {
            
                            // get selected radio button from radioGroup
                            int selectedId = radioSexGroup.getCheckedRadioButtonId();
            
                            // find the radiobutton by returned id
                            radioSexButton = (RadioButton) findViewById(selectedId);
            
                            Toast.makeText(RadioButtonExample.this,
                                    radioSexButton.getText(), Toast.LENGTH_SHORT).show();
            
                        }
            
                    });
            
                }
    }

 

Conclusion :

Final words- Just download the project and run it using your emulator and if you have any doubts or suggestions, please place them in the comments.