Introduction
In Android applications, CheckBox and RadioButton are used to collect user choices.
- CheckBox allows users to select multiple options from a list.
- RadioButton allows users to select only one option from a group.
These components are commonly used in registration forms, surveys, settings pages, and preference selections.
Step 1: Create a New Android Project
- Open Android Studio
- Click New Project
- Select Empty Activity
- Choose Java as the programming language
- Click Finish
Step 2: activity_main.xml
Open res โ layout โ activity_main.xml
<?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:orientation="vertical"
android:padding="20dp">
<!-- CheckBox Section -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Your Hobbies"
android:textSize="18sp"/>
<CheckBox
android:id="@+id/cbMusic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Music"/>
<CheckBox
android:id="@+id/cbSports"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sports"/>
<CheckBox
android:id="@+id/cbReading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reading"/>
<!-- Radio Button Section -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Gender"
android:textSize="18sp"
android:layout_marginTop="20dp"/>
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/rbMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male"/>
<RadioButton
android:id="@+id/rbFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"/>
</RadioGroup>
</LinearLayout>
Output
When the application runs:
- Users can select multiple hobbies using CheckBox.
- Users can select one gender using RadioButton.
- The selected options can be displayed using a Toast message.
Conclusion
In this program, we designed CheckBox and RadioButton components in an Android application.
- CheckBox allows selecting multiple options.
- RadioButton allows selecting only one option from a group.
These components are widely used in forms, surveys, and user preference settings.