XML File
<Button
<?xml version="1.0" encoding="utf-8"?>
android:id="@+id/btnSubmit"
<LinearLayout
android:layout_width="wrap_content"
xmlns:android="http://schemas.android.com/
android:layout_height="wrap_content"
apk/res/android"
android:text="Show Selected"
android:layout_width="match_parent"
android:layout_marginTop="20dp"/>
android:layout_height="match_parent"
android:orientation="vertical"
</LinearLayout>
android:padding="20dp">
Main Activity File
<TextView
package com.example.checkbox;
android:layout_width="wrap_content"
android:layout_height="wrap_content" import android.os.Bundle;
android:text="Select Your Favourite Car" import android.view.View;
android:textSize="18sp" import android.widget.Button;
import android.widget.CheckBox;
android:textStyle="bold" import android.widget.Toast;
android:paddingBottom="10dp" /> import androidx.appcompat.app.AppCompatActivity;
<CheckBox public class MainActivity extends AppCompatActivity {
android:id="@+id/checkBox1"
CheckBox checkBox1, checkBox2, checkBox3,
android:layout_width="wrap_content" checkBox4, checkBox5;
android:layout_height="wrap_content" Button btnSubmit;
android:text="Supra" />
@Override
protected void onCreate(Bundle savedInstanceState) {
<CheckBox super.onCreate(savedInstanceState);
android:id="@+id/checkBox2" setContentView(R.layout.activity_main);
android:layout_width="wrap_content"
android:layout_height="wrap_content" checkBox1 = findViewById(R.id.checkBox1);
checkBox2 = findViewById(R.id.checkBox2);
android:text="G-Wagon" />
checkBox3 = findViewById(R.id.checkBox3);
checkBox4 = findViewById(R.id.checkBox4);
<CheckBox checkBox5 = findViewById(R.id.checkBox5);
android:id="@+id/checkBox3" btnSubmit = findViewById(R.id.btnSubmit);
android:layout_width="wrap_content"
btnSubmit.setOnClickListener(new
android:layout_height="wrap_content" View.OnClickListener() {
android:text="Ferrari" /> @Override
public void onClick(View v) {
<CheckBox String selected = "Selected: ";
android:id="@+id/checkBox4"
if (checkBox1.isChecked()) selected +=
android:layout_width="wrap_content" checkBox1.getText() + ", ";
android:layout_height="wrap_content" if (checkBox2.isChecked()) selected +=
android:text="Range Rover" /> checkBox2.getText() + ", ";
if (checkBox3.isChecked()) selected +=
checkBox3.getText() + ", ";
<CheckBox if (checkBox4.isChecked()) selected +=
android:id="@+id/checkBox5" checkBox4.getText() + ", ";
android:layout_width="wrap_content" if (checkBox5.isChecked()) selected +=
android:layout_height="wrap_content" checkBox5.getText() + ", ";
android:text="BMW" />
if (selected.equals("Selected: ")) {
selected = "No options selected";
}
Toast.makeText(getApplicationContext(),
selected, Toast.LENGTH_LONG).show();
}
});
}
}