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
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:
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>
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:
RadioGroup
and the submit Button
.RadioButton
is selected within the RadioGroup
by calling checkedRadioButtonId
on the RadioGroup
.RadioButton
and display its text using a Toast
.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.
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"/>
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>
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"/>
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 }
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>
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"/>
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 }
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"/>