📝
Android Programming
  • Introduction
  • Contents
  • Android
    • Q1
    • Q2
    • Q3
    • Q4
    • Q5
    • Q6
    • Q7
    • Q8
    • Q9
    • Q10
    • Q11
    • Q12
    • End
Powered by GitBook
On this page
  • 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.
  • Code
  • Output
  1. Android

Q7

Question 7

PreviousQ6NextQ8

Last updated 4 years ago

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.

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

Code

<?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>
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);
            }

        });

    }
}

Output

Download to Run and Test Locally

Q7_Radio_Buttons.apk
Browse Source Code
Studio Screenshot Main Activity (1/12)
App Screenshot Main Activity (2/12)
Studio Screenshot Displaying Toast of Chosen Value (3/12)
App Screenshot Displaying Toast of Chosen Value (4/12)
Studio Screenshot Displaying Toast of Chosen Value (5/12)
App Screenshot Displaying Toast of Chosen Value (6/12)
Studio Screenshot Displaying Toast of Chosen Value (7/12)
App Screenshot Displaying Toast of Chosen Value (8/12)
Studio Screenshot Displaying Toast of Chosen Value (9/12)
App Screenshot Displaying Toast of Chosen Value (10/12)
Studio Screenshot Displaying Toast of Chosen Value (11/12)
App Screenshot Displaying Toast of Chosen Value (12/12)