Android Tutorial

Software Setup and Configuration

Android Studio

File Structure

Components

Core Topics

Layout

View

Button

Intent and Intent Filters

Toast

RecyclerView

Fragments

Adapters

Other UI Component

Image Loading Libraries

Date and Time

Material Design

Bars

Working with Google Maps

Chart

Animation

Database

Advance Android

Jetpack

Architecture

App Publish

App Monetization

How to add Radio Buttons in an Android Application?

Adding radio buttons to an Android application allows you to present a list of options where the user can select only one option from the set. To group radio buttons and ensure that only one can be selected at a time, you should use the RadioGroup widget.

Here's a step-by-step guide to add radio buttons in an Android application:

1. Create a new XML Layout:

In your activity_main.xml (or any other XML layout file), add a RadioGroup and some RadioButton elements inside it:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:orientation="vertical">

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

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

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

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

    </RadioGroup>

    <Button
        android:id="@+id/submitButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Submit" />

</LinearLayout>

2. Add Logic in Your Activity:

In your MainActivity.kt:

import android.os.Bundle
import android.widget.Button
import android.widget.RadioButton
import android.widget.RadioGroup
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val radioGroup: RadioGroup = findViewById(R.id.radioGroup)
        val submitButton: Button = findViewById(R.id.submitButton)

        submitButton.setOnClickListener {
            val selectedId = radioGroup.checkedRadioButtonId
            val radioButton: RadioButton = findViewById(selectedId)
            Toast.makeText(this, "Selected: ${radioButton.text}", Toast.LENGTH_SHORT).show()
        }
    }
}

In the above Kotlin code:

  • We get a reference to our RadioGroup and the submit Button.
  • When the submit button is clicked, we find out which RadioButton is selected within the RadioGroup by calling checkedRadioButtonId on the RadioGroup.
  • We then get a reference to that RadioButton and display its text using a Toast.

3. Run Your App:

Now, when you run your app, you'll see the radio buttons. When you click the submit button, the app will display a toast message with the selected radio button's text.

  1. Adding Radio Buttons in Android Studio:

    Include a RadioButton in your XML layout:

    <RadioButton
        android:id="@+id/radioButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Radio Button"/>
    
  2. Using RadioGroup with Radio Buttons in Android:

    Group RadioButtons using a RadioGroup to ensure only one can be selected:

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    
        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Option 1"/>
    
        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Option 2"/>
    </RadioGroup>
    
  3. Customizing Radio Buttons in Android example:

    Customize the appearance of RadioButtons using XML attributes or styles:

    <RadioButton
        android:id="@+id/radioButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Customized Radio Button"
        android:textSize="18sp"
        android:textColor="@color/customColor"/>
    
  4. Handling Radio Button selection in Android:

    Set up a listener to handle radio button selection:

    val radioGroup = findViewById<RadioGroup>(R.id.radioGroup)
    
    radioGroup.setOnCheckedChangeListener { group, checkedId ->
        // Handle radio button selection
        val radioButton: RadioButton = findViewById(checkedId)
        // Do something with radioButton
    }
    
  5. Setting up Radio Button groups in Android app:

    Use RadioGroup to group related RadioButtons:

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    
        <!-- RadioButtons go here -->
    
    </RadioGroup>
    
  6. Styling and theming Radio Buttons in Android:

    Apply styles and themes to RadioButtons in your styles.xml:

    <style name="RadioButtonStyle" parent="Widget.AppCompat.CompoundButton.RadioButton">
        <!-- Custom styling here -->
    </style>
    

    Apply the style to your RadioButton:

    <RadioButton
        android:id="@+id/radioButton"
        style="@style/RadioButtonStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Styled Radio Button"/>
    
  7. Listening to Radio Button changes in Android:

    Use the OnCheckedChangeListener to listen for changes:

    val radioGroup = findViewById<RadioGroup>(R.id.radioGroup)
    
    radioGroup.setOnCheckedChangeListener { group, checkedId ->
        // Handle radio button selection
    }
    
  8. RadioButton XML attributes in Android:

    Customize RadioButtons using various XML attributes such as android:checked, android:text, android:textColor, etc.

    <RadioButton
        android:id="@+id/radioButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Custom Radio Button"
        android:checked="true"
        android:textColor="@color/customColor"/>