DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Experiment 2.2
Student Name: Armaan Verma UID: 20BCS3226
Branch: CSE Section/Group: 906/A
Semester: 6th Date of Performance: 22-03-2023
Subject Name: MAD Lab Subject Code: 20CSP356
• Aim: Create an Android App using various controls such TexEdit, CheckBox, RadioButton,
RadioGroup, etc.
• Objective: To create a menu using Check boxes and Radio buttons and show toast
message of selected items.
• Tools used:
1. Android Studio.
2. Eclipse IDE.
• Script and Output:
1. First create a new Android Application. This will create an XML file “
activity_main.xml” and a Java File “[Link]”.
2. Open the “activity_main.xml” file and add the following widgets in a
RelativeLayout:
• A TextView to display the question message 2 CheckBoxes to hold answers
each.
• A RadioGroup to hold the option Radio Buttons which are the possible answers
• A RadioButtons to hold an answer each.
• A Submit and a Clear button to store the response.
Also, Assign the ID to each of the components along with other attributes as shown in the given image
and the code below. The assigned ID on a component helps that component to be easily found and
used in the Java files.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
3. Now, after the UI, this step will create the Backend of Application. For this, open the
“[Link]” file and instantiate the components made in the XML file (RadioGroup,
TextView, Clear, and Submit Button) using findViewById() method. This method binds the created
object to the UI Components with the help of the assigned ID.
4. Now, after the UI, this step will create the Backend of Application. For this, open the
“[Link]” file and instantiate the components made in the XML file (RadioGroup,
TextView, Clear, and Submit Button) using findViewById() method. This method binds the created
object to the UI Components with the help of the assigned ID.
This step involves setting up the operations on the RadioGroup, RadioButtons, and the Submit and
Clear Buttons.
These operations are as follows:
• Unset all the Radio Buttons initially as the default value. This is done by the following command:
[Link]();
• Add the Listener on the RadioGroup. This will help to know whenever the user clicks on any Radio
Button, and the further operation will be performed. The listener can be added as follows:
[Link](new [Link](){}
• Define the operations to be done when a radio button is clicked. This involves getting the specific
radio button that has been clicked, using its id. Then this radio button gets set and the rest of the
radio button is reset.
• Add the listener on Submit button and clear button. This will be used to check when the user clicks
on the button. This is done as follows:
[Link](new [Link]() {} [Link](new
[Link]() {}
In the Submit Button Listener, set the operations to be performed. This involves displaying the
marked answer in the form of Toast.
5. Now run the app and operate as follows:
• When the app is opened, it displays a question with 4 answers and a clear and submit button.
• When any answer is clicked, that radio button gets set.
• Clicking on any other radio button sets that one and resets the others.
• Clicking on Submit button displays the currently marked answer as a Toast.
• Clicking on Clear button resets all the radio buttons to their default state.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
CODE:
[Link]
package [Link].unit2;
import [Link];import
[Link]; import [Link]; import
[Link]; import [Link];
import [Link]; import
[Link]; import [Link];
public class MainActivity extends AppCompatActivity {CheckBox
c1,c2;
EditText t;
RadioButton r1,r2;
Button bt;
String txt="";
@Override protected void onCreate(Bundle
savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
t = (EditText) findViewById([Link]); c1 =
(CheckBox) findViewById([Link]);
c2 = (CheckBox) findViewById([Link].checkBox2);
r1 = (RadioButton) findViewById([Link]);r2 =
(RadioButton) findViewById([Link].radioButton2);
bt = (Button) findViewById([Link]);
[Link](new [Link]() {
@Override public void
onClick(View view) {
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
String t1 = [Link]().toString(); String
v1 = [Link]().toString();String v2 =
[Link]().toString();String v3 =
[Link]().toString();
String v4 = [Link]().toString();txt =
t1+" selected:";
if([Link]() )
{txt = txt+" "+v1;} if([Link]())
{txt
= txt+" "+v2;} if([Link]()) {txt
= txt+" "+v3;} if([Link]()) {txt
= txt+" "+v4;}
Toast t = [Link](getApplicationContext(),txt,Toast.LENGTH_LONG);
[Link]();
}
});
}}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link] android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" android:gravity="center"
tools:context=".MainActivity">
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
<EditText
android:id="@+id/txt" android:layout_width="match_parent"
android:layout_height="wrap_content" android:ems="10"
android:inputType="textPersonName" android:hint="Your Name"
android:textAlignment="center" android:layout_marginBottom="20dp"
android:layout_marginEnd="30dp"
android:layout_marginStart="30dp"/>
<CheckBox android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cricket" />
<CheckBox android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Football" />
<RadioGroup android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton android:id="@+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Chess"
/>
<RadioButton android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Archery"
/>
</RadioGroup>
<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
android:layout_height="wrap_content"
android:text="Submit"
/>
</LinearLayout>
OUTPUT: