Checkbox in Android: Step-by-Step Tutorial

Android CheckBox :

Android CheckBox, Generally when there is form or any any other kind where we have to choose multiple options then we use check-box.

Several application use it to create a checklist,  enabling or disabling services, selecting products, contacts , messages and many more much easier than selection individually.

If you want to select one out of two or more but only a single option(one button radio) then radio-button is the best one for it can refer below link

To avoid unwanted data and also to choose from available data we use android checkbox, we can select multiple options while making use of checkbox which looks similar to radio buttons.

custom radio button androidandroid radio button example 

https://androidcoding.in/radio-button/

now i hope you understand difference between android checkbox and radiobutton

A Check-box in android can also be customized  i.e., design can be changed accordingly color of check box and backgrounds can be changed on user click.

In checkbox checked and unchecked states are available you can provide user with an option to choose from two available options.

In this android checkbox example i will show how to make a check on a check-box to select values from present three values user can select any value or all the available values and on clicking submit button selected value is toasted one by one.

 

MainActivity.java :

Using a if condition we are assigning functionality on each checkbox selection, on submit button click you can see the toast appearing for the selected values.

This is a simple basic example where we are using if conditions you can alter according to your requirement.

You can even add android checkbox listener if any query’s in implementation let me know in comment section.

Make android checkbox set checked programmatically to false by default.

public class MainActivity extends Activity {

	CheckBox Chocolates, IceCream, Wafers;
	Button submitbtn;


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

		Chocolates = (CheckBox) findViewById(R.id.Chocolates);
		IceCream    = (CheckBox) findViewById(R.id.IceCream);
		Wafers  = (CheckBox) findViewById(R.id.Wafers);
		submitbtn = (Button) findViewById(R.id.submitbtn);



		submitbtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {


		 if(Chocolates.isChecked()){
		 Toast.makeText(MainActivity.this, "Selected --> Chocolates",
		 			Toast.LENGTH_LONG).show();
			        }
		if(IceCream.isChecked()){
		 Toast.makeText(MainActivity.this, "Selected --> IceCream",
					Toast.LENGTH_LONG).show();
			        }
		if(Wafers.isChecked()){
		 Toast.makeText(MainActivity.this, "Selected --> Wafers",
					Toast.LENGTH_LONG).show();
			        }

			}
		});

	}

 

Add check boxes in layout file design can be made based on number of check boxes.

 

activity_main.xml :

In this layout file we have added 2 Android CheckBox components to demonstrate the usage of it.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.androidcoding.abhi.check_box.MainActivity" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Check-Box"
        android:layout_marginTop="80dp"
        android:layout_gravity="center"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <CheckBox
        android:id="@+id/Chocolates"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:layout_marginTop="50dp"
        android:text="Chocolates" />

    <CheckBox
        android:id="@+id/IceCream"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:textSize="25dp"
        android:text="Ice-Cream" />

    <CheckBox
        android:id="@+id/Wafers"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:textSize="25dp"
        android:text="Wafers" />

    <Button
        android:id="@+id/submitbtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:text="Submit" />

</LinearLayout>



AndroidManifest.xml :

There is nothing new to be specified in the manifest file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.androidcoding.abhi.check_box"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
      <activity
            android:name="com.androidcoding.abhi.check_box.MainActivity" 
            android:label="@string/app_name" > 
      <intent-filter> 
            <action android:name="android.intent.action.MAIN" /> 
            <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
    </activity> 
    </application> 
    </manifest>

 

Android CheckBox Output :

The screen below depicts the implementation of checkbox in android usage in your app.

 

Android CheckBox

 

checkbox2

 

If you have any query regarding this tutorial on checkbox android studio let me know below in comment section.

Like and share for more interesting android tutorials.

Show Buttons
Hide Buttons
Read previous post:
Designing User-Friendly Alert Boxes in Android: Tutorial and Best Practices

  Android Alert Box : An android alertdialog/ android alert box is used to show a alert message to user...

Close