Android Checkbox Example: How to Add Checkbox Group in Android App

Have you already seen the checkbox in Android right? Probably while filling the registration form on some website. Well, they can be added to mobile apps as well. The Android checkbox is a two-state button that can be either checked or unchecked. If the Android checkbox button is unchecked, the user can click on it to select and vice-versa.

Android Checkbox Example

Today, we’ll be creating a simple app to demonstrate three checkbox Android Studio that responds to user click event. Whenever the user selects a checkbox button, it will display a toast with its state.

Let’s Get Started

👉 Create a new project under the file menu and modify your project details.

Create a new project

👉 Select Mini SDK Version in the next step.

Select Mini SDK Version

👉 Add an Empty Activity.

Add an Empty Activity

👉 And lastly, customize the Activity.

customize the Activity

Start Code Integration

MainActivity

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private CheckBox cbAndroid;
private CheckBox cbIos;
private CheckBox cbWindows;
private Button btnShow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initControls();
}
private void initControls() {
cbAndroid = (CheckBox) findViewById(R.id.cbAndroid);
cbIos = (CheckBox) findViewById(R.id.cbIos);
cbWindows = (CheckBox) findViewById(R.id.cbWindows);
btnShow = (Button) findViewById(R.id.btnShow);
btnShow.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnShow:
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(cbAndroid.isChecked() ? "Android is checked" : "Android is unchecked");
stringBuffer.append(cbIos.isChecked() ? "\nIos is checked" : "\nIos is unchecked");
stringBuffer.append(cbWindows.isChecked() ? "\nWindows is checked" : "\nWindows is unchecked");
Toast.makeText(this, stringBuffer, Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}

Want To Create An Android Application?

Looking to Create An Android app? Get in touch with our experienced Android app developers for a free consultation.

Cta Image

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context="com.checkboxdemo.MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<CheckBox
android:id="@+id/cbAndroid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/android" />
<CheckBox
android:id="@+id/cbIos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ios" />
<CheckBox
android:id="@+id/cbWindows"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/windows" />
<Button
android:id="@+id/btnShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:text="@string/show" />
</LinearLayout>
</LinearLayout>

Strings.xml

<resources>
<string name="android">Android</string>
<string name="ios">Ios</string>
<string name="windows">Windows</string>
<string name="show">Show</string>
</resources>

Dimens.xml

<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>

And done!

If you followed all the steps, demo should run just fine and look like this:

Android checkbox example

If you face any difficulty while implementing it, you can contact our Android developers for help. And if you’re not aware why the checkbox still really matter, you should know that they do contribute to making your contact & registration forms user-friendly. So if you’re developing a mobile app, do remember this point, or hire Android app development company to create a strategic development plan for your Android app.

Grab a free copy of Android checkbox example demo from Github.

Bhaval Patel

Written by

Bhaval Patel is a Director (Operations) at Space-O Technologies. He has 20+ years of experience helping startups and enterprises with custom software solutions to drive maximum results. Under his leadership, Space-O has won the 8th GESIA annual award for being the best mobile app development company. So far, he has validated more than 300 app ideas and successfully delivered 100 custom solutions using the technologies, such as Swift, Kotlin, React Native, Flutter, PHP, RoR, IoT, AI, NFC, AR/VR, Blockchain, NFT, and more.