Practical no 11: android:text="Show Selected" />
MainActivity.xml: </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> Activitymain.java
<LinearLayout
package com.example.hello;
xmlns:android="http://schemas.android.com/
apk/res/android" import android.os.Bundle;
android:layout_width="match_parent" import android.view.View;
android:layout_height="match_parent" import android.widget.Button;
android:orientation="vertical" import android.widget.CheckBox;
android:padding="16dp"> import android.widget.Toast;
<CheckBox import
android:id="@+id/checkbox1" androidx.appcompat.app.AppCompatActivity;
android:layout_width="wrap_content"
android:layout_height="wrap_content" public class MainActivity extends
android:text="Android" /> AppCompatActivity {
<CheckBox @Override
android:id="@+id/checkbox2" protected void onCreate(Bundle
android:layout_width="wrap_content" savedInstanceState) {
android:layout_height="wrap_content" super.onCreate(savedInstanceState);
android:text="ETI" /> setContentView(R.layout.activity_main);
<CheckBox CheckBox checkbox1 =
android:id="@+id/checkbox3" findViewById(R.id.checkbox1);
android:layout_width="wrap_content" CheckBox checkbox2 =
android:layout_height="wrap_content" findViewById(R.id.checkbox2);
android:text="PHP" /> CheckBox checkbox3 =
findViewById(R.id.checkbox3);
<CheckBox CheckBox checkbox4 =
android:id="@+id/checkbox4" findViewById(R.id.checkbox4);
android:layout_width="wrap_content" CheckBox checkbox5 =
android:layout_height="wrap_content" findViewById(R.id.checkbox5);
android:text="PYTHON" /> Button btnShowSelected =
findViewById(R.id.btnShowSelected);
<CheckBox
android:id="@+id/checkbox5" btnShowSelected.setOnClickListener(new
android:layout_width="wrap_content" View.OnClickListener() {
android:layout_height="wrap_content" @Override
android:text="EDE" /> public void onClick(View v) {
StringBuilder selectedOptions = new
<Button StringBuilder("Selected: ");
android:id="@+id/btnShowSelected"
android:layout_width="wrap_content" if (checkbox1.isChecked())
android:layout_height="wrap_content" selectedOptions.append("Android ");
if (checkbox2.isChecked())
selectedOptions.append("ETI");
if (checkbox3.isChecked())
selectedOptions.append("PHP ");
if (checkbox4.isChecked())
selectedOptions.append("PYTHON");
if (checkbox5.isChecked())
selectedOptions.append("EDE");
String result =
selectedOptions.toString().trim();
if (result.equals("Selected:")) {
result = "No options selected";
}
Toast.makeText(MainActivity.this,
result, Toast.LENGTH_SHORT).show();
}
});
}
}
Output: