A CheckBox is an on/off switch that can be toggled by the user. You should use check-boxes when presenting users with a group of selectable options that are not mutually exclusive.
Following are the important attributes related to CheckBox control. You can check Android official documentation for complete list of attributes and related methods which you can use to change these attributes are run time.
Attribute | Description |
---|---|
android:autoText | If set, specifies that this TextView has a textual input method and automatically corrects some common spelling errors. |
android:drawableBottom | This is the drawable to be drawn below the text. |
android:drawableRight | This is the drawable to be drawn to the right of the text. |
android:editable | If set, specifies that this TextView has an input method. |
android:text | This is the Text to display. |
In this example i will be demonstrating how to control on CheckBox.
Let’s get start by creating a project in Eclipse IDE.
1. Create a new project in Eclipse by navigating to File ⇒ New Android ⇒ Application Project and fill required details. (I kept my main activity name as MainActivity.java)
2. Open your your AndroidManifest.xml file and make your “MainActivity” as Launcher activity, there is no change in this manifest file.
3. Create a layout file for MainActivtiy.java under res ⇒ layout folder. I named the layout file as activity_main.xml. In this layout there are two layout displaying two separated kind of checkbox.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:baselineAligned="true" android:orientation="horizontal" android:padding="5dp" > <LinearLayout android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:orientation="vertical" android:padding="5dp" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="5dp" android:text="@string/button_click" /> <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/facebook" /> <CheckBox android:id="@+id/checkBox2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hike" /> <CheckBox android:id="@+id/checkBox3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/whatsapp" /> <CheckBox android:id="@+id/checkBox4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/twitter" /> <Button android:id="@+id/click" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:padding="10dp" android:text="@string/click" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:orientation="vertical" android:padding="5dp" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="5dp" android:text="@string/check_button_click" /> <CheckBox android:id="@+id/checkBox5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/twitter" /> <CheckBox android:id="@+id/checkBox6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/facebook" /> <CheckBox android:id="@+id/checkBox7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hike" /> <CheckBox android:id="@+id/checkBox8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/whatsapp" /> </LinearLayout> </LinearLayout> |
4. Now, add the following code in MainActivity.java. In this class we displayed the selected checkbox name and it is checked or not via button click and check box clicking.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
package com.checkbox; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.view.View.OnClickListener; import android.widget.CheckBox; import android.widget.Toast; public class MainActivity extends ActionBarActivity implements OnClickListener { CheckBox check1, check2, check3, check4, check5, check6, check7, check8; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Getting all checkbox IDs check1 = (CheckBox) findViewById(R.id.checkBox1); check2 = (CheckBox) findViewById(R.id.checkBox2); check3 = (CheckBox) findViewById(R.id.checkBox3); check4 = (CheckBox) findViewById(R.id.checkBox4); check5 = (CheckBox) findViewById(R.id.checkBox5); check6 = (CheckBox) findViewById(R.id.checkBox6); check7 = (CheckBox) findViewById(R.id.checkBox7); check8 = (CheckBox) findViewById(R.id.checkBox8); // Implementing click listener on button findViewById(R.id.click).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // String buffer to store to append many strings into one StringBuffer result = new StringBuffer(); // Appending checkBox text and its checked or not (boolean) result.append(check1.getText() + " : ").append( check1.isChecked()); result.append("n" + check2.getText() + " : ").append( check2.isChecked()); result.append("n" + check3.getText() + " : ").append( check3.isChecked()); result.append("n" + check4.getText() + " : ").append( check4.isChecked()); // Displaying string buffer Toast.makeText(MainActivity.this, result.toString(), Toast.LENGTH_LONG).show(); } }); // Implementing click listeners on other check boxes check5.setOnClickListener(this); check6.setOnClickListener(this); check7.setOnClickListener(this); check8.setOnClickListener(this); // You can use this method also to check whether the check box is // checked or not /* * check5.setOnCheckedChangeListener(new OnCheckedChangeListener() { * * @Override public void onCheckedChanged(CompoundButton arg0, boolean * ischeced) { if(ischeced){ Toast.makeText(MainActivity.this, * check5.getText() + " : " + check5.isChecked(), * Toast.LENGTH_LONG).show(); * * } else{ Toast.makeText(MainActivity.this, check5.getText() + " : " + * check5.isChecked(), Toast.LENGTH_LONG).show(); } * * } }); */ } @Override public void onClick(View v) { switch (v.getId()) { // Displaying a toast on particular check box clicked with its name and // checked or not (boolean) case R.id.checkBox5: Toast.makeText(MainActivity.this, check5.getText() + " : " + check5.isChecked(), Toast.LENGTH_LONG).show(); break; case R.id.checkBox6: Toast.makeText(MainActivity.this, check6.getText() + " : " + check6.isChecked(), Toast.LENGTH_LONG).show(); break; case R.id.checkBox7: Toast.makeText(MainActivity.this, check7.getText() + " : " + check7.isChecked(), Toast.LENGTH_LONG).show(); break; case R.id.checkBox8: Toast.makeText(MainActivity.this, check8.getText() + " : " + check8.isChecked(), Toast.LENGTH_LONG).show(); break; } } } |
5. Run the application and you will get the output as shown in video and you are done.
Thanks. 🙂
Subscribe to us and get the latest news.