# Q7

### Create a radio button group with radio button of all courses in your college and on selecting a particular course, teacher-in-charge of that course should appear at the bottom of the screen.

{% hint style="info" %}
**App Insights:**

Used **RadioGroup** and **RadioButton** and pretty much added the menu options that way and wrote logic code in **MainActivity**, also used **TextView** to display output

[Browse Source Code](https://github.com/MJK618/AndroidProgramming/tree/main/Source%20Code%20for%20Questions/Q7_Radio_Buttons)
{% endhint %}

![](https://3448212806-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MZCvUCwpAEgH8ewpyoU%2F-MZJHvKl_nBhFIWnPCRp%2F-MZJO5u6pT2pqxmy8dwR%2Fimage.png?alt=media\&token=e77bac46-3c7e-41f7-8c32-e885f539db9f)

### **Code**

{% tabs %}
{% tab title="activity\_main.xml" %}

```markup
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    >

    <TextView
        android:id="@+id/t1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="@string/radio_button_demo" />

    <RadioGroup
        android:id="@+id/rg1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="150dp"
        android:paddingLeft="50dp"
        >

        <RadioButton
            android:id="@+id/rb1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Python Programming" />

        <RadioButton
            android:id="@+id/rb2"
            android:layout_width="229dp"
            android:layout_height="wrap_content"
            android:text="Internet Technologies" />

        <RadioButton
            android:id="@+id/rb3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="DBMS" />

        <RadioButton
            android:id="@+id/rb4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Operating Systems" />

        <RadioButton
            android:id="@+id/rb5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Java Programming" />
    </RadioGroup>

</RelativeLayout>
```

{% endtab %}

{% tab title="MainActivity.java" %}

```java
package com.example.q7_radio_buttons;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private RadioGroup rgrp;
    private RadioButton r1,r2,r3,r4,r5;
    private TextView t1;

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

        rgrp = (RadioGroup)findViewById(R.id.rg1);
        r1=(RadioButton)findViewById(R.id.rb1);
        r2=(RadioButton)findViewById(R.id.rb2);
        r3=(RadioButton)findViewById(R.id.rb3);
        r4=(RadioButton)findViewById(R.id.rb4);
        r5=(RadioButton)findViewById(R.id.rb5);
        t1= (TextView)findViewById(R.id.t1);

        rgrp.setOnCheckedChangeListener(
                (RadioGroup.OnCheckedChangeListener)
                (group, checkedId) -> {

            RadioButton nameRadio = (RadioButton)
                    findViewById(checkedId);


            String s1=nameRadio.getText().toString();
            Toast.makeText(MainActivity.this,s1,
             Toast.LENGTH_SHORT).show();

            if(checkedId==r1.getId())
            {
                String s2="Teacher - Dr.Suruchi";
                t1.setText(s2);
            }
            else if(checkedId==r2.getId()) {
                String s3="Teacher - Dr. Surbhi";
                t1.setText(s3);
            }
            else{
                String s4="Teacher - Dr. Meenakshi";
                t1.setText(s4);
            }

        });

    }
}
```

{% endtab %}
{% endtabs %}

{% hint style="success" %}
Download [Q7\_Radio\_Buttons.apk](https://github.com/MJK618/AndroidProgramming/raw/main/APK's/Q7_Radio_Buttons.apk) to Run and Test Locally
{% endhint %}

### Output

![Studio Screenshot Main Activity (1/12)](https://3448212806-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MZCvUCwpAEgH8ewpyoU%2F-MZJHvKl_nBhFIWnPCRp%2F-MZJPE8Ur2NwV9chKd4t%2Fimage.png?alt=media\&token=b1cfe6cf-4543-46be-ac27-1c290d0ca5fc)

![App Screenshot Main Activity (2/12)](https://3448212806-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MZCvUCwpAEgH8ewpyoU%2F-MZJHvKl_nBhFIWnPCRp%2F-MZJR40bpKBfuBHGRU8F%2Fimage.png?alt=media\&token=14a6a5e3-cde7-4ed0-b961-854ceb01b437)

![Studio Screenshot Displaying Toast of Chosen Value (3/12)](https://3448212806-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MZCvUCwpAEgH8ewpyoU%2F-MZJHvKl_nBhFIWnPCRp%2F-MZJPkA4nh_6WbUPneOy%2Fimage.png?alt=media\&token=193acbb6-4842-43c6-a110-db92f1c14b23)

![App Screenshot  Displaying Toast of Chosen Value (4/12)](https://3448212806-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MZCvUCwpAEgH8ewpyoU%2F-MZJHvKl_nBhFIWnPCRp%2F-MZJQmpeqaEYMunp6kK3%2Fimage.png?alt=media\&token=67736fdd-d8de-4094-8557-16c58e15934e)

![Studio Screenshot  Displaying Toast of Chosen Value (5/12)](https://3448212806-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MZCvUCwpAEgH8ewpyoU%2F-MZJHvKl_nBhFIWnPCRp%2F-MZJPoHaTs_E4yKogC5W%2Fimage.png?alt=media\&token=28661a95-8449-448d-a1bc-f9c35afb96ea)

![App Screenshot Displaying Toast of Chosen Value (6/12)](https://3448212806-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MZCvUCwpAEgH8ewpyoU%2F-MZJHvKl_nBhFIWnPCRp%2F-MZJQefwG7zOHsHl1bck%2Fimage.png?alt=media\&token=dc0a2b6c-114d-4fd5-86e3-066e6d5d30e3)

![Studio Screenshot Displaying Toast of Chosen Value (7/12)](https://3448212806-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MZCvUCwpAEgH8ewpyoU%2F-MZJHvKl_nBhFIWnPCRp%2F-MZJPs3Xn0MUwrBRrRAF%2Fimage.png?alt=media\&token=a3e088d4-fd6b-44fd-beb4-897810fda04e)

![App Screenshot Displaying Toast of Chosen Value (8/12)](https://3448212806-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MZCvUCwpAEgH8ewpyoU%2F-MZJHvKl_nBhFIWnPCRp%2F-MZJQXZFX00WFnzw9n_s%2Fimage.png?alt=media\&token=2d83b674-9fac-49e4-bb5a-5b0ecb11dbd2)

![Studio Screenshot Displaying Toast of Chosen Value (9/12)](https://3448212806-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MZCvUCwpAEgH8ewpyoU%2F-MZJHvKl_nBhFIWnPCRp%2F-MZJPvv2nWGjY6BjykDm%2Fimage.png?alt=media\&token=fc7ad6c9-ed55-499e-a74d-6ec4d07aebea)

![App Screenshot Displaying Toast of Chosen Value (10/12)](https://3448212806-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MZCvUCwpAEgH8ewpyoU%2F-MZJHvKl_nBhFIWnPCRp%2F-MZJQRAPzUVqpUQtpxiI%2Fimage.png?alt=media\&token=0731827a-da57-4000-a706-bd8ed6ecff38)

![Studio Screenshot Displaying Toast of Chosen Value (11/12)](https://3448212806-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MZCvUCwpAEgH8ewpyoU%2F-MZJHvKl_nBhFIWnPCRp%2F-MZJQ9E3uHMkzWj62TBo%2Fimage.png?alt=media\&token=a5bf2662-5c71-4b5f-b39e-558ff7acdea4)

![App Screenshot Displaying Toast of Chosen Value (12/12)](https://3448212806-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MZCvUCwpAEgH8ewpyoU%2F-MZJHvKl_nBhFIWnPCRp%2F-MZJQLCHTrRIWq_RQGU9%2Fimage.png?alt=media\&token=61bbb747-05b1-4dc5-b97c-15daee2209c3)
