How to Add Android Checkbox With a Group of Selectable Options in Android App

0
Shares
How to Add Android Checkbox With a Group of Selectable Options in Android App
2.3 (46.67%) 3 votes

You have already seen the checkbox right? Probably while filling the registration form on some website. Well, they can be added on 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, 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 that responds to user click event. Whenever user selects a checkbox button, it will display a toast with its state.

Let’s Get Started

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

imagepicker1

Select Mini SDK Version in the next step.

imagepicker2

Add an Empty Activity.

imagepicker3

And lastly, customize the Activity.

imagepicker4

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;
}
}
}

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

If you face any difficulty while implementing it, you can contact our developers for help. And if you’re not aware why the checkbox still really matter, well you should know that they do contribute in 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.

 
0
Shares
 

Want to Develop Android App From Scratch? Contact Us Now