Android CheckBox with Examples | How to use CheckBox in Android

Android CheckBox – Introduction

Android CheckBox –

Create activity_check_box File in XML

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout
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”
android:layout_marginTop=”20dp”
android:layout_marginLeft=”10dp”
android:orientation=”vertical”
tools:context=”.checkBoxActivity”>

<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_gravity=”center”
android:layout_marginTop=”20dp”
android:text=”This is the Example of Multiple Choice Quiz”
android:textColor=”@android:color/holo_blue_dark”
android:textSize=”15sp”
android:textStyle=”bold” />

<TextView
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_marginTop=”20dp”
android:text=”Your Question Here.”
android:textSize=”20dp”/>

<CheckBox
android:id=”@+id/checkAns1″
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:text=”Answer Number 1″/>

<CheckBox
android:id=”@+id/checkAns2″
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:text=”Answer Number 2″/>

<CheckBox
android:id=”@+id/checkAns3″
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:text=”Answer Number 3″/>

<CheckBox
android:id=”@+id/checkAns4″
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:text=”Answer Number 4″/>

<Button
android:id=”@+id/quizSubmit”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Submit”
android:layout_marginTop=”20dp”
android:layout_gravity=”center”
android:textColor=”@color/purple_200″
app:backgroundTint=”@color/teal_700″ />

<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_gravity=”center”
android:layout_marginTop=”50dp”
android:text=”This is the Second Example of CheckBox”
android:textColor=”@android:color/holo_blue_dark”
android:textSize=”20sp”
android:textStyle=”bold” />

<LinearLayout
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:orientation=”horizontal”
android:layout_gravity=”center”
android:gravity=”center”
android:layout_marginTop=”20dp”>

<CheckBox
android:id=”@+id/checkBox1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Check Box 1″/>

<CheckBox
android:id=”@+id/checkBox2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Check Box 2″/>

<CheckBox
android:id=”@+id/checkBox3″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Check Box 3″/>

</LinearLayout>

<Button
android:id=”@+id/buttonOutput”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Submit”
android:layout_marginTop=”20dp”
android:layout_gravity=”center”
android:textColor=”@color/purple_200″
android:backgroundTint=”@color/teal_700″ />

</LinearLayout>

Create checkBoxActivity File in Java

package com.eiheducation.demo;

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;

public class checkBoxActivity extends AppCompatActivity {

CheckBox checkAns1, checkAns2, checkAns3, checkAns4, checkBox1, checkBox2, checkBox3; // Check Box Objects
Button quizSubmit, buttonOutput; // Button Objects

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check_box);

// We need to find our elements by IDs

checkAns1 = findViewById(R.id.checkAns1);
checkAns2 = findViewById(R.id.checkAns2);
checkAns3 = findViewById(R.id.checkAns3);
checkAns4 = findViewById(R.id.checkAns4);
checkBox1 = findViewById(R.id.checkBox1);
checkBox2 = findViewById(R.id.checkBox2);
checkBox3 = findViewById(R.id.checkBox3);

buttonOutput = findViewById(R.id.buttonOutput);
quizSubmit = findViewById(R.id.quizSubmit);

// Button Code

buttonOutput.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringBuffer output = new StringBuffer();
if (checkBox1.isChecked()) {
output.append(checkBox1.getText());
}
if (checkBox2.isChecked()) {
output.append(“\n”+ checkBox2.getText());
}
if (checkBox3.isChecked()) {
output.append(“\n”+ checkBox3.getText());
}
if (checkBox1.isChecked() || checkBox2.isChecked() || checkBox3.isChecked()) {
Toast.makeText(checkBoxActivity.this, “” + output, Toast.LENGTH_SHORT).show();
}
}
});

quizSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringBuffer quiz = new StringBuffer();
if (checkAns1.isChecked()) {
quiz.append(“\n”+ checkAns1.getText());
}
if (checkAns2.isChecked()) {
quiz.append(“\n”+ checkAns2.getText());
}
if (checkAns3.isChecked()) {
quiz.append(“\n”+ checkAns3.getText());
}
if (checkAns4.isChecked()) {
quiz.append(“\n”+ checkAns4.getText());
}
if (checkAns1.isChecked() || checkAns2.isChecked() || checkAns3.isChecked() || checkAns4.isChecked()) {
Toast.makeText(checkBoxActivity.this, “” + quiz, Toast.LENGTH_SHORT).show();
}
}
});

}
}

Android Manifest File

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.eiheducation.demo”>

<application
android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”
android:roundIcon=”@mipmap/ic_launcher_round”
android:supportsRtl=”true”
android:theme=”@style/Theme.EIHeducationDemo”>
<activity
android:name=”.MainActivity”
android:label=”@string/app_name”
android:theme=”@style/Theme.EIHeducationDemo.NoActionBar”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
<activity android:name=”.checkBoxActivity” />
</application>

</manifest>

Android CheckBox – Output

Check Out the Headphones – 50% to 70% Off
Check Out the Earbuds – 50% to 70% Off
Grammarly – Best Free Grammar checker | Spelling checker | Mistake-free document writer for Laptop/Desktop
Check Out The LAB Configuration eBook – Class A, Class B, Class C, MiN worth $78 Get at $18

Leave a Reply

Your email address will not be published.