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

Practical 11

The document provides a code example for an Android application that displays five checkboxes for programming languages and a button to show the selected options. The layout is defined in XML, and the functionality is implemented in Java, where a toast message displays the selected checkboxes when the button is clicked. If no checkboxes are selected, a message indicating that no options were selected is shown.
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)
10 views3 pages

Practical 11

The document provides a code example for an Android application that displays five checkboxes for programming languages and a button to show the selected options. The layout is defined in XML, and the functionality is implemented in Java, where a toast message displays the selected checkboxes when the button is clicked. If no checkboxes are selected, a message indicating that no options were selected is shown.
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 :-

Que. Write a program to show five checkboxes and toast selected checkbox

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/img" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="20dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set Your Programing Language"
android:textColor="#FFFFFF"
android:textSize="18sp"
android:textStyle="bold"
android:paddingBottom="10dp" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C"
android:textColor="#FFFFFF" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C++"
android:textColor="#FFFFFF" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Java"
android:textColor="#FFFFFF" />
<CheckBox
android:id="@+id/checkBox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Python"
android:textColor="#FFFFFF" />
<CheckBox
android:id="@+id/checkBox5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Js"
android:textColor="#FFFFFF" />
<Button
android:id="@+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show selected"
android:layout_marginTop="20dp" />
</LinearLayout>
</RelativeLayout>
MainActivity.java
package com.example.practical011;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
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 checkBox1, checkBox2, checkBox3, checkBox4, checkBox5;
Button btnSubmit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkBox1 = findViewById(R.id.checkBox1);
checkBox2 = findViewById(R.id.checkBox2);
checkBox3 = findViewById(R.id.checkBox3);
checkBox4 = findViewById(R.id.checkBox4);
checkBox5 = findViewById(R.id.checkBox5);
btnSubmit = findViewById(R.id.btnSubmit);

btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String selected = "Selected: ";
if (checkBox1.isChecked()) selected += checkBox1.getText() + ", ";
if (checkBox2.isChecked()) selected += checkBox2.getText() + ", ";
if (checkBox3.isChecked()) selected += checkBox3.getText() + ", ";
if (checkBox4.isChecked()) selected += checkBox4.getText() + ", ";
if (checkBox5.isChecked()) selected += checkBox5.getText() + ", ";
if (selected.equals("Selected: ")) {
selected = "No options selected";
}
Toast.makeText(getApplicationContext(), selected, Toast.LENGTH_LONG).show();
}
});
}
}

You might also like