0% found this document useful (0 votes)
586 views2 pages

Practical No 11

This document contains code for an Android application that allows a user to select an Indian city from a list of checkboxes and see a toast message with the selected city name. The activity_main.xml layout contains checkboxes for the cities of Pune, Mumbai, Nagpur, Dhule, and Nashik. The MainActivity class finds the checkbox views and adds the same OnCheckedChangeListener to each one. When a checkbox is checked or unchecked, the listener gets the city name from the checkbox text and displays it in a toast if checked. This allows the user to select one of the cities and see the name of the "Cleanest City".

Uploaded by

atharvabutte03
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)
586 views2 pages

Practical No 11

This document contains code for an Android application that allows a user to select an Indian city from a list of checkboxes and see a toast message with the selected city name. The activity_main.xml layout contains checkboxes for the cities of Pune, Mumbai, Nagpur, Dhule, and Nashik. The MainActivity class finds the checkbox views and adds the same OnCheckedChangeListener to each one. When a checkbox is checked or unchecked, the listener gets the city name from the checkbox text and displays it in a toast if checked. This allows the user to select one of the cities and see the name of the "Cleanest City".

Uploaded by

atharvabutte03
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/ 2

Practical No 11

Q1.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="306dp"
android:layout_height="442dp"
android:layout_marginStart="52dp"
android:layout_marginTop="92dp"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="30dp"
android:text="Select city"
android:textSize="20dp" />

<CheckBox
android:id="@+id/ch1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Pune" />

<CheckBox
android:id="@+id/ch2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Mumbai" />

<CheckBox
android:id="@+id/ch3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Nagpur" />

<CheckBox
android:id="@+id/ch4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dhule" />

<CheckBox
android:id="@+id/ch5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Nashik" />
</LinearLayout>

<EditText
android:layout_width="162dp"
android:layout_height="48dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Atharva Butte"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.practical11;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.*;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CheckBox ch1 = findViewById(R.id.ch1);
CheckBox ch2 = findViewById(R.id.ch2);
CheckBox ch3 = findViewById(R.id.ch3);
CheckBox ch4 = findViewById(R.id.ch4);
CheckBox ch5 = findViewById(R.id.ch5);

CompoundButton.OnCheckedChangeListener ch_listner = new


CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
int id = buttonView.getId();
String city = "";
if(id == R.id.ch1 && isChecked)
city = buttonView.getText().toString();
else if (id == R.id.ch2 && isChecked) {
city = buttonView.getText().toString();
} else if (id == R.id.ch3 && isChecked) {
city = buttonView.getText().toString();
}else if (id == R.id.ch4 && isChecked) {
city = buttonView.getText().toString();
} else if (id == R.id.ch5 && isChecked) {
city = buttonView.getText().toString();
}else {

}
if (isChecked)
Toast.makeText(MainActivity.this,

"Cleanest City is "+city,Toast.LENGTH_LONG).show();


}
};

ch1.setOnCheckedChangeListener(ch_listner);
ch2.setOnCheckedChangeListener(ch_listner);
ch3.setOnCheckedChangeListener(ch_listner);
ch4.setOnCheckedChangeListener(ch_listner);
ch5.setOnCheckedChangeListener(ch_listner);
}
}

You might also like