Sei sulla pagina 1di 2

SHRUSTI TIWARI PRACTICAL NO.

11 210431

1. Write a Program to show five checkboxes and toast selected checkbox.


CODE:

XML:

<?xml version="1.0" encoding="utf-8"?> android:layout_width="wrap_content"


<LinearLayout android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/re android:text="checkBox3"
s/android" android:textSize="30dp"
xmlns:app="http://schemas.android.com/apk/res- />
auto" <CheckBox
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/c4"
android:layout_width="match_parent" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical" android:text="checkBox4"
tools:context=".MainActivity"> android:textSize="30dp"
/>
<CheckBox <CheckBox
android:id="@+id/c1" android:id="@+id/c5"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="checkBox1" android:text="checkBox5"
android:textSize="30dp" android:textSize="30dp"
android:checked="true" />
/> <Button
android:id="@+id/b1"
<CheckBox android:layout_width="wrap_content"
android:id="@+id/c2" android:layout_height="wrap_content"
android:layout_width="wrap_content" android:text="Get Details"
android:layout_height="wrap_content" android:layout_gravity="center"
android:text="checkBox2" android:textSize="30sp"
android:textSize="30dp" android:layout_marginTop="30dp"
/> />
<CheckBox </LinearLayout>
android:id="@+id/c3"

JAVA:

package com.example.exp11part2; final CheckBox c3 =


import (CheckBox)findViewById(R.id.c3);
androidx.appcompat.app.AppCompatActivity; final CheckBox c4 =
import android.os.Bundle; (CheckBox)findViewById(R.id.c4);
import android.view.View; final CheckBox c5 =
import android.widget.Button; (CheckBox)findViewById(R.id.c5);
import android.widget.CheckBox; Button b1 = (Button) findViewById(R.id.b1);
import android.widget.Toast; b1.setOnClickListener(new
public class MainActivity extends View.OnClickListener() {
AppCompatActivity { @Override
String str = ""; public void onClick(View v) {
@Override if(c1.isChecked())
protected void onCreate(Bundle str += c1.getText().toString()+"\n";
savedInstanceState) { if(c2.isChecked())
super.onCreate(savedInstanceState); str += c2.getText().toString()+"\n";
setContentView(R.layout.activity_main); if(c3.isChecked())
final CheckBox c1 = str += c3.getText().toString()+"\n";
(CheckBox)findViewById(R.id.c1); if(c4.isChecked())
final CheckBox c2 = str += c4.getText().toString()+"\n";
(CheckBox)findViewById(R.id.c2); if(c5.isChecked())
SHRUSTI TIWARI PRACTICAL NO. 11 210431

str += c5.getText().toString()+"\n";
Toast.makeText(MainActivity.this, str,
Toast.LENGTH_SHORT).show();
} }); }}

Potrebbero piacerti anche