In This example creating checkbox and listener to listen checkbox click and show selected checkbox value.

Checkboxes allow the user to select one or more options from a set.

Create a CheckBox in your layout. Checkbox options allows the user to select multiple items, each checkbox is managed separately and you must register a click listener for each one.

 

Project Structure :

 

checkbox basics project sketch

 

File : res/layout/main.xml

 


        <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >    
            
                    <CheckBox
                        android:id="@+id/android"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Android" />
                    
                    <CheckBox
                        android:id="@+id/java"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Java"
                        android:checked="true" />
                    
                    <CheckBox
                        android:id="@+id/opencv"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="OpenCV" />
                    
                    <Button
                        android:id="@+id/Clickhere"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Click here" />
        
        </LinearLayout>

 

File : src/CheckboxExample.java

 


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

public class CheckboxExample extends Activity {

    private CheckBox android, java, opencv;
    private Button Clickhere;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        android   = (CheckBox) findViewById(R.id.android);
        java      = (CheckBox) findViewById(R.id.java);
        opencv    = (CheckBox) findViewById(R.id.opencv);
        Clickhere = (Button) findViewById(R.id.Clickhere);
        
        
        
        Clickhere.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                // Create string buffer to

                StringBuffer OUTPUT = new StringBuffer();
                OUTPUT.append("Android : ")
                        .append(android.isChecked());

                OUTPUT.append("\nJava : ").append(
                        java.isChecked());

               OUTPUT.append("\nOpenCV :").append(
                        opencv.isChecked());

                Toast.makeText(CheckboxExample.this, OUTPUT.toString(),
                        Toast.LENGTH_LONG).show();

            }
        });
        
    }
}


 

Conclusion :

Final words- Just download the project and run it using your emulator and if you have any doubts or suggestions, please place them in the comments.