0% found this document useful (0 votes)
11 views9 pages

MADA Pr-8

The document provides a practical guide on implementing event handling for CheckBox and RadioButton in Android applications. It explains the attributes and implementation steps for both UI components, highlighting their differences and usage scenarios. The document includes XML layout examples and Java code for handling user interactions with these controls.

Uploaded by

np027923
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views9 pages

MADA Pr-8

The document provides a practical guide on implementing event handling for CheckBox and RadioButton in Android applications. It explains the attributes and implementation steps for both UI components, highlighting their differences and usage scenarios. The document includes XML layout examples and Java code for handling user interactions with these controls.

Uploaded by

np027923
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Practical-08

Aim: - Develop code to demonstrate Event handling of CheckBox and


RadioButton selection.
Solution:-

 Introduction to Android RadioButton


 Beginning with Android RadioButton, A RadioButton is a button that has
two states that are either check or uncheck. This means if we click on a
RadioButton it gets into checked state and by default, it comes in
Unchecked State.
 In the Android Radio button, one thing to be noted is, once it gets into the
checked state we cannot undo it. Generally, we use them in our application
to let the users select one option from a set of options. Radio Button is one
of the important Android UI controls, as it provides an interactive User
Interface for the users.

Attributes of Android RadioButton


Some of the attributes that we can use to customize the RadioButton in Android
are as follows:

1. android: id – It set a unique identity.


2. android: checked – It specifies the current state of the button.
3. android: gravity – It sets the alignment of the text.
4. android: text – It sets the text for the Radio Button.
5. android:textColor – It sets the color of the text.
6. android:textSize – It sets the size of the text.
7. android:textStyle – It sets the style of the text, like – bold, italics.
8. android: background – It sets the color for the background of the button.
9. android: padding – It sets the padding from left, right bottom or the top.
10. android:onClick – It invokes the method onClick() when the button is
clicked.
11. android: visibility – It controls the visibility of the Radio Button.
Implementation of Android RadioButton
and Android RadioGroup
Step 1. First of all, we will create a new project and name it. After this, we will
define the layout in activity_main.xml file:
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 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=".MainActivityOnClick2"
android:orientation="vertical">

<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/activity_main_answers"
android:checkedButton="@id/activity_main_onclicklistener_answer2">

<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/answer1"
android:id="@+id/activity_main_onclicklistener_answer1"/>

<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/answer2"
android:id="@+id/activity_main_onclicklistener_answer2" />

<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/answer3"
android:id="@+id/activity_main_onclicklistener_answer3" />
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/answer4"
android:id="@+id/activity_main_onclicklistener_answer4" />

</RadioGroup>

</LinearLayout>

Step 2. Now the thing we will do is, writing the following code
in MainActivity.java file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MainActivityOnClick2"
android:orientation="vertical">

<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/activity_main_answers"
android:checkedButton="@id/activity_main_onclicklistener_answer2">

<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/answer1"
android:id="@+id/activity_main_onclicklistener_answer1"/>

<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/answer2"
android:id="@+id/activity_main_onclicklistener_answer2" />
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/answer3"
android:id="@+id/activity_main_onclicklistener_answer3" />

<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/answer4"
android:id="@+id/activity_main_onclicklistener_answer4" />

</RadioGroup>

</LinearLayout>

Step 3. Now we will run this:


 CheckBox in Android
 CheckBox is also a two-state button, it can have either checked state
or unchecked state. The major difference between Android CheckBox
and Android RadioButton is that checkboxes can be unchecked
manually.

 Check means ON or True


 Uncheck means OFF or False
 Android CheckBox is mostly useful when there are multiple options &
the users are allowed to choose all the options that are applicable.

Attributes of Android CheckBox


We can also set attributes on CheckBox to customize it in our way, using the
following attributes:
1. android: id – It set a unique identity
2. android: checked – It specifies the current state of the CheckBox.
3. android: gravity – It sets the alignment of the CheckBox.
4. android: text – It sets the text for the Radio CheckBox.
5. android:textColor – It sets the color of the text in the CheckBox.
6. android:textSize – It sets the size of the text in CheckBox.
7. android:textStyle – It sets the style of the text, like – bold, italics.
8. android: background – It sets the color for the background of the
CheckBox.
9. android: padding – It sets the padding from left, right bottom or the top.
10.android:onClick – It invokes the method onClick() when the button is
clicked.
11. android: visibility – It controls the visibility of the CheckBox.

Implementation of CheckBox in Android


Step 1: To implement it as always, we will first create a new project. Now
we will define the CheckBox in activity_main.xml file as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical">

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="30dp"
android:text="Choose your hobbies:"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<CheckBox
android:id="@+id/checkBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Painting"
android:layout_marginTop="16dp"
android:textSize="16sp" />

<CheckBox
android:id="@+id/checkBox2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Reading"
android:layout_marginTop="16dp"
android:textSize="16sp" />

<CheckBox
android:id="@+id/checkBox3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Singing"
android:textSize="16sp"
app:layout_constraintTop_toTopOf="@+id/textView"
tools:layout_editor_absoluteX="382dp" />

<CheckBox
android:id="@+id/checkBox4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Cooking"
android:layout_marginTop="16dp"
android:textSize="16sp"
app:layout_constraintTop_toBottomOf="@+id/checkBox"
tools:layout_editor_absoluteX="386dp" />

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:onClick="Check"
android:text="submit" />
</LinearLayout>
Step 2: Now we will write the following code in MainActivity.java file:
package com.gfg.checkbox;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {


CheckBox ch,ch1,ch2,ch3;

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

ch=findViewById(R.id.checkBox);
ch1=findViewById(R.id.checkBox2);
ch2=findViewById(R.id.checkBox3);
ch3=findViewById(R.id.checkBox4);
}

public void Check(View v){


String msg="Selected Items are";
// Checking the selection
if(ch.isChecked())
msg=msg+" [Painting]";
if(ch1.isChecked())
msg=msg+", [Reading]";
if(ch2.isChecked())
msg=msg+", [Singing]";
if(ch3.isChecked())
msg=msg+", [Cooking]";

// Executing the Toast


Toast.makeText(this,msg,Toast.LENGTH_LONG).show();
// Clearing all the selection
ch.setChecked(false);
ch1.setChecked(false);
ch2.setChecked(false);
ch3.setChecked(false);
}
}

Output:-

You might also like