To create each checkbox option, create a
CheckBox in your layout. Because
a set of checkbox options lets the user select multiple items, each checkbox is managed separately,
and you must register a click listener for each one.
Respond to click events
Begin by creating a layout with CheckBox objects in a list:
Once your layout is ready, head to your Activity or Fragment, find your
CheckBox views, and set a change listener, as in the following example:
Kotlin
findViewById<CheckBox>(R.id.checkbox_meat).setOnCheckedChangeListener{buttonView,isChecked->
Log.d("CHECKBOXES","Meat is checked: $isChecked")}findViewById<CheckBox>(R.id.checkbox_cheese).setOnCheckedChangeListener{buttonView,isChecked->
Log.d("CHECKBOXES","Cheese is checked: $isChecked")}
Java
findViewById<CheckBox>(R.id.checkbox_meat).setOnCheckedChangeListener{buttonView,isChecked->
Log.d("CHECKBOXES","Meat is checked: $isChecked");}findViewById<CheckBox>(R.id.checkbox_cheese).setOnCheckedChangeListener{buttonView,isChecked->
Log.d("CHECKBOXES","Cheese is checked: $isChecked");}
The previous code prints a message in Logcat every time the checkboxes change status.
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2024-10-31 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-10-31 UTC."],[],[],null,["# Add checkboxes to your app\n\nTry the Compose way \nJetpack Compose is the recommended UI toolkit for Android. Learn how to add components in Compose. \n[Checkbox →](/develop/ui/compose/components/checkbox) \n| **Note:** For a better user experience, see the [Material Design Checkbox](https://m3.material.io/components/checkbox/overview) documentation.\n\nCheckboxes let the user select one or more options from a set. Typically, you present checkbox\noptions in a vertical list.\n**Figure 1.** An example of checkboxes from [Material Design Checkbox](https://m3.material.io/components/checkbox/guidelines).\n\nTo create each checkbox option, create a\n[CheckBox](/reference/android/widget/CheckBox) in your layout. Because\na set of checkbox options lets the user select multiple items, each checkbox is managed separately,\nand you must register a click listener for each one.\n\nRespond to click events\n-----------------------\n\nBegin by creating a layout with `CheckBox` objects in a list: \n\n```xml\n\u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n\u003cLinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n android:orientation=\"vertical\"\n android:layout_width=\"match_parent\"\n android:layout_height=\"match_parent\"\u003e\n \u003cCheckBox android:id=\"@+id/checkbox_meat\"\n android:layout_width=\"wrap_content\"\n android:layout_height=\"wrap_content\"\n android:text=\"Meat\" /\u003e\n \u003cCheckBox android:id=\"@+id/checkbox_cheese\"\n android:layout_width=\"wrap_content\"\n android:layout_height=\"wrap_content\"\n android:text=\"Cheese\"/\u003e\n\u003c/LinearLayout\u003e\n```\n\nOnce your layout is ready, head to your `Activity` or `Fragment`, find your\n`CheckBox` views, and set a change listener, as in the following example: \n\n### Kotlin\n\n```kotlin\nfindViewById\u003cCheckBox\u003e(R.id.checkbox_meat)\n .setOnCheckedChangeListener { buttonView, isChecked -\u003e\n Log.d(\"CHECKBOXES\", \"Meat is checked: $isChecked\")\n }\n\nfindViewById\u003cCheckBox\u003e(R.id.checkbox_cheese)\n .setOnCheckedChangeListener { buttonView, isChecked -\u003e\n Log.d(\"CHECKBOXES\", \"Cheese is checked: $isChecked\")\n }\n```\n\n### Java\n\n```java\nfindViewById\u003cCheckBox\u003e(R.id.checkbox_meat)\n .setOnCheckedChangeListener { buttonView, isChecked -\u003e\n Log.d(\"CHECKBOXES\", \"Meat is checked: $isChecked\");\n }\n\nfindViewById\u003cCheckBox\u003e(R.id.checkbox_cheese)\n .setOnCheckedChangeListener { buttonView, isChecked -\u003e\n Log.d(\"CHECKBOXES\", \"Cheese is checked: $isChecked\");\n }\n```\n\nThe previous code prints a message in Logcat every time the checkboxes change status.\n| **Tip:** If you need to change the checkbox state yourself, use the [setChecked(boolean)](/reference/android/widget/CompoundButton#setChecked(boolean)) or [toggle()](/reference/android/widget/CompoundButton#toggle()) method."]]