14 Aneesh Mad 11
14 Aneesh Mad 11
X.Exercise
1. Write a program to Show the checkboxes and toast selected checkboxes
Ans -:
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="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:layout_editor_absoluteX="195dp"
tools:layout_editor_absoluteY="101dp">
<CheckBox
android:id="@+id/c1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Aneesh"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="32dp" />
<CheckBox
android:id="@+id/c2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chess"
tools:layout_editor_absoluteX="254dp"
tools:layout_editor_absoluteY="32dp" />
<CheckBox
android:id="@+id/c3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Singing"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="112dp" />
<CheckBox
android:id="@+id/c4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Developer"
tools:layout_editor_absoluteX="254dp"
tools:layout_editor_absoluteY="112dp" />
<CheckBox
android:id="@+id/c5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TYCO"
tools:layout_editor_absoluteX="145dp"
tools:layout_editor_absoluteY="200dp" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Click" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Main_Activity.java
package com.example.practical11a;
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cb1 = (CheckBox) findViewById(R.id.c1);
cb2 = (CheckBox) findViewById(R.id.c2);
cb3 = (CheckBox) findViewById(R.id.c3);
cb4 = (CheckBox) findViewById(R.id.c4);
cb5 = (CheckBox) findViewById(R.id.c5);
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (cb1.isChecked())
{
Toast.makeText(getApplicationContext(),"You Selected
Aneesh",Toast.LENGTH_LONG).show();
}
if(cb2.isChecked())
{
Toast.makeText(getApplicationContext(),"You Selected
Chess",Toast.LENGTH_LONG).show();
}
if(cb3.isChecked())
{
Toast.makeText(getApplicationContext(),"You Selected
Singing",Toast.LENGTH_LONG).show();
}
if(cb4.isChecked())
{
Toast.makeText(getApplicationContext(),"You Selected
Developer",Toast.LENGTH_LONG).show();
}
if(cb5.isChecked())
{
Toast.makeText(getApplicationContext(),"You Selected
TYCO",Toast.LENGTH_LONG).show();
}
}
});
}
}
Output