{"id":878,"date":"2016-02-10T07:58:23","date_gmt":"2016-02-10T07:58:23","guid":{"rendered":"http:\/\/abhiandroid.com\/ui\/?page_id=878"},"modified":"2019-06-12T06:44:21","modified_gmt":"2019-06-12T06:44:21","slug":"checkbox","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/ui\/checkbox","title":{"rendered":"CheckBox Tutorial With Example In Android Studio"},"content":{"rendered":"<p>In Android, CheckBox is a type of two state button either unchecked or checked in Android. Or you can\u00a0say it is a type of\u00a0on\/off switch that can be toggled by the users. You should use checkbox when presenting a group of selectable options to users that are not mutually exclusive.\u00a0CompoundButton is the parent class of CheckBox class.<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-902\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/CheckBox.jpg\" alt=\"CheckBox\" width=\"489\" height=\"242\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/CheckBox.jpg 489w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/CheckBox-300x148.jpg 300w\" sizes=\"auto, (max-width: 489px) 100vw, 489px\" \/><\/center>In android there is a lot of usage of check box. For example, to take survey in\u00a0Android app we can list few options and allow user to choose using CheckBox. The\u00a0user will\u00a0simply checked these checkboxes rather than type their own option in <a href=\"\/ui\/edittext\">EditText<\/a>. Another very common use of CheckBox is as remember me option in\u00a0Login form.<\/p>\n<p><strong>CheckBox code in XML:<\/strong><\/p>\n<pre>&lt;CheckBox\r\nandroid:id=\"@+id\/simpleCheckBox\"\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:text=\"Simple CheckBox\"\/&gt;\r\n<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-880\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/CheckBox-In-Android-300x211.jpg\" alt=\"CheckBox In Android\" width=\"300\" height=\"211\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/CheckBox-In-Android-300x211.jpg 300w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/CheckBox-In-Android.jpg 315w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/center><span style=\"color: #ff0000;\"><strong>Important Note:<\/strong><\/span> You can check the current state of a check box programmatically by using isChecked() method. This method returns a Boolean value either\u00a0true or false, if a check box is checked then it returns true otherwise it returns false. Below is an example code in which we checked the current state of a check box.<\/p>\n<pre>\/\/initiate a check box\r\nCheckBox simpleCheckBox = (CheckBox) findViewById(R.id.<em>simpleCheckBox<\/em>);\r\n\r\n\/\/check current state of a check box (true or false)\r\nBoolean checkBoxState = simpleCheckBox.isChecked();\r\n<\/pre>\n<hr \/>\n<h4><strong>Attributes of CheckBox:<\/strong><\/h4>\n<p>Now let\u2019s \u00a0we discuss important\u00a0attributes that helps us to configure a check box in XML\u00a0file (layout).<\/p>\n<p><span style=\"color: #008000;\"><strong>1.id:<\/strong><\/span>\u00a0id is an attribute used to uniquely identify a check box. Below we set the id of a image button.<\/p>\n<pre>&lt;CheckBox\r\nandroid:id=\"@+id\/simpleCheckBox\"\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:text=\"Simple CheckBox\"\/&gt;<\/pre>\n<p><span style=\"color: #008000;\"><strong>2. checked: <\/strong><\/span>checked is an attribute of check box used to set the current state of a check box. The value should be true or false where true shows the checked state and false shows unchecked state of a check box. The default value of checked attribute is false. We can also set the current state programmatically.<\/p>\n<p>Below is an example code in which we set true value for checked attribute that sets the current state to checked.<\/p>\n<pre>&lt;CheckBox\r\n    android:id=\"@+id\/simpleCheckBox\"\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:text=\"Simple CheckBox\" \r\n    android:checked=\"true\"\/&gt; &lt;!--set the current state of the check box--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-881\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/checked-in-CheckBox-Android-300x216.jpg\" alt=\"checked in CheckBox Android\" width=\"300\" height=\"216\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/checked-in-CheckBox-Android-300x216.jpg 300w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/checked-in-CheckBox-Android.jpg 312w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/center><strong>Setting Current State Of CheckBox\u00a0In Java Class:<\/strong><\/p>\n<p>Below we set the current state of CheckBox in java class.<\/p>\n<pre>\/*Add in Oncreate() funtion after setContentView()*\/\r\n\/\/ initiate a check box\r\nCheckBox simpleCheckBox = (CheckBox) findViewById(R.id.<em>simpleCheckBox<\/em>);\r\n\r\n\/\/ set the current state of a check box\r\nsimpleCheckBox.setChecked(true);\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>3. gravity: <\/strong><\/span>The gravity attribute is an optional attribute which is used to control the alignment of the text in CheckBox like left, right, center, top, bottom, center_vertical, center_horizontal etc.<\/p>\n<p>Below we set the right and center_vertical gravity for the text of a check box.<\/p>\n<pre>&lt;CheckBox\r\n    android:id=\"@+id\/simpleCheckBox\"\r\n    android:layout_width=\"fill_parent\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:text=\"Simple CheckBox\"\r\n    android:checked=\"true\"\r\n    android:gravity=\"right|center_vertical\"\/&gt;  &lt;!-- gravity of the check box--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-882\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/gravity-in-CheckBox-Android.jpg\" alt=\"gravity in CheckBox Android\" width=\"252\" height=\"200\" \/><\/center><span style=\"color: #008000;\"><strong>4. text:<\/strong><\/span> text attribute is used to set the text in a check box. We can set the text in xml as well as in the java class.<\/p>\n<p>Below is the example code with explanation included in which we set the text \u201cText Attribute Of Check Box\u201d for a check box.<\/p>\n<pre>&lt;CheckBox\r\n    android:id=\"@+id\/simpleCheckBox\"\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:checked=\"true\"\r\n    android:text=\"Text Attribute Of Check Box\"\/&gt; &lt;!--displayed text of the check box--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-883\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/test-in-CheckBox-Android-300x219.jpg\" alt=\"test in CheckBox Android\" width=\"300\" height=\"219\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/test-in-CheckBox-Android-300x219.jpg 300w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/test-in-CheckBox-Android.jpg 303w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/center><strong>Setting text in CheckBox\u00a0In Java class:<\/strong><\/p>\n<p>Below is the example code in which we set the text of a check box programmatically means in java class.<\/p>\n<pre>\/*Add in Oncreate() funtion after setContentView()*\/\r\n\/\/ initiate check box\r\nCheckBox simpleCheckBox = (CheckBox) findViewById(R.id.<strong><em>simpleCheckBox<\/em><\/strong>);\r\n\r\n\/\/ displayed text of the check box\r\nsimpleCheckBox.setText(<strong>\"Text Attribute Of Check Box\"<\/strong>);<\/pre>\n<p><span style=\"color: #008000;\"><strong>5. textColor:<\/strong><\/span> textColor attribute is used to set the text color of a check box. Color value is in form of &#8220;#argb&#8221;, &#8220;#rgb&#8221;, &#8220;#rrggbb&#8221;, or &#8220;#aarrggbb&#8221;.<\/p>\n<p>Below we set the red color for the displayed text of a check box.<\/p>\n<pre>&lt;CheckBox\r\n    android:id=\"@+id\/simpleCheckBox\"\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:text=\"Text is Red Color\"\r\n    android:textColor=\"#f00\" \r\n    android:checked=\"true\"\/&gt; &lt;!-- red color for the text of check box--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-884\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/textColor-in-Checkbox-Android.jpg\" alt=\"textColor in Checkbox Android\" width=\"299\" height=\"201\" \/><\/center><strong>Setting textColor in CheckBox In Java class:<\/strong><\/p>\n<p>Below we set the text color of a check box programmatically.<\/p>\n<pre>\/*Add in Oncreate() funtion after setContentView()*\/\r\n\/\/initiate the checkbox\r\nCheckBox simpleCheckBox = (CheckBox) findViewById(R.id.<strong><em>simpleCheckBox<\/em><\/strong>);\r\n\r\n\/\/red color for displayed text\r\nsimpleCheckBox.setTextColor(Color.<strong><em>RED<\/em><\/strong>);\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>6. textSize:<\/strong><\/span>\u00a0textSize attribute is used to set the size of text of a check box. We can set the text size in sp(scale independent pixel) or dp(density pixel).<\/p>\n<p>Below is the example code in which we set the 20sp size for the text of a check box.<\/p>\n<pre>&lt;CheckBox\r\n    android:id=\"@+id\/simpleCheckBox\"\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:text=\"Text size Attribute Of Check Box\"\r\n    android:textColor=\"#00f\"\r\n    android:checked=\"false\"\r\n    android:textSize=\"20sp\"\/&gt;&lt;!--set Text Size of text in CheckBox--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-885\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/textSize-in-CheckBox-Android.jpg\" alt=\"textSize in CheckBox Android\" width=\"251\" height=\"194\" \/><\/center><strong>Setting Text Size in CheckBox In Java class:<\/strong><\/p>\n<p>Below we set the text size of a check box in java class.<\/p>\n<pre>\/*Add in Oncreate() funtion after setContentView()*\/\r\nCheckBox simpleCheckBox = (CheckBox) findViewById(R.id.<strong><em>simpleCheckBox<\/em><\/strong>);\r\n\/\/set 20sp displayed text size\r\nsimpleCheckBox.setTextSize(20);\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>7. textStyle:<\/strong><\/span>\u00a0textStyle attribute is used to set the text style of the text of a check box. The possible text styles are bold, italic and normal.\u00a0 If we need to use two or more styles for a text view then \u201c|\u201d operator is used for that.<\/p>\n<p>Below we set the bold and italic text styles for text of a check box.<\/p>\n<pre>&lt;CheckBox\r\n    android:id=\"@+id\/simpleCheckBox\"\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:text=\"Text Style Attribute Of Check Box\"\r\n    android:textColor=\"#44f\"\r\n    android:textSize=\"20sp\"\r\n    android:checked=\"false\"\r\n    android:textStyle=\"bold|italic\"\/&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-886\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/textStyle-in-CheckBox-Android-300x205.jpg\" alt=\"textStyle in CheckBox Android\" width=\"300\" height=\"205\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/textStyle-in-CheckBox-Android-300x205.jpg 300w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/textStyle-in-CheckBox-Android.jpg 304w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/center><span style=\"color: #008000;\"><strong>8. background:<\/strong><\/span>\u00a0background attribute is used to set the background of a check box. We can set a color or a drawable in the background of a check box.<\/p>\n<p>Below we set the black color for the background, white color for the displayed text of a check box.<\/p>\n<pre>&lt;CheckBox\r\n    android:id=\"@+id\/simpleCheckBox\"\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:text=\"Text size Attribute Of Check Box\"\r\n    android:textColor=\"#fff\"\r\n    android:textSize=\"20sp\"\r\n    android:textStyle=\"bold|italic\"\r\n    android:checked=\"true\"\r\n    android:background=\"#000\" \/&gt;&lt;!-- black background for the background of check box--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-887\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/Background-and-TextColor-in-CheckBox-300x223.jpg\" alt=\"Background and TextColor in CheckBox\" width=\"300\" height=\"223\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/Background-and-TextColor-in-CheckBox-300x223.jpg 300w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/Background-and-TextColor-in-CheckBox.jpg 302w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/center><strong>Setting Background in CheckBox In Java class:<\/strong><\/p>\n<p>Below is the example code in which we set the background color of a check box programmatically means in java class.<\/p>\n<pre>\/*Add in Oncreate() funtion after setContentView()*\/\r\nCheckBox simpleCheckBox = (CheckBox) findViewById(R.id.<strong><em>simpleCheckBox<\/em><\/strong>);\r\n\/\/ set background in CheckBox\r\nsimpleCheckBox.setBackgroundColor(Color.<strong><em>BLACK<\/em><\/strong>);\r\n<\/pre>\n<p><strong><span style=\"color: #008000;\">9. padding:<\/span>\u00a0<\/strong>padding attribute is used to set the padding from left, right, top or bottom.<\/p>\n<ul>\n<li><strong>paddingRight :<\/strong>set the padding from the right side of the check box<strong>.<\/strong><\/li>\n<li><strong>paddingLeft :<\/strong>set the padding from the left side of the check box<strong>.<\/strong><\/li>\n<li><strong>paddingTop :<\/strong>set the padding from the top side of the check box<strong>.<\/strong><\/li>\n<li><strong>paddingBottom :<\/strong>set the padding from the bottom side of the check box<strong>.<\/strong><\/li>\n<li><strong>Padding :<\/strong>set the padding from the all side\u2019s of the check box<strong>.<\/strong><\/li>\n<\/ul>\n<p>Below is the example code of padding where\u00a0we set the 30dp padding from all the side\u2019s of the check box.<\/p>\n<pre>&lt;CheckBox\r\n    android:id=\"@+id\/simpleCheckBox\"\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:text=\"Padding Attribute Of Check Box\"\r\n    android:textColor=\"#44f\" \r\n    android:textSize=\"20sp\"\r\n    android:textStyle=\"bold|italic\"\r\n    android:checked=\"false\"\r\n    android:padding=\"30dp\"\/&gt; &lt;!--30dp padding from all side\u2019s--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-888\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/Padding-in-CheckBox-Android-300x241.jpg\" alt=\"Padding in CheckBox Android\" width=\"300\" height=\"241\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/Padding-in-CheckBox-Android-300x241.jpg 300w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/Padding-in-CheckBox-Android.jpg 307w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/center><\/p>\n<hr \/>\n<h4><strong>Example of CheckBox In Android Studio:<\/strong><\/h4>\n<p>Below is the example of CheckBox in Android, in which we display five check boxes using\u00a0background and other attributes we discusses earlier in this post. Every check box represents a different subject name and whenever you click on a check box then text of that checked check box is displayed in a toast. Below is the final output, code and step by step explanation of the example:<\/p>\n<p style=\"text-align: center;\"><a class=\"download\" href=\"https:\/\/github.com\/abhisheksaini4\/CheckBoxExample\" target=\"_blank\" rel=\"nofollow\">Download Code<\/a> <a class=\"help\" title=\"Learn How To Download Code And Import In Android Studio\" href=\"\/androidstudio\/download-code-abhiandroid\" target=\"_blank\" rel=\"nofollow\"> ? <\/a><\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-904\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/CheckBox-Example-in-Android-Studio.jpg\" alt=\"CheckBox Example in Android Studio\" width=\"238\" height=\"410\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/CheckBox-Example-in-Android-Studio.jpg 238w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/CheckBox-Example-in-Android-Studio-174x300.jpg 174w\" sizes=\"auto, (max-width: 238px) 100vw, 238px\" \/><\/center><span style=\"color: #008000;\"><strong>Step 1:<\/strong><\/span> Create a new project and name it CheckBoxExample<\/p>\n<p>In this step we create a new project in android studio by filling all the necessary\u00a0details of the app like app name, package name, api versions etc.<\/p>\n<pre>Select File -&gt; New -&gt; New Project and Fill the forms and click \"Finish\" button.<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 2:<\/strong><\/span> Now Open res -&gt; layout -&gt;\u00a0<strong>activity_main.<\/strong><strong>xml (or) main.xml\u00a0<\/strong>and add the following code:<\/p>\n<p>In this step we open an xml file and add the code for displaying five one TextView and check boxes.<\/p>\n<pre>&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    android:paddingBottom=\"@dimen\/activity_vertical_margin\"\r\n    android:paddingLeft=\"@dimen\/activity_horizontal_margin\"\r\n    android:paddingRight=\"@dimen\/activity_horizontal_margin\"\r\n    android:paddingTop=\"@dimen\/activity_vertical_margin\"\r\n    tools:context=\".MainActivity\"&gt;\r\n\r\n    &lt;TextView\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:text=\"Select Your Programming language: \"\r\n        android:textColor=\"#f00\"\r\n        android:textSize=\"20sp\"\r\n        android:textStyle=\"bold\" \/&gt;\r\n\r\n    &lt;LinearLayout\r\n        android:id=\"@+id\/linearLayout\"\r\n        android:layout_width=\"fill_parent\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_marginTop=\"30dp\"\r\n        android:background=\"#e0e0e0\"\r\n        android:orientation=\"vertical\"&gt;\r\n\r\n        &lt;CheckBox\r\n            android:id=\"@+id\/androidCheckBox\"\r\n            android:layout_width=\"wrap_content\"\r\n            android:layout_height=\"wrap_content\"\r\n            android:layout_centerHorizontal=\"true\"\r\n            android:checked=\"false\"\r\n            android:padding=\"20dp\"\r\n            android:text=\"@string\/android\"\r\n            android:textColor=\"#44f\"\r\n            android:textSize=\"20sp\"\r\n            android:textStyle=\"bold|italic\" \/&gt;\r\n\r\n        &lt;CheckBox\r\n            android:id=\"@+id\/javaCheckBox\"\r\n            android:layout_width=\"wrap_content\"\r\n            android:layout_height=\"wrap_content\"\r\n            android:layout_centerHorizontal=\"true\"\r\n            android:checked=\"false\"\r\n            android:padding=\"20dp\"\r\n            android:text=\"@string\/java\"\r\n            android:textColor=\"#f44\"\r\n            android:textSize=\"20sp\"\r\n            android:textStyle=\"bold|italic\" \/&gt;\r\n\r\n        &lt;CheckBox\r\n            android:id=\"@+id\/phpCheckBox\"\r\n            android:layout_width=\"wrap_content\"\r\n            android:layout_height=\"wrap_content\"\r\n            android:layout_centerHorizontal=\"true\"\r\n            android:checked=\"false\"\r\n            android:padding=\"20dp\"\r\n            android:text=\"@string\/php\"\r\n            android:textColor=\"#444\"\r\n            android:textSize=\"20sp\"\r\n            android:textStyle=\"bold|italic\" \/&gt;\r\n\r\n        &lt;CheckBox\r\n            android:id=\"@+id\/pythonCheckBox\"\r\n            android:layout_width=\"wrap_content\"\r\n            android:layout_height=\"wrap_content\"\r\n            android:layout_centerHorizontal=\"true\"\r\n            android:checked=\"false\"\r\n            android:padding=\"20dp\"\r\n            android:text=\"@string\/python\"\r\n            android:textColor=\"#888\"\r\n            android:textSize=\"20sp\"\r\n            android:textStyle=\"bold|italic\" \/&gt;\r\n\r\n        &lt;CheckBox\r\n            android:id=\"@+id\/unityCheckBox\"\r\n            android:layout_width=\"wrap_content\"\r\n            android:layout_height=\"wrap_content\"\r\n            android:layout_centerHorizontal=\"true\"\r\n            android:checked=\"false\"\r\n            android:padding=\"20dp\"\r\n            android:text=\"@string\/unity\"\r\n            android:textColor=\"#101010\"\r\n            android:textSize=\"20sp\"\r\n            android:textStyle=\"bold|italic\" \/&gt;\r\n    &lt;\/LinearLayout&gt;\r\n\r\n&lt;\/RelativeLayout&gt;<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 3:<\/strong><\/span> Now Open \u00a0app -&gt;\u00a0java-&gt; package -&gt;\u00a0<strong>MainActivity.<\/strong><strong>java<\/strong><\/p>\n<p>In this step we add the code to\u00a0initiate the check boxes we created. And then we perform click event on button and display the text for selected check boxes using a toast.<\/p>\n<pre>package example.abhiandriod.checkboxexample;\r\n\r\nimport android.graphics.Color;\r\nimport android.support.v7.app.AppCompatActivity;\r\nimport android.os.Bundle;\r\nimport android.view.Menu;\r\nimport android.view.MenuItem;\r\nimport android.view.View;\r\nimport android.widget.Button;\r\nimport android.widget.CheckBox;\r\nimport android.widget.Toast;\r\n\r\npublic class MainActivity extends AppCompatActivity implements View.OnClickListener {\r\n\r\n    CheckBox android, java, python, php, unity3D;\r\n\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_main);\r\n        \/\/ initiate views\r\n        android = (CheckBox) findViewById(R.id.androidCheckBox);\r\n        android.setOnClickListener(this);\r\n        java = (CheckBox) findViewById(R.id.javaCheckBox);\r\n        java.setOnClickListener(this);\r\n        python = (CheckBox) findViewById(R.id.pythonCheckBox);\r\n        python.setOnClickListener(this);\r\n        php = (CheckBox) findViewById(R.id.phpCheckBox);\r\n        php.setOnClickListener(this);\r\n        unity3D = (CheckBox) findViewById(R.id.unityCheckBox);\r\n        unity3D.setOnClickListener(this);\r\n    }\r\n\r\n    @Override\r\n    public void onClick(View view) {\r\n\r\n        switch (view.getId()) {\r\n            case R.id.androidCheckBox:\r\n                if (android.isChecked())\r\n                    Toast.makeText(getApplicationContext(), \"Android\", Toast.LENGTH_LONG).show();\r\n                break;\r\n            case R.id.javaCheckBox:\r\n                if (java.isChecked())\r\n                    Toast.makeText(getApplicationContext(), \"Java\", Toast.LENGTH_LONG).show();\r\n                break;\r\n            case R.id.phpCheckBox:\r\n                if (php.isChecked())\r\n                    Toast.makeText(getApplicationContext(), \"PHP\", Toast.LENGTH_LONG).show();\r\n                break;\r\n            case R.id.pythonCheckBox:\r\n                if (python.isChecked())\r\n                    Toast.makeText(getApplicationContext(), \"Python\", Toast.LENGTH_LONG).show();\r\n                break;\r\n            case R.id.unityCheckBox:\r\n                if (unity3D.isChecked())\r\n                    Toast.makeText(getApplicationContext(), \"Unity 3D\", Toast.LENGTH_LONG).show();\r\n                break;\r\n        }\r\n    }\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 5:<\/strong><\/span> Open res\u00a0-&gt;values -&gt;\u00a0<strong>strings.<\/strong><strong>xml<\/strong><\/p>\n<p>In this step we show string file which is used to store string data of an\u00a0app.<strong>\u00a0<\/strong><\/p>\n<pre>&lt;resources&gt;\r\n    &lt;string name=\"app_name\"&gt;CheckBoxExample&lt;\/string&gt;\r\n    &lt;string name=\"hello_world\"&gt;Hello world!&lt;\/string&gt;\r\n    &lt;string name=\"action_settings\"&gt;Settings&lt;\/string&gt;\r\n    &lt;string name=\"android\"&gt;Android&lt;\/string&gt;\r\n    &lt;string name=\"java\"&gt;Java&lt;\/string&gt;\r\n    &lt;string name=\"php\"&gt;PHP&lt;\/string&gt;\r\n    &lt;string name=\"python\" &gt;Python&lt;\/string&gt;\r\n    &lt;string name=\"unity\"&gt;Unity 3D&lt;\/string&gt;\r\n&lt;\/resources&gt;<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<p>Now start the AVD in Emulator and run the App. You will see 5 checkbox option asking you to choose your programming language. Select and the text of that particular CheckBox will appear on Screen.<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-900\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/CheckBox-Example-Output-Android-Studio.jpg\" alt=\"CheckBox Example Output Android Studio\" width=\"208\" height=\"340\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/CheckBox-Example-Output-Android-Studio.jpg 208w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/CheckBox-Example-Output-Android-Studio-184x300.jpg 184w\" sizes=\"auto, (max-width: 208px) 100vw, 208px\" \/><\/center><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Android, CheckBox is a type of two state button either unchecked or checked in Android. Or you can\u00a0say it is a type of\u00a0on\/off switch that can be toggled by the users. You should use checkbox when presenting a group of selectable options to users that are not mutually exclusive.\u00a0CompoundButton is the parent class of &hellip; <a href=\"https:\/\/abhiandroid.com\/ui\/checkbox\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">CheckBox Tutorial With Example In Android Studio<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"home.php","meta":{"_acf_changed":false,"footnotes":""},"class_list":["post-878","page","type-page","status-publish","hentry"],"acf":[],"psp_head":"<title>CheckBox Tutorial With Example In Android Studio \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Learn use of CheckBox in Android following our complete tutorial with example in Android Studio. CheckBox is a type of two state button either unchecked or checked in Android.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/ui\/checkbox\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/878","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/comments?post=878"}],"version-history":[{"count":3,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/878\/revisions"}],"predecessor-version":[{"id":2779,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/878\/revisions\/2779"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media?parent=878"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}