Cover Image for RadioButton in Android
83 views

RadioButton in Android

The Android RadioButton is a user interface component used to represent a choice or an option within a group of mutually exclusive options. Users can select only one RadioButton at a time within a specific group. RadioButton is often used in conjunction with a RadioGroup to create sets of related options where only one option can be selected. Here’s how to use RadioButton in Android:

Add a RadioGroup to Your Layout: In your XML layout file, add a RadioGroup element to group the RadioButton options:

XML
 <RadioGroup
     android:id="@+id/radio_group"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:orientation="vertical">
     <!-- Add RadioButtons here -->
 </RadioGroup>
  • android:id: Assign a unique ID to the RadioGroup for later reference.
  • android:layout_width and android:layout_height: Adjust these attributes based on your layout requirements.
  • android:orientation: Set it to "vertical" or "horizontal" depending on how you want the RadioButtons to be laid out.

Add RadioButtons to the RadioGroup: Within the RadioGroup, add individual RadioButton elements. Each RadioButton represents a choice or option:

XML
 <RadioButton
     android:id="@+id/radio_option1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Option 1" />

 <RadioButton
     android:id="@+id/radio_option2"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Option 2" />

 <!-- Add more RadioButtons as needed -->
  • android:id: Assign unique IDs to each RadioButton for later reference.
  • android:layout_width and android:layout_height: Adjust these attributes based on your layout requirements.
  • android:text: Specify the text label for each RadioButton.

Access the RadioGroup and Handle Selection: In your Java or Kotlin code (usually within your activity or fragment), obtain references to the RadioGroup and individual RadioButtons. Then, you can handle the selection as follows:

Java
 RadioGroup radioGroup = findViewById(R.id.radio_group);
 RadioButton option1RadioButton = findViewById(R.id.radio_option1);
 RadioButton option2RadioButton = findViewById(R.id.radio_option2);

 // Set an initial selection (optional)
 option1RadioButton.setChecked(true);

 // Handle RadioButton selection
 radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
     @Override
     public void onCheckedChanged(RadioGroup group, int checkedId) {
         if (checkedId == R.id.radio_option1) {
             // Option 1 is selected
             // Handle option 1 selection
         } else if (checkedId == R.id.radio_option2) {
             // Option 2 is selected
             // Handle option 2 selection
         }
     }
 });
  • You can set an initial selection using the setChecked(true) method on one of the RadioButtons.
  • Use the setOnCheckedChangeListener method to listen for changes in the selected RadioButton.

Run Your App: When you run your app, the RadioButtons will be displayed, and users can select one option at a time. The onCheckedChanged method will be called when the user makes a selection, allowing you to handle their choice.

You can easily implement a group of mutually exclusive RadioButtons in your Android app to allow users to select one option from a list of choices.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS