TY Exp No. 6 Develop a program to design Checkbox and Radiobutton.

TY Exp No. 6 Develop a program to design Checkbox and Radiobutton.


1. Introduction

CheckBox

A CheckBox allows the user to select multiple options simultaneously. Each CheckBox works independently.

RadioButton

A RadioButton allows the user to select only one option from a group. It is always placed inside a RadioGroup to ensure single selection.

Below is the fully corrected, crash-free, READY-TO-PASTE implementation.

All files are rewritten carefully to ensure:

• No runtime crash
• Correct activity navigation
• Proper manifest declaration
• Clean UI and reliable logic
• Suitable for Android Studio + Medium Phone AVD + Practical Exam

You may copy–paste each file exactly as given.


1️⃣ activity_main.xml

📍 Input Page (Improved UI)

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="16dp">

        <!-- App Title -->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Checkbox &amp; RadioButton Demo"
            android:textSize="20sp"
            android:textStyle="bold"
            android:layout_marginBottom="8dp" />

        <!-- Student Name -->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Student Name: ABC XYZ"
            android:textSize="16sp"
            android:textStyle="bold"
            android:layout_marginBottom="16dp" />

        <!-- Skills -->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Select Your Skills"
            android:textSize="16sp"
            android:textStyle="bold" />

        <CheckBox
            android:id="@+id/cbJava"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Java" />

        <CheckBox
            android:id="@+id/cbAndroid"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Android" />

        <CheckBox
            android:id="@+id/cbWeb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Web Development" />

        <!-- Gender -->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Select Gender"
            android:textSize="16sp"
            android:textStyle="bold"
            android:layout_marginTop="16dp" />

        <RadioGroup
            android:id="@+id/radioGroupGender"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <RadioButton
                android:id="@+id/rbMale"
                android:text="Male" />

            <RadioButton
                android:id="@+id/rbFemale"
                android:text="Female" />

            <RadioButton
                android:id="@+id/rbOther"
                android:text="Other" />
        </RadioGroup>

        <!-- Submit -->
        <Button
            android:id="@+id/btnSubmit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Submit"
            android:layout_marginTop="20dp" />

    </LinearLayout>
</ScrollView>

2️⃣ MainActivity.java

📍 Collect input and redirect safely

package com.example.checkboxradiobuttondemo;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    CheckBox cbJava, cbAndroid, cbWeb;
    RadioGroup radioGroupGender;
    Button btnSubmit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        cbJava = findViewById(R.id.cbJava);
        cbAndroid = findViewById(R.id.cbAndroid);
        cbWeb = findViewById(R.id.cbWeb);
        radioGroupGender = findViewById(R.id.radioGroupGender);
        btnSubmit = findViewById(R.id.btnSubmit);

        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                StringBuilder skills = new StringBuilder();

                if (cbJava.isChecked()) skills.append("Java ");
                if (cbAndroid.isChecked()) skills.append("Android ");
                if (cbWeb.isChecked()) skills.append("Web Development ");

                int selectedId = radioGroupGender.getCheckedRadioButtonId();
                String gender = "Not Selected";

                if (selectedId != -1) {
                    RadioButton rb = findViewById(selectedId);
                    gender = rb.getText().toString();
                }

                Intent intent = new Intent(MainActivity.this, ResultActivity.class);
                intent.putExtra("skills", skills.toString());
                intent.putExtra("gender", gender);
                startActivity(intent);
            }
        });
    }
}

3️⃣ activity_result.xml

📍 Result Page (Clean & readable)

<?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="16dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Result Page"
        android:textSize="20sp"
        android:textStyle="bold"
        android:layout_marginBottom="12dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Student Name: ABC XYZ"
        android:textSize="16sp"
        android:textStyle="bold"
        android:layout_marginBottom="12dp" />

    <TextView
        android:id="@+id/tvSkills"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:layout_marginBottom="8dp" />

    <TextView
        android:id="@+id/tvGender"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp" />

</LinearLayout>

4️⃣ ResultActivity.java

📍 Display received data

package com.example.checkboxradiobuttondemo;

import android.os.Bundle;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class ResultActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);

        TextView tvSkills = findViewById(R.id.tvSkills);
        TextView tvGender = findViewById(R.id.tvGender);

        String skills = getIntent().getStringExtra("skills");
        String gender = getIntent().getStringExtra("gender");

        tvSkills.setText("Selected Skills: " + skills);
        tvGender.setText("Gender: " + gender);
    }
}

5️⃣ AndroidManifest.xml

📍 CRITICAL – prevents crash

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.checkboxradiobuttondemo">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.CheckBoxRadioButtonDemo">

        <activity
            android:name=".ResultActivity"
            android:exported="false" />

        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>

4. Project Directory Structure (Tree Diagram)

Project Directory Structure (Tree Diagram)

CheckBoxRadioButtonDemo │ ├── app │ ├── src │ │ ├── main │ │ │ ├── java │ │ │ │ └── com │ │ │ │ └── example │ │ │ │ └── checkboxradiobuttondemo │ │ │ │ ├── MainActivity.java │ │ │ │ └── ResultActivity.java │ │ │ │ │ │ │ ├── res │ │ │ │ ├── layout │ │ │ │ │ ├── activity_main.xml │ │ │ │ │ └── activity_result.xml │ │ │ │ │ │ │ │ ├── values │ │ │ │ ├── colors.xml │ │ │ │ ├── strings.xml │ │ │ │ └── themes.xml │ │ │ │ │ │ │ └── AndroidManifest.xml │ │ │ │ │ ├── androidTest │ │ │ └── java │ │ │ └── com │ │ │ └── example │ │ │ └── checkboxradiobuttondemo │ │ │ └── ExampleInstrumentedTest.java │ │ │ │ │ └── test │ │ └── java │ │ └── com │ │ └── example │ │ └── checkboxradiobuttondemo │ │ └── ExampleUnitTest.java │ │ │ └── build.gradle │ ├── Gradle Scripts │ ├── build.gradle (Project) │ ├── build.gradle (Module: app) │ ├── settings.gradle │ ├── gradle.properties │ └── local.properties │ └── README.md (optional)

5. Sample Output Explanation

  • User selects:

    • Java and Android (CheckBox)

    • Male (RadioButton)

  • On clicking Submit:

    • A Toast message appears:

Selected Skills: Java Android
Gender: Male

6. Learning Outcome Achievement (LLO 6.1)

✔ Demonstrated multiple selection using CheckBox
✔ Demonstrated single selection using RadioButton
✔ Proper use of RadioGroup
✔ Event handling using Button
✔ Fully executable Android application



Popular posts from this blog