0% found this document useful (0 votes)
6 views3 pages

PR 11

The document contains XML and Java code for an Android application that utilizes checkboxes and a button. The XML layout defines multiple checkboxes and a button within a linear layout, while the Java code handles the button click event to display selected checkbox items using a Toast message. The application prompts users to select at least one checkbox before displaying the selected subjects.

Uploaded by

Samiksha Bhosale
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)
6 views3 pages

PR 11

The document contains XML and Java code for an Android application that utilizes checkboxes and a button. The XML layout defines multiple checkboxes and a button within a linear layout, while the Java code handles the button click event to display selected checkbox items using a Toast message. The application prompts users to select at least one checkbox before displaying the selected subjects.

Uploaded by

Samiksha Bhosale
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/ 3

Practical 11

Q1.
XML Code:
<?xml version="1.0" encoding="utf-8"?> <CheckBox
<LinearLayout android:id="@+id/cb2"
xmlns:android="http://schemas.android.com/apk android:layout_width="match_parent"
/res/android" android:layout_height="wrap_content"
android:text="@string/pwp"/>
xmlns:app="http://schemas.android.com/apk/res
-auto" <CheckBox
android:id="@+id/cb3"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_height="wrap_content" android:text="@string/eti"/>
android:layout_marginTop="150dp"
android:orientation="vertical" <CheckBox
android:layout_gravity="center_horizontal" android:id="@+id/cb4"
tools:context=".MainActivity"> android:layout_width="match_parent"
<TextView android:layout_height="wrap_content"
android:id="@+id/tv1" android:text="@string/nis"/>
android:layout_width="match_parent"
android:layout_height="wrap_content" <CheckBox
android:text="@string/subjects" android:id="@+id/cb5"
android:gravity="center" android:layout_width="match_parent"
android:textSize="22sp" android:layout_height="wrap_content"
android:textColor="#FA1605" android:text="@string/mgt"/>
android:layout_marginBottom="20dp"/>
<CheckBox <Button
android:id="@+id/cb1" android:id="@+id/btn1"
android:layout_width="match_parent" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/mad"/> android:text="@string/submit"
android:gravity="center"
android:layout_marginTop="10dp"/>
</LinearLayout>
Practical 11

JAVA Code:
package com.example.checkbox;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

Button b1;
CheckBox c1,c2,c3,c4,c5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.btn1);
c1=(CheckBox)findViewById(R.id.cb1);
c2=(CheckBox)findViewById(R.id.cb2);
c3=(CheckBox)findViewById(R.id.cb3);
c4=(CheckBox)findViewById(R.id.cb4);
c5=(CheckBox)findViewById(R.id.cb5);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringBuffer s=new StringBuffer();
if(c1.isChecked()||c2.isChecked()||c3.isChecked()||c4.isChecked()||c5.isChecked()) {
s.append("Selected Subjects are :");
if (c1.isChecked()) {
s.append("\n" + c1.getText());
}
if (c2.isChecked()) {
s.append("\n" + c2.getText());
}
if (c3.isChecked()) {
s.append("\n" + c3.getText());
}
if (c4.isChecked()) {
s.append("\n" + c4.getText());
}
if (c5.isChecked()) {
s.append("\n" + c5.getText());
Practical 11

}
}
else
{
s.append("Select at least One");
}
Toast.makeText(getApplicationContext(),s.toString(),Toast.LENGTH_SHORT).show();
}
});
}
}

Output:

You might also like