0% found this document useful (0 votes)
15 views10 pages

RadioButton Event Handling in Android

The document contains code for two practical exercises demonstrating event handling in Android applications. The first exercise involves handling RadioButton selections to display user input, while the second exercise showcases CheckBox and ToggleButton interactions to capture user hobbies and student status. Both exercises include XML layout files and corresponding Java activity classes.

Uploaded by

pinali patel
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)
15 views10 pages

RadioButton Event Handling in Android

The document contains code for two practical exercises demonstrating event handling in Android applications. The first exercise involves handling RadioButton selections to display user input, while the second exercise showcases CheckBox and ToggleButton interactions to capture user hobbies and student status. Both exercises include XML layout files and corresponding Java activity classes.

Uploaded by

pinali patel
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

Enrollment no: 236370307099

Practical 8.1
Aim: Develop code to demonstrate Event handling of RadioButton selection.
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:background="#D6E1EA"
android:orientation="vertical"
tools:context=".MainActivity">

<[Link]
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#020C3E">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="My Application"
android:textColor="@color/white"
android:textSize="30dp"
android:textStyle="bold" />
</[Link]>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:background="#ACC0CF"
android:orientation="vertical"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Application"
android:textColor="#020C3E"
android:textSize="25dp"
android:textStyle="bold" />

<EditText
Enrollment no: 236370307099
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="20dp"
android:gravity="center_vertical"
android:hint="Enter Name"
android:paddingLeft="10dp"
android:id="@+id/et_name"
android:textSize="20dp" />

<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="20dp"
android:gravity="center_vertical"
android:hint="Enter Number"
android:paddingLeft="10dp"
android:id="@+id/et_number"
android:textSize="20dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="Select Branch :"
android:textColor="#020C3E"
android:textSize="20dp"
android:textStyle="bold" />

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

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Computer"
android:textSize="20dp" />

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Civil"
android:textSize="20dp" />

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Ele."
android:textSize="20dp" />
</RadioGroup>
Enrollment no: 236370307099

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center"
android:orientation="horizontal">

<Button
android:id="@+id/btn_submit"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="Submit"
android:textColor="@color/white"
android:textSize="20dp" />

<Button
android:id="@+id/btn_close"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="Cancle"
android:textColor="@color/white"
android:textSize="20dp" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_marginTop="30dp"
android:orientation="vertical"
android:layout_height="wrap_content">

<TextView
android:id="@+id/txt_name"
android:textSize="20dp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/txt_number"
android:layout_marginTop="10dp"
android:textSize="20dp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
Enrollment no: 236370307099
android:id="@+id/txt_branch"
android:layout_marginTop="10dp"
android:textSize="20dp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

</LinearLayout>

</LinearLayout>
</LinearLayout>

Main [Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {

private RadioButton genderradioButton;


private RadioGroup radioGroup;
private TextView txt_name;
private TextView txt_number;
private TextView txt_branch;
private EditText et_number;
private EditText et_name;
private Button btn_submit;
private Button btn_close;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

radioGroup = (RadioGroup) findViewById([Link]);


btn_submit = (Button) findViewById([Link].btn_submit);
et_name = (EditText) findViewById([Link].et_name);
et_number = (EditText) findViewById([Link].et_number);
txt_branch = (TextView) findViewById([Link].txt_branch);
txt_name = (TextView) findViewById([Link].txt_name);
txt_number = (TextView) findViewById([Link].txt_number);

btn_submit.setOnClickListener(new [Link]() {
@Override
public void onClick(View view) {
Enrollment no: 236370307099
txt_name.setText("Name :"+et_name.getText().toString());
txt_number.setText("Number :"+et_number.getText().toString());

int selectedId = [Link]();


genderradioButton = (RadioButton) findViewById(selectedId);
if (selectedId == -1) {
[Link]([Link], "Branch is not selected", Toast.LENGTH_SHORT).show();
} else {
txt_branch.setText("Selected Branch :"+[Link]().toString());
}
}
});

}
Output:
Enrollment no: 236370307099
Practical 8.2
Aim: Develop code to demonstrate Event handling of CheckBox and ToggleButton.
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:background="#D6E1EA"
android:orientation="vertical"
tools:context=".MainActivity">

<[Link]
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#020C3E">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="My Application"
android:textColor="@color/white"
android:textSize="30dp"
android:textStyle="bold" />
</[Link]>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:background="#ACC0CF"
android:orientation="vertical"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Application"
android:textColor="#020C3E"
android:textSize="25dp"
android:textStyle="bold" />

<TextView
Enrollment no: 236370307099
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Choose your hobbies:"
android:textSize="25sp"
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:layout_marginTop="16dp"
android:text="Painting"
android:textSize="18sp" />

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

<CheckBox
android:id="@+id/checkBox3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Singing"
android:textSize="18sp"
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:layout_marginTop="16dp"
android:text="Cooking"
android:textSize="18sp"
app:layout_constraintTop_toBottomOf="@+id/checkBox"
tools:layout_editor_absoluteX="386dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="Are you Student ?"
android:textColor="#020C3E"
android:textSize="20dp"
Enrollment no: 236370307099
android:textStyle="bold" />

<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:textOn="Yes"
android:textOff="No"
android:layout_centerVertical="true" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center"
android:orientation="horizontal">

<Button
android:id="@+id/btn_submit"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="Submit"
android:textColor="@color/white"
android:textSize="20dp" />

<Button
android:id="@+id/btn_close"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="Cancle"
android:textColor="@color/white"
android:textSize="20dp" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:orientation="vertical">

<TextView
android:id="@+id/txt_hobby"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
Enrollment no: 236370307099
android:textStyle="bold" />

<TextView
android:id="@+id/txt_toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="20dp"
android:textStyle="bold" />

</LinearLayout>

</LinearLayout>

</LinearLayout>
</LinearLayout>
Main [Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {


private TextView txt_hobby;
private TextView txt_toggle;
private Button btn_submit;
private ToggleButton togglebutton;
CheckBox ch, ch1, ch2, ch3;
String msg="";

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
btn_submit = (Button) findViewById([Link].btn_submit);
txt_hobby = (TextView) findViewById([Link].txt_hobby);
txt_toggle = (TextView) findViewById([Link].txt_toggle);
togglebutton = (ToggleButton)findViewById([Link]);

ch=(CheckBox)findViewById([Link]);
ch1=(CheckBox)findViewById([Link].checkBox2);
ch2=(CheckBox)findViewById([Link].checkBox3);
ch3=(CheckBox)findViewById([Link].checkBox4);

btn_submit.setOnClickListener(new [Link]() {
@Override
public void onClick(View view) {
if ([Link]()) {
txt_toggle.setText("User is Student");
Enrollment no: 236370307099
}
else {
txt_toggle.setText("User is not Student");
}

if([Link]())
msg = msg + " Painting ";
if([Link]())
msg = msg + " Reading ";
if([Link]())
msg = msg + " Singing ";
if([Link]())
msg = msg + " Cooking ";

txt_hobby.setText(msg+ " are selected");


}
});
}
}
Output:

You might also like